Skip to content

Commit

Permalink
feat: command to list repositories in a remote registry (#525)
Browse files Browse the repository at this point in the history
Signed-off-by: Juncheng Zhu <[email protected]>
  • Loading branch information
junczhu authored Sep 5, 2022
1 parent 67dd9b0 commit f4a31ec
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 6 deletions.
1 change: 1 addition & 0 deletions cmd/oras/repository/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func Cmd() *cobra.Command {
}

cmd.AddCommand(
listCmd(),
showTagsCmd(),
)
return cmd
Expand Down
72 changes: 72 additions & 0 deletions cmd/oras/repository/list.go
Original file line number Diff line number Diff line change
@@ -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
})
}
9 changes: 3 additions & 6 deletions cmd/oras/repository/show-tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
})
}

0 comments on commit f4a31ec

Please sign in to comment.