From 0014f3ada29edaf5fb7f07c658821b0e80cffd1a Mon Sep 17 00:00:00 2001 From: Billy Zha Date: Sat, 11 May 2024 01:38:11 +0000 Subject: [PATCH] code clean Signed-off-by: Billy Zha --- cmd/oras/internal/option/format.go | 25 ++++++++++++++++--------- cmd/oras/root/discover.go | 2 +- cmd/oras/root/manifest/fetch.go | 2 +- 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/cmd/oras/internal/option/format.go b/cmd/oras/internal/option/format.go index 2c0405a0d..495ccd216 100644 --- a/cmd/oras/internal/option/format.go +++ b/cmd/oras/internal/option/format.go @@ -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. @@ -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") } @@ -98,10 +100,10 @@ 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) } @@ -109,10 +111,10 @@ func (opts *Format) parseFlag() error { } 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 } @@ -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 +} diff --git a/cmd/oras/root/discover.go b/cmd/oras/root/discover.go index f9abc7436..f29f75434 100644 --- a/cmd/oras/root/discover.go +++ b/cmd/oras/root/discover.go @@ -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"}, diff --git a/cmd/oras/root/manifest/fetch.go b/cmd/oras/root/manifest/fetch.go index f6c0ece4b..7ed5cb51f 100644 --- a/cmd/oras/root/manifest/fetch.go +++ b/cmd/oras/root/manifest/fetch.go @@ -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")