Skip to content

Commit 88f6c00

Browse files
authored
update crane mutate annotation/label args to allow commas in label values (#1178)
With the current argument type, a common in a label value (a reasonable thing to have) will be split and parsed as a separate label, and the client errors. This commit updates the flag to StringToStringVarP so we start with a map[string]string off the bat, and do not need to do special parsing beyond checking for empty strings. Signed-off-by: vsoch <[email protected]> Co-authored-by: vsoch <[email protected]>
1 parent f30efdd commit 88f6c00

File tree

5 files changed

+27
-27
lines changed

5 files changed

+27
-27
lines changed

CONTRIBUTING.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,9 @@ Ensure the following passes:
2828
```
2929
./hack/presubmit.sh
3030
```
31-
and commit any resultant changes to `go.mod` and `go.sum`.
31+
and commit any resultant changes to `go.mod` and `go.sum`. To update any docs
32+
after client changes, run:
33+
34+
```
35+
./hack/update-codegen.sh
36+
```

cmd/crane/cmd/mutate.go

+14-18
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ package cmd
1717
import (
1818
"fmt"
1919
"log"
20-
"strings"
2120

2221
"github.com/google/go-containerregistry/pkg/crane"
2322
"github.com/google/go-containerregistry/pkg/name"
@@ -28,20 +27,20 @@ import (
2827

2928
// NewCmdMutate creates a new cobra.Command for the mutate subcommand.
3029
func NewCmdMutate(options *[]crane.Option) *cobra.Command {
31-
var lbls []string
30+
var labels map[string]string
3231
var entrypoint string
3332
var newRef string
34-
var anntns []string
33+
var annotations map[string]string
3534

3635
mutateCmd := &cobra.Command{
3736
Use: "mutate",
38-
Short: "Modify image labels and annotations",
37+
Short: "Modify image labels and annotations. The container must be pushed to a registry, and the manifest is updated there.",
3938
Args: cobra.ExactArgs(1),
4039
Run: func(_ *cobra.Command, args []string) {
4140
// Pull image and get config.
4241
ref := args[0]
4342

44-
if len(anntns) != 0 {
43+
if len(annotations) != 0 {
4544
desc, err := crane.Head(ref, *options...)
4645
if err != nil {
4746
log.Fatalf("checking %s: %v", ref, err)
@@ -66,7 +65,7 @@ func NewCmdMutate(options *[]crane.Option) *cobra.Command {
6665
cfg.Config.Labels = map[string]string{}
6766
}
6867

69-
labels, err := splitKeyVals(lbls)
68+
err = validateKeyVals(labels)
7069
if err != nil {
7170
log.Fatal(err)
7271
}
@@ -75,7 +74,7 @@ func NewCmdMutate(options *[]crane.Option) *cobra.Command {
7574
cfg.Config.Labels[k] = v
7675
}
7776

78-
annotations, err := splitKeyVals(anntns)
77+
err = validateKeyVals(annotations)
7978
if err != nil {
8079
log.Fatal(err)
8180
}
@@ -118,22 +117,19 @@ func NewCmdMutate(options *[]crane.Option) *cobra.Command {
118117
fmt.Println(r.Context().Digest(digest.String()))
119118
},
120119
}
121-
mutateCmd.Flags().StringSliceVarP(&anntns, "annotation", "a", nil, "New annotations to add")
122-
mutateCmd.Flags().StringSliceVarP(&lbls, "label", "l", nil, "New labels to add")
120+
mutateCmd.Flags().StringToStringVarP(&annotations, "annotation", "a", nil, "New annotations to add")
121+
mutateCmd.Flags().StringToStringVarP(&labels, "label", "l", nil, "New labels to add")
123122
mutateCmd.Flags().StringVar(&entrypoint, "entrypoint", "", "New entrypoint to set")
124123
mutateCmd.Flags().StringVarP(&newRef, "tag", "t", "", "New tag to apply to mutated image. If not provided, push by digest to the original image repository.")
125124
return mutateCmd
126125
}
127126

128-
// splitKeyVals splits key value pairs which is in form hello=world
129-
func splitKeyVals(kvPairs []string) (map[string]string, error) {
130-
m := map[string]string{}
131-
for _, l := range kvPairs {
132-
parts := strings.SplitN(l, "=", 2)
133-
if len(parts) == 1 {
134-
return nil, fmt.Errorf("parsing label %q, not enough parts", l)
127+
// validateKeyVals ensures no values are empty, returns error if they are
128+
func validateKeyVals(kvPairs map[string]string) error {
129+
for label, value := range kvPairs {
130+
if value == "" {
131+
return fmt.Errorf("parsing label %q, value is empty", label)
135132
}
136-
m[parts[0]] = parts[1]
137133
}
138-
return m, nil
134+
return nil
139135
}

cmd/crane/doc/crane.md

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/crane/doc/crane_mutate.md

+6-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/v1/zz_deepcopy_generated.go

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)