Skip to content

Commit

Permalink
code clean
Browse files Browse the repository at this point in the history
Signed-off-by: Billy Zha <[email protected]>
  • Loading branch information
qweeah committed May 11, 2024
1 parent 53392e2 commit 0014f3a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
25 changes: 16 additions & 9 deletions cmd/oras/internal/option/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@ type FormatType struct {
type Format struct {
Type string
Template string
Input string
types []FormatType
// FormatFlag can be private once deprecated `--output` is removed from
// `oras discover`
FormatFlag string
types []FormatType
}

// ApplyFlag implements FlagProvider.ApplyFlag.
Expand All @@ -68,7 +70,7 @@ func (opts *Format) ApplyFlags(fs *pflag.FlagSet) {
}

// apply flags
fs.StringVar(&opts.Input, "format", opts.Input, usage)
fs.StringVar(&opts.FormatFlag, "format", opts.FormatFlag, usage)
fs.StringVar(&opts.Template, "template", "", "Template string used to format output")
}

Expand Down Expand Up @@ -98,21 +100,21 @@ func (opts *Format) Parse(_ *cobra.Command) error {
}

func (opts *Format) parseFlag() error {
opts.Type = opts.Input
opts.Type = opts.FormatFlag
if opts.Template != "" {
// template explicitly set
opts.Type = opts.Input
opts.Type = opts.FormatFlag
if opts.Type != FormatTypeGoTemplate {
return fmt.Errorf("--template must be used with --format %s", FormatTypeGoTemplate)
}
return nil
}

goTemplatePrefix := FormatTypeGoTemplate + "="
if strings.HasPrefix(opts.Input, goTemplatePrefix) {
if strings.HasPrefix(opts.FormatFlag, goTemplatePrefix) {
// add parameter to template
opts.Type = FormatTypeGoTemplate
opts.Template = opts.Input[len(goTemplatePrefix):]
opts.Template = opts.FormatFlag[len(goTemplatePrefix):]
}
return nil
}
Expand All @@ -125,11 +127,16 @@ func (opts *Format) SetTypes(types []FormatType) {
// SetTypesAndDefault resets the format options and default value.
// Caller should make sure that this function is used before applying flags.
func (opts *Format) SetTypesAndDefault(defaultType string, types []FormatType) {
opts.Input = defaultType
opts.FormatFlag = defaultType
opts.types = types
}

// FormatError generate the error message for an invalid type.
// FormatError generates the error message for an invalid type.
func (opts *Format) TypeError() error {
return fmt.Errorf("unsupported format type: %s", opts.Type)
}

// RawFormatFlag returns raw input of --format flag.
func (opts *Format) RawFormatFlag() string {
return opts.FormatFlag
}
2 changes: 1 addition & 1 deletion cmd/oras/root/discover.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ Example - Discover referrers of the manifest tagged 'v1' in an OCI image layout
}

cmd.Flags().StringVarP(&opts.artifactType, "artifact-type", "", "", "artifact type")
cmd.Flags().StringVarP(&opts.Format.Input, "output", "o", "tree", "[Deprecated] format in which to display referrers (table, json, or tree). tree format will also show indirect referrers")
cmd.Flags().StringVarP(&opts.Format.FormatFlag, "output", "o", "tree", "[Deprecated] format in which to display referrers (table, json, or tree). tree format will also show indirect referrers")
opts.SetTypesAndDefault(option.FormatTypeTree, []option.FormatType{
{Name: option.FormatTypeTree, Usage: "Get referrers recursively and print in tree format"},
{Name: option.FormatTypeTable, Usage: "Get direct referrers and output in table format"},
Expand Down
2 changes: 1 addition & 1 deletion cmd/oras/root/manifest/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ Example - Fetch raw manifest from an OCI layout archive file 'layout.tar':
Args: oerrors.CheckArgs(argument.Exactly(1), "the manifest to fetch"),
PreRunE: func(cmd *cobra.Command, args []string) error {
switch {
case opts.outputPath == "-" && opts.Input != "":
case opts.outputPath == "-" && opts.RawFormatFlag() != "":
return fmt.Errorf("`--output -` cannot be used with `--format %s` at the same time", opts.Template)
case opts.outputPath == "-" && opts.OutputDescriptor:
return fmt.Errorf("`--descriptor` cannot be used with `--output -` at the same time")
Expand Down

0 comments on commit 0014f3a

Please sign in to comment.