Skip to content

Commit

Permalink
resolved comments
Browse files Browse the repository at this point in the history
Signed-off-by: Xiaoxuan Wang <[email protected]>
  • Loading branch information
wangxiaoxuan273 committed Mar 28, 2024
1 parent 40e4e75 commit 87d6805
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
15 changes: 10 additions & 5 deletions cmd/oras/internal/option/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,11 @@ func (opts *Target) ApplyFlagsWithPrefix(fs *pflag.FlagSet, prefix, description
func (opts *Target) Parse() error {
switch {
case opts.IsOCILayout:
var err error
opts.Type = TargetTypeOCILayout
if len(opts.headerFlags) != 0 {
return errors.New("custom header flags cannot be used on an OCI image layout target")
}
opts.Path, opts.Reference, err = opts.parseOCILayoutReference()
return err
return opts.parseOCILayoutReference()
default:
opts.Type = TargetTypeRemote
if _, err := registry.ParseReference(opts.RawReference); err != nil {
Expand All @@ -118,8 +116,12 @@ func (opts *Target) Parse() error {
}

// parseOCILayoutReference parses the raw in format of <path>[:<tag>|@<digest>]
func (opts *Target) parseOCILayoutReference() (path string, ref string, err error) {
func (opts *Target) parseOCILayoutReference() error {
raw := opts.RawReference
var path string
var ref string
var err error

if idx := strings.LastIndex(raw, "@"); idx != -1 {
// `digest` found
path = raw[:idx]
Expand All @@ -129,9 +131,12 @@ func (opts *Target) parseOCILayoutReference() (path string, ref string, err erro
path, ref, err = fileref.Parse(raw, "")
if err != nil {
err = errors.Join(err, errdef.ErrInvalidReference)
return err
}
}
return
opts.Path = path
opts.Reference = ref
return nil
}

func (opts *Target) newOCIStore() (*oci.Store, error) {
Expand Down
10 changes: 5 additions & 5 deletions cmd/oras/internal/option/target_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,16 @@ func Test_parseOCILayoutReference(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
opts.RawReference = tt.raw
got, got1, err := opts.parseOCILayoutReference()
err := opts.parseOCILayoutReference()
if (err != nil) != tt.wantErr {
t.Errorf("parseOCILayoutReference() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("parseOCILayoutReference() got = %v, want %v", got, tt.want)
if opts.Path != tt.want {
t.Errorf("parseOCILayoutReference() got = %v, want %v", opts.Path, tt.want)
}
if got1 != tt.want1 {
t.Errorf("parseOCILayoutReference() got1 = %v, want %v", got1, tt.want1)
if opts.Reference != tt.want1 {
t.Errorf("parseOCILayoutReference() got1 = %v, want %v", opts.Reference, tt.want1)
}
})
}
Expand Down

0 comments on commit 87d6805

Please sign in to comment.