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: suppport repo ls sub-namespace #768

Merged
merged 3 commits into from
Feb 3, 2023
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
36 changes: 32 additions & 4 deletions cmd/oras/repository/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,19 @@ package repository

import (
"fmt"
"strings"

"github.com/spf13/cobra"
"oras.land/oras-go/v2/registry"
"oras.land/oras/cmd/oras/internal/option"
)

type repositoryOptions struct {
option.Remote
option.Common
hostname string
last string
hostname string
namespace string
wangxiaoxuan273 marked this conversation as resolved.
Show resolved Hide resolved
last string
}

func listCmd() *cobra.Command {
Expand All @@ -41,6 +44,9 @@ func listCmd() *cobra.Command {
Example - List the repositories under the registry:
oras repo ls localhost:5000

Example - List the repositories under a namespace in the registry:
oras repo ls localhost:5000/example-namespace

Example - List the repositories under the registry that include values lexically after last:
oras repo ls --last "last_repo" localhost:5000
`,
Expand All @@ -50,7 +56,9 @@ Example - List the repositories under the registry that include values lexically
return option.Parse(&opts)
},
RunE: func(cmd *cobra.Command, args []string) error {
opts.hostname = args[0]
if err := parseRepoPath(&opts, args[0]); err != nil {
return fmt.Errorf("could not parse repository path: %w", err)
}
return listRepository(opts)
},
}
Expand All @@ -60,6 +68,24 @@ Example - List the repositories under the registry that include values lexically
return cmd
}

func parseRepoPath(opts *repositoryOptions, arg string) error {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add unit tests for this function?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added unit tests.

path := strings.TrimSuffix(arg, "/")
if strings.Contains(path, "/") {
reference, err := registry.ParseReference(path)
if err != nil {
return err
}
if reference.Reference != "" {
return fmt.Errorf("tags or digests should not be provided")
}
opts.hostname = reference.Registry
opts.namespace = reference.Repository + "/"
} else {
opts.hostname = path
}
return nil
}

func listRepository(opts repositoryOptions) error {
ctx, _ := opts.SetLoggerLevel()
reg, err := opts.Remote.NewRegistry(opts.hostname, opts.Common)
Expand All @@ -68,7 +94,9 @@ func listRepository(opts repositoryOptions) error {
}
return reg.Repositories(ctx, opts.last, func(repos []string) error {
for _, repo := range repos {
fmt.Println(repo)
if subRepo, found := strings.CutPrefix(repo, opts.namespace); found {
fmt.Println(subRepo)
}
}
return nil
})
Expand Down
104 changes: 104 additions & 0 deletions cmd/oras/repository/ls_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
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 "testing"

func Test_parseRepoPath(t *testing.T) {
type args struct {
opts *repositoryOptions
arg string
}
var testOpts repositoryOptions
tests := []struct {
name string
args args
wantErr bool
wantHostname string
wantNamespace string
}{
{
name: "hostname only",
args: args{&testOpts, "testregistry.example.io"},
wantErr: false,
wantHostname: "testregistry.example.io",
wantNamespace: "",
},
{
name: "hostname with trailing slash",
args: args{&testOpts, "testregistry.example.io/"},
wantErr: false,
wantHostname: "testregistry.example.io",
wantNamespace: "",
},
{
name: "hostname and repo",
args: args{&testOpts, "testregistry.example.io/showcase"},
wantErr: false,
wantHostname: "testregistry.example.io",
wantNamespace: "showcase/",
},
{
name: "hostname and repo in a sub-namespace",
args: args{&testOpts, "testregistry.example.io/showcase/beta"},
wantErr: false,
wantHostname: "testregistry.example.io",
wantNamespace: "showcase/beta/",
},
{
name: "hostname and repo in a sub-namespace with trailing slash",
args: args{&testOpts, "testregistry.example.io/showcase/beta/"},
wantErr: false,
wantHostname: "testregistry.example.io",
wantNamespace: "showcase/beta/",
},
{
name: "error when a tag is provided",
args: args{&testOpts, "testregistry.example.io/showcase:latest"},
wantErr: true,
wantHostname: "",
wantNamespace: "",
},
{
name: "error when a digest is provided",
args: args{&testOpts, "testregistry.example.io/showcase:sha256:2e0e0fe1fb3edbcdddad941c90d2b51e25a6bcd593e82545441a216de7bfa834"},
wantErr: true,
wantHostname: "",
wantNamespace: "",
},
{
name: "error when a malformed path is provided",
args: args{&testOpts, "testregistry.example.io///showcase/"},
wantErr: true,
wantHostname: "",
wantNamespace: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := parseRepoPath(tt.args.opts, tt.args.arg)
if (err != nil) != tt.wantErr {
t.Errorf("parseRepoPath() error = %v, wantErr %v", err, tt.wantErr)
} else if testOpts.hostname != tt.wantHostname {
t.Errorf("got incorrect hostname = %v, want %v", testOpts.hostname, tt.wantHostname)
} else if testOpts.namespace != tt.wantNamespace {
t.Errorf("got incorrect hostname = %v, want %v", testOpts.namespace, tt.wantNamespace)
}
testOpts.hostname = ""
testOpts.namespace = ""
})
}
}
1 change: 1 addition & 0 deletions test/e2e/internal/utils/testdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const (
PreviewDesc = "** This command is in preview and under development. **"
ExampleDesc = "\nExample - "
Repo = "command/images"
Namespace = "command"
FoobarImageTag = "foobar"
FoobarConfigDesc = "{\"mediaType\":\"application/vnd.unknown.config.v1+json\",\"digest\":\"sha256:44136fa355b3678a1146ad16f7e8649e94fb4fc21fe77e8310c060f61caaff8a\",\"size\":2}"
MultiImageTag = "multi"
Expand Down
15 changes: 14 additions & 1 deletion test/e2e/suite/command/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ var _ = Describe("ORAS beginners:", func() {

It("should fail listing repositories if wrong registry provided", func() {
ORAS("repo", "ls").ExpectFailure().MatchErrKeyWords("Error:").Exec()
ORAS("repo", "ls", Reference(Host, Repo, "")).ExpectFailure().MatchErrKeyWords("Error:").Exec()
ORAS("repo", "ls", Reference(Host, Repo, "some-tag")).ExpectFailure().MatchErrKeyWords("Error:").Exec()
})
})
Expand All @@ -61,6 +60,20 @@ var _ = Describe("Common registry users:", func() {
It("should list repositories", func() {
ORAS("repository", "list", Host).MatchKeyWords(Repo).Exec()
})
It("should list repositories under provided namespace", func() {
ORAS("repo", "ls", Reference(Host, Namespace, "")).MatchKeyWords(Repo[len(Namespace)+1:]).Exec()
})

It("should not list repositories without a fully matched namespace", func() {
dstRepo := "command-draft/images"
ORAS("cp", Reference(Host, Repo, FoobarImageTag), Reference(Host, dstRepo, FoobarImageTag)).
WithDescription("prepare destination repo: " + dstRepo).
Exec()
ORAS("repo", "ls", Host).MatchKeyWords(Repo, dstRepo).Exec()
session := ORAS("repo", "ls", Reference(Host, Namespace, "")).MatchKeyWords(Repo[len(Namespace)+1:]).Exec()
Expect(session.Out).ShouldNot(gbytes.Say(dstRepo[len(Namespace)+1:]))
})

It("should list repositories via short command", func() {
ORAS("repo", "ls", Host).MatchKeyWords(Repo).Exec()
})
Expand Down