diff --git a/cmd/oras/repository/cmd.go b/cmd/oras/repository/cmd.go index 1bc86506b..96939d1af 100644 --- a/cmd/oras/repository/cmd.go +++ b/cmd/oras/repository/cmd.go @@ -24,6 +24,7 @@ func Cmd() *cobra.Command { } cmd.AddCommand( + listCmd(), showTagsCmd(), ) return cmd diff --git a/cmd/oras/repository/list.go b/cmd/oras/repository/list.go new file mode 100644 index 000000000..7dc32190b --- /dev/null +++ b/cmd/oras/repository/list.go @@ -0,0 +1,72 @@ +/* +Copyright The ORAS Authors. +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package repository + +import ( + "fmt" + + "github.com/spf13/cobra" + "oras.land/oras/cmd/oras/internal/option" +) + +type repositoryOptions struct { + option.Remote + option.Common + hostname string + last string +} + +func listCmd() *cobra.Command { + var opts repositoryOptions + cmd := &cobra.Command{ + Use: "list [flags] REGISTRY", + Short: "[Preview] List the repositories under the registry", + Long: `[Preview] List the repositories under the registry + +** This command is in preview and under development. ** + +Example - List the repositories under the registry: + oras repository list localhost:5000 + +Example - List the repositories under the registry that include values lexically after last: + oras repository list localhost:5000 --last "last_repo" +`, + Args: cobra.ExactArgs(1), + Aliases: []string{"ls"}, + RunE: func(cmd *cobra.Command, args []string) error { + opts.hostname = args[0] + return listRepository(opts) + }, + } + + cmd.Flags().StringVar(&opts.last, "last", "", "start after the repository specified by `last`") + option.ApplyFlags(&opts, cmd.Flags()) + return cmd +} + +func listRepository(opts repositoryOptions) error { + ctx, _ := opts.SetLoggerLevel() + reg, err := opts.Remote.NewRegistry(opts.hostname, opts.Common) + if err != nil { + return err + } + return reg.Repositories(ctx, opts.last, func(repos []string) error { + for _, repo := range repos { + fmt.Println(repo) + } + return nil + }) +} diff --git a/cmd/oras/repository/show-tags.go b/cmd/oras/repository/show-tags.go index f0788fa2a..1300e27d4 100644 --- a/cmd/oras/repository/show-tags.go +++ b/cmd/oras/repository/show-tags.go @@ -32,7 +32,7 @@ type showTagsOptions struct { func showTagsCmd() *cobra.Command { var opts showTagsOptions cmd := &cobra.Command{ - Use: "show-tags REPO [flags]", + Use: "show-tags [flags] REPOSITORY", Short: "[Preview] Show tags of the target repository", Long: `[Preview] Show tags of the target repository @@ -61,13 +61,10 @@ func showTags(opts showTagsOptions) error { if err != nil { return err } - if showTagErr := repo.Tags(ctx, opts.last, func(tags []string) error { + return repo.Tags(ctx, opts.last, func(tags []string) error { for _, tag := range tags { fmt.Println(tag) } return nil - }); showTagErr != nil { - return showTagErr - } - return nil + }) }