diff --git a/commands/command_option.go b/commands/command_option.go index 66ba75698..9c4056f42 100644 --- a/commands/command_option.go +++ b/commands/command_option.go @@ -52,3 +52,12 @@ func overrideCmdNS(ns string) cmdOption { c.overrideNS = ns } } + +// WithOverrideNS is a cmdOption that sets the overrideNS for the command. +// This should be used when the overrideNS needs to be set before the command +// is fully built, such as when flags need to be registered with the correct namespace. +func WithOverrideNS(ns string) cmdOption { + return func(c *Command) { + c.overrideNS = ns + } +} diff --git a/commands/registry.go b/commands/registry.go index 79db9a8d2..737d03069 100644 --- a/commands/registry.go +++ b/commands/registry.go @@ -163,10 +163,9 @@ func Repository() *Command { cmd, RunListRepositories, "list", "List repositories for a container registry", listRepositoriesDesc, - Writer, aliasOpt("ls"), displayerType(&displayers.Repository{}), + Writer, aliasOpt("ls"), WithOverrideNS(overrideNS), displayerType(&displayers.Repository{}), hiddenCmd(), ) - cmdListRepositories.overrideNS = overrideNS addRegistryFlag(cmdListRepositories) cmdListRepositories.Example = `The following example lists repositories in a registry named ` + "`" + `example-registry` + "`" + ` and uses the ` + "`" + `--format` + "`" + ` flag to return only the name and update time of each repository: doctl registry repository list --format Name,UpdatedAt` @@ -181,9 +180,8 @@ func Repository() *Command { cmd, RunListRepositoriesV2, "list-v2", "List repositories for a container registry", listRepositoriesV2Desc, - Writer, aliasOpt("ls2"), displayerType(&displayers.Repository{}), + Writer, aliasOpt("ls2"), WithOverrideNS(overrideNS), displayerType(&displayers.Repository{}), ) - cmdListRepositoriesV2.overrideNS = overrideNS addRegistryFlag(cmdListRepositoriesV2) cmdListRepositoriesV2.Example = `The following example lists repositories in a registry named ` + "`" + `example-registry` + "`" + ` and uses the ` + "`" + `--format` + "`" + ` flag to return only the name and update time of each repository: doctl registry repository list-v2 --format Name,UpdatedAt` @@ -197,9 +195,8 @@ func Repository() *Command { cmd, RunListRepositoryTags, "list-tags ", "List tags for a repository in a container registry", listRepositoryTagsDesc, - Writer, aliasOpt("lt"), displayerType(&displayers.RepositoryTag{}), + Writer, aliasOpt("lt"), WithOverrideNS(overrideNS), displayerType(&displayers.RepositoryTag{}), ) - cmdListRepositoryTags.overrideNS = overrideNS addRegistryFlag(cmdListRepositoryTags) cmdListRepositoryTags.Example = `The following example lists tags in a repository named ` + "`" + `example-repository` + "`" + ` in a registry named ` + "`" + `example-registry` + "`" + `. The command also uses the ` + "`" + `--format` + "`" + ` flag to return only the tag name and manifest digest for each tag: doctl registry repository list-tags example-repository --format Tag,ManifestDigest` @@ -230,9 +227,8 @@ func Repository() *Command { cmd, RunListRepositoryManifests, "list-manifests ", "List manifests for a repository in a container registry", listRepositoryManifests, - Writer, aliasOpt("lm"), displayerType(&displayers.RepositoryManifest{}), + Writer, aliasOpt("lm"), WithOverrideNS(overrideNS), displayerType(&displayers.RepositoryManifest{}), ) - cmdListRepositoryManifests.overrideNS = overrideNS addRegistryFlag(cmdListRepositoryManifests) cmdListRepositoryManifests.Example = `The following example lists manifests in a repository named ` + "`" + `example-repository` + "`" + `. The command also uses the ` + "`" + `--format` + "`" + ` flag to return only the digest and update time for each manifest: doctl registry repository list-manifests example-repository --format Digest,UpdatedAt` @@ -281,9 +277,8 @@ func GarbageCollection() *Command { runStartGarbageCollectionDesc, Writer, aliasOpt("s"), - displayerType(&displayers.GarbageCollection{}), + WithOverrideNS(overrideNS), displayerType(&displayers.GarbageCollection{}), ) - cmdStartGarbageCollection.overrideNS = overrideNS AddBoolFlag(cmdStartGarbageCollection, doctl.ArgGCIncludeUntaggedManifests, "", false, "Include untagged manifests in garbage collection.") AddBoolFlag(cmdStartGarbageCollection, doctl.ArgGCExcludeUnreferencedBlobs, "", false, @@ -310,9 +305,8 @@ func GarbageCollection() *Command { runGetGarbageCollectionDesc, Writer, aliasOpt("ga", "g"), - displayerType(&displayers.GarbageCollection{}), + WithOverrideNS(overrideNS), displayerType(&displayers.GarbageCollection{}), ) - cmdGetGarbageCollection.overrideNS = overrideNS cmdGetGarbageCollection.Example = `The following example retrieves the currently-active garbage collection for a registry: doctl registry garbage-collection get-active The following example retrieves the currently-active garbage collection for a registry named ` + "`" + `example-registry` + "`" + `: doctl registry garbage-collection get-active example-registry` @@ -326,9 +320,8 @@ The following example retrieves the currently-active garbage collection for a re runListGarbageCollectionsDesc, Writer, aliasOpt("ls", "l"), - displayerType(&displayers.GarbageCollection{}), + WithOverrideNS(overrideNS), displayerType(&displayers.GarbageCollection{}), ) - cmdListGarbageCollections.overrideNS = overrideNS cmdListGarbageCollections.Example = `The following example retrieves a list of past garbage collections for a registry: doctl registry garbage-collection list The following example retrieves a list of past garbage collections for a registry named ` + "`" + `example-registry` + "`" + `: doctl registry garbage-collection list example-registry` @@ -1527,10 +1520,9 @@ func RegistriesRepository() *Command { cmd, RunRegistriesListRepositories, "list ", "List repositories for a container registry", listRepositoriesDesc, - Writer, aliasOpt("ls"), displayerType(&displayers.Repository{}), + Writer, aliasOpt("ls"), WithOverrideNS(overrideNS), displayerType(&displayers.Repository{}), hiddenCmd(), ) - cmdListRepositories.overrideNS = overrideNS cmdListRepositories.Example = `The following example lists repositories in a registry named ` + "`" + `example-registry` + "`" + ` and uses the ` + "`" + `--format` + "`" + ` flag to return only the name and update time of each repository: doctl registries repository list example-registry --format Name,UpdatedAt` listRepositoriesV2Desc := `Retrieves information about repositories in a registry, including: @@ -1544,9 +1536,8 @@ func RegistriesRepository() *Command { cmd, RunRegistriesListRepositoriesV2, "list-v2 ", "List repositories for a container registry", listRepositoriesV2Desc, - Writer, aliasOpt("ls2"), displayerType(&displayers.Repository{}), + Writer, aliasOpt("ls2"), WithOverrideNS(overrideNS), displayerType(&displayers.Repository{}), ) - cmdListRepositoriesV2.overrideNS = overrideNS cmdListRepositoriesV2.Example = `The following example lists repositories in a registry named ` + "`" + `example-registry` + "`" + ` and uses the ` + "`" + `--format` + "`" + ` flag to return only the name and update time of each repository: doctl registries repository list-v2 example-registry --format Name,UpdatedAt` listRepositoryTagsDesc := `Retrieves information about tags in a repository, including: @@ -1559,9 +1550,8 @@ func RegistriesRepository() *Command { cmd, RunRegistriesListRepositoryTags, "list-tags ", "List tags for a repository in a container registry", listRepositoryTagsDesc, - Writer, aliasOpt("lt"), displayerType(&displayers.RepositoryTag{}), + Writer, aliasOpt("lt"), WithOverrideNS(overrideNS), displayerType(&displayers.RepositoryTag{}), ) - cmdListRepositoryTags.overrideNS = overrideNS cmdListRepositoryTags.Example = `The following example lists tags in a repository named ` + "`" + `example-repository` + "`" + ` in a registry named ` + "`" + `example-registry` + "`" + `. The command also uses the ` + "`" + `--format` + "`" + ` flag to return only the tag name and manifest digest for each tag: doctl registries repository list-tags example-registry example-repository --format Tag,ManifestDigest` deleteTagDesc := "Permanently deletes one or more repository tags." @@ -1590,9 +1580,8 @@ func RegistriesRepository() *Command { cmd, RunRegistriesListRepositoryManifests, "list-manifests ", "List manifests for a repository in a container registry", listRepositoryManifests, - Writer, aliasOpt("lm"), displayerType(&displayers.RepositoryManifest{}), + Writer, aliasOpt("lm"), WithOverrideNS(overrideNS), displayerType(&displayers.RepositoryManifest{}), ) - cmdListRepositoryManifests.overrideNS = overrideNS cmdListRepositoryManifests.Example = `The following example lists manifests in a repository named ` + "`" + `example-repository` + "`" + ` in a registry named ` + "`" + `example-registry` + "`" + `. The command also uses the ` + "`" + `--format` + "`" + ` flag to return only the digest and update time for each manifest: doctl registries repository list-manifests example-registry example-repository --format Digest,UpdatedAt` deleteManifestDesc := "Permanently deletes one or more repository manifests by digest." @@ -1634,9 +1623,8 @@ func RegistriesGarbageCollection() *Command { runStartGarbageCollectionDesc, Writer, aliasOpt("s"), - displayerType(&displayers.GarbageCollection{}), + WithOverrideNS(overrideNS), displayerType(&displayers.GarbageCollection{}), ) - cmdStartGarbageCollection.overrideNS = overrideNS AddBoolFlag(cmdStartGarbageCollection, doctl.ArgGCIncludeUntaggedManifests, "", false, "Include untagged manifests in garbage collection.") AddBoolFlag(cmdStartGarbageCollection, doctl.ArgGCExcludeUnreferencedBlobs, "", false, @@ -1663,9 +1651,8 @@ func RegistriesGarbageCollection() *Command { runGetGarbageCollectionDesc, Writer, aliasOpt("ga", "g"), - displayerType(&displayers.GarbageCollection{}), + WithOverrideNS(overrideNS), displayerType(&displayers.GarbageCollection{}), ) - cmdGetGarbageCollection.overrideNS = overrideNS cmdGetGarbageCollection.Example = `The following example retrieves the currently-active garbage collection for a registry named ` + "`" + `example-registry` + "`" + `: doctl registries garbage-collection get-active example-registry` runListGarbageCollectionsDesc := "Retrieves a list of past garbage collections for a registry. Information about each garbage collection includes:" + gcInfoIncluded @@ -1677,9 +1664,8 @@ func RegistriesGarbageCollection() *Command { runListGarbageCollectionsDesc, Writer, aliasOpt("ls", "l"), - displayerType(&displayers.GarbageCollection{}), + WithOverrideNS(overrideNS), displayerType(&displayers.GarbageCollection{}), ) - cmdListGarbageCollections.overrideNS = overrideNS cmdListGarbageCollections.Example = `The following example retrieves a list of past garbage collections for a registry named ` + "`" + `example-registry` + "`" + `: doctl registries garbage-collection list example-registry` runCancelGarbageCollectionDesc := "Cancels the currently-active garbage collection for a container registry." diff --git a/integration/registry_repo_tag_list_test.go b/integration/registry_repo_tag_list_test.go index f369aae98..5f4fc51b9 100644 --- a/integration/registry_repo_tag_list_test.go +++ b/integration/registry_repo_tag_list_test.go @@ -77,6 +77,42 @@ var _ = suite("registry/repository/list-tags", func(t *testing.T, when spec.G, i expect.Equal(strings.TrimSpace(repositoryTagListOutput), strings.TrimSpace(string(output))) }) + + it("returns list of repositories in registry with format flag", func() { + cmd := exec.Command(builtBinaryPath, + "-t", "some-magic-token", + "-u", server.URL, + "registry", + "repository", + "list-tags", + "my-repo", + "--format", "Tag", + ) + + output, err := cmd.CombinedOutput() + expect.NoError(err) + + expectedOutput := "Tag\nmy-tag\n" + expect.Equal(expectedOutput, string(output)) + }) + + it("returns list of repositories in registry with no-header flag", func() { + cmd := exec.Command(builtBinaryPath, + "-t", "some-magic-token", + "-u", server.URL, + "registry", + "repository", + "list-tags", + "my-repo", + "--no-header", + ) + + output, err := cmd.CombinedOutput() + expect.NoError(err) + + expectedOutput := "my-tag 1.00 kB 2020-04-01 00:00:00 +0000 UTC sha256:e692418e4cbaf90ca69d05a66403747baa33ee08806650b51fab815ad7fc331f\n" + expect.Equal(expectedOutput, string(output)) + }) }) var (