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

cmd/cp: Fix --from-oci-layout when using full reference #1507

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
19 changes: 10 additions & 9 deletions cmd/oras/internal/option/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ import (
"oras.land/oras-go/v2/registry/remote/auth"
"oras.land/oras-go/v2/registry/remote/errcode"
oerrors "oras.land/oras/cmd/oras/internal/errors"
"oras.land/oras/cmd/oras/internal/fileref"
)

const (
Expand Down Expand Up @@ -123,20 +122,22 @@ func (opts *Target) Parse(cmd *cobra.Command) error {
// parseOCILayoutReference parses the raw in format of <path>[:<tag>|@<digest>]
func (opts *Target) parseOCILayoutReference() error {
raw := opts.RawReference
var path string
path := raw
var ref string
if idx := strings.LastIndex(raw, "@"); idx != -1 {
// `digest` found
path = raw[:idx]
ref = raw[idx+1:]
} else {
// find `tag`
var err error
path, ref, err = fileref.Parse(raw, "")
if err != nil {
return errors.Join(err, errdef.ErrInvalidReference)
}
} else if idx := strings.Index(raw, ":"); idx != -1 {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
} else if idx := strings.Index(raw, ":"); idx != -1 {
} else if idx := strings.LastIndex(raw, ":"); idx != -1 {

// `tag` found
path = raw[:idx]
ref = raw[idx+1:]
}

if path == "" {
return fmt.Errorf("found empty file path in %q", raw)
}

opts.Path = path
opts.Reference = ref
return nil
Expand Down
3 changes: 2 additions & 1 deletion cmd/oras/internal/option/target_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,12 @@ func Test_parseOCILayoutReference(t *testing.T) {
}{
{"Empty input", "", "", "", true},
{"Empty path and tag", ":", "", "", true},
{"Empty path and digest", "@", "", "", false},
{"Empty path and digest", "@", "", "", true},
{"Empty digest", "path@", "path", "", false},
{"Empty tag", "path:", "path", "", false},
{"path and digest", "path@digest", "path", "digest", false},
{"path and tag", "path:tag", "path", "tag", false},
{"path with full reference", "path:repo:tag", "path", "repo:tag", false},
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't the path be path:repo?

Copy link
Contributor

Choose a reason for hiding this comment

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

path:repo fails, but the included content passes

}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
Loading