Skip to content

Commit

Permalink
bug fix
Browse files Browse the repository at this point in the history
Signed-off-by: Billy Zha <[email protected]>
  • Loading branch information
qweeah committed May 9, 2024
1 parent 10510b7 commit 69afad6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 19 deletions.
24 changes: 12 additions & 12 deletions cmd/oras/internal/option/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,6 @@ func (opts *Format) Parse(_ *cobra.Command) error {
return err
}

if opts.Template != "" && opts.Type != FormatTypeGoTemplate {
return fmt.Errorf("--template must be used with --format %s", FormatTypeGoTemplate)
}
if opts.Type == "" {
// flag not specified
return nil
Expand All @@ -89,12 +86,13 @@ func (opts *Format) Parse(_ *cobra.Command) error {
var optionalTypes []string
for _, option := range opts.types {
if opts.Type == option.Name {
// type validation passed
return nil
}
optionalTypes = append(optionalTypes, option.Name)
}
return &oerrors.Error{
Err: fmt.Errorf("invalid format type: %s", opts.Type),
Err: fmt.Errorf("invalid format type: %q", opts.Type),
Recommendation: fmt.Sprintf("supported types: %s", strings.Join(optionalTypes, ", ")),
}
}
Expand All @@ -103,18 +101,20 @@ func (opts *Format) parseFlag() error {
if opts.Template != "" {
// template explicitly set
opts.Type = opts.Input
if opts.Type != FormatTypeGoTemplate {
return fmt.Errorf("--template must be used with --format %s", FormatTypeGoTemplate)
}
return nil
}
index := strings.Index(opts.Input, "=")
if index == -1 {
// no proper template found in the type flag

goTemplatePrefix := FormatTypeGoTemplate + "="
if strings.HasPrefix(opts.Input, goTemplatePrefix) {
// add parameter to template
opts.Type = FormatTypeGoTemplate
opts.Template = opts.Input[len(goTemplatePrefix):]
} else {
opts.Type = opts.Input
return nil
} else if index == len(opts.Input)-1 || index == 0 {
return fmt.Errorf("invalid format flag: %s", opts.Input)
}
opts.Type = opts.Input[:index]
opts.Template = opts.Input[index+1:]
return nil
}

Expand Down
10 changes: 3 additions & 7 deletions test/e2e/suite/command/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,13 @@ var _ = Describe("ORAS beginners:", func() {
ORAS("pull", ref, "--template", "{{.}}").
WithWorkDir(tempDir).
ExpectFailure().
MatchErrKeyWords("--template must be used with --format go-template").
Exec()
})

It("should fail if template format is invalid", func() {
It("should fail if template format is invalid", Focus, func() {
tempDir := PrepareTempFiles()
invalidPrompt := "invalid format flag"
invalidPrompt := "invalid format type"
ref := RegistryRef(ZOTHost, ArtifactRepo, foobar.Tag)
ORAS("pull", ref, "--format", "json", "--format", "=").
WithWorkDir(tempDir).
Expand All @@ -110,11 +111,6 @@ var _ = Describe("ORAS beginners:", func() {
ExpectFailure().
MatchErrKeyWords(invalidPrompt).
Exec()
ORAS("pull", ref, "--format", "json", "--format", "go-template=").
WithWorkDir(tempDir).
ExpectFailure().
MatchErrKeyWords(invalidPrompt).
Exec()
})

It("should fail and show detailed error description if no argument provided", func() {
Expand Down

0 comments on commit 69afad6

Please sign in to comment.