Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions cli/command/manifest/annotate.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ type annotateOptions struct {
arch string
osFeatures []string
osVersion string

// variantChanged and osVersionChanged are set if the flags were passed,
// so that an empty value can be used to unset them.
variantChanged bool
osVersionChanged bool
}

// manifestStoreProvider is used in tests to provide a dummy store.
Expand Down Expand Up @@ -84,6 +89,8 @@ func newAnnotateCommand(dockerCLI command.Cli) *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
opts.target = args[0]
opts.image = args[1]
opts.variantChanged = cmd.Flags().Changed("variant")
opts.osVersionChanged = cmd.Flags().Changed("os-version")
return runManifestAnnotate(dockerCLI, opts)
},
DisableFlagsInUseLine: true,
Expand Down Expand Up @@ -132,10 +139,10 @@ func runManifestAnnotate(dockerCLI command.Cli, opts annotateOptions) error {
for _, osFeature := range opts.osFeatures {
imageManifest.Descriptor.Platform.OSFeatures = appendIfUnique(imageManifest.Descriptor.Platform.OSFeatures, osFeature)
}
if opts.variant != "" {
if opts.variantChanged {
imageManifest.Descriptor.Platform.Variant = opts.variant
}
if opts.osVersion != "" {
if opts.osVersionChanged {
imageManifest.Descriptor.Platform.OSVersion = opts.osVersion
}

Expand Down
40 changes: 40 additions & 0 deletions cli/command/manifest/annotate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,43 @@ func TestManifestAnnotate(t *testing.T) {
expected := golden.Get(t, "inspect-annotate.golden")
assert.Check(t, is.Equal(string(expected), actual.String()))
}

func TestManifestAnnotateUnsetFields(t *testing.T) {
manifestStore := store.NewStore(t.TempDir())

cli := test.NewFakeCli(nil)
cli.SetManifestStore(manifestStore)
listRef := ref(t, "list:v1")
namedRef := ref(t, "alpine:3.0")
err := manifestStore.Save(listRef, namedRef, fullImageManifest(t, namedRef))
assert.NilError(t, err)

annotate := func(args ...string) {
t.Helper()
cmd := newAnnotateCommand(cli)
cmd.SetArgs(append([]string{"example.com/list:v1", "example.com/alpine:3.0"}, args...))
cmd.SetOut(io.Discard)
cmd.SetErr(io.Discard)
assert.NilError(t, cmd.Execute())
}

annotate("--os", "linux", "--arch", "arm", "--variant", "v7", "--os-version", "1")
imageManifest, err := manifestStore.Get(listRef, namedRef)
assert.NilError(t, err)
assert.Check(t, is.Equal(imageManifest.Descriptor.Platform.Variant, "v7"))
assert.Check(t, is.Equal(imageManifest.Descriptor.Platform.OSVersion, "1"))

// Not passing the flags should preserve the existing values.
annotate()
imageManifest, err = manifestStore.Get(listRef, namedRef)
assert.NilError(t, err)
assert.Check(t, is.Equal(imageManifest.Descriptor.Platform.Variant, "v7"))
assert.Check(t, is.Equal(imageManifest.Descriptor.Platform.OSVersion, "1"))

// Passing an empty value should unset them.
annotate("--variant", "", "--os-version", "")
imageManifest, err = manifestStore.Get(listRef, namedRef)
assert.NilError(t, err)
assert.Check(t, is.Equal(imageManifest.Descriptor.Platform.Variant, ""))
assert.Check(t, is.Equal(imageManifest.Descriptor.Platform.OSVersion, ""))
}
Loading