Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: command to list repositories in a remote registry #525

Merged
merged 18 commits into from
Sep 5, 2022
Merged
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
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()
junczhu marked this conversation as resolved.
Show resolved Hide resolved
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
})
}