From ae6b4f231fdedaf928b4482ebbc23a3740db852d Mon Sep 17 00:00:00 2001 From: Billy Zha Date: Thu, 9 May 2024 06:35:29 +0000 Subject: [PATCH] code clean Signed-off-by: Billy Zha --- cmd/oras/internal/display/handler.go | 24 ++++++++++++------------ cmd/oras/internal/option/format.go | 18 +++++++++--------- cmd/oras/root/discover.go | 10 +++++----- cmd/oras/root/manifest/fetch.go | 4 ++-- 4 files changed, 28 insertions(+), 28 deletions(-) diff --git a/cmd/oras/internal/display/handler.go b/cmd/oras/internal/display/handler.go index cd40aa1d5..75d9db473 100644 --- a/cmd/oras/internal/display/handler.go +++ b/cmd/oras/internal/display/handler.go @@ -48,9 +48,9 @@ func NewPushHandler(out io.Writer, format option.Format, tty *os.File, verbose b switch format.Type { case "": metadataHandler = text.NewPushHandler(out) - case option.TypeJSON: + case option.FormatTypeJSON: metadataHandler = json.NewPushHandler(out) - case option.TypeGoTemplate: + case option.FormatTypeGoTemplate: metadataHandler = template.NewPushHandler(out, format.Template) default: return nil, nil, format.TypeError() @@ -73,9 +73,9 @@ func NewAttachHandler(out io.Writer, format option.Format, tty *os.File, verbose switch format.Type { case "": metadataHandler = text.NewAttachHandler(out) - case option.TypeJSON: + case option.FormatTypeJSON: metadataHandler = json.NewAttachHandler(out) - case option.TypeGoTemplate: + case option.FormatTypeGoTemplate: metadataHandler = template.NewAttachHandler(out, format.Template) default: return nil, nil, format.TypeError() @@ -98,9 +98,9 @@ func NewPullHandler(out io.Writer, format option.Format, path string, tty *os.Fi switch format.Type { case "": metadataHandler = text.NewPullHandler(out) - case option.TypeJSON: + case option.FormatTypeJSON: metadataHandler = json.NewPullHandler(out, path) - case option.TypeGoTemplate: + case option.FormatTypeGoTemplate: metadataHandler = template.NewPullHandler(out, path, format.Template) default: return nil, nil, format.TypeError() @@ -112,13 +112,13 @@ func NewPullHandler(out io.Writer, format option.Format, path string, tty *os.Fi func NewDiscoverHandler(out io.Writer, format option.Format, path string, rawReference string, desc ocispec.Descriptor, verbose bool) (metadata.DiscoverHandler, error) { var handler metadata.DiscoverHandler switch format.Type { - case option.TypeTree, "": + case option.FormatTypeTree, "": handler = tree.NewDiscoverHandler(out, path, desc, verbose) - case option.TypeTable: + case option.FormatTypeTable: handler = table.NewDiscoverHandler(out, rawReference, desc, verbose) - case option.TypeJSON: + case option.FormatTypeJSON: handler = json.NewDiscoverHandler(out, desc, path) - case option.TypeGoTemplate: + case option.FormatTypeGoTemplate: handler = template.NewDiscoverHandler(out, desc, path, format.Template) default: return nil, format.TypeError() @@ -139,13 +139,13 @@ func NewManifestFetchHandler(out io.Writer, format option.Format, outputDescript } else { metadataHandler = metadata.NewDiscardHandler() } - case option.TypeJSON: + case option.FormatTypeJSON: // json metadataHandler = json.NewManifestFetchHandler(out) if outputPath == "" { contentHandler = content.NewDiscardHandler() } - case option.TypeGoTemplate: + case option.FormatTypeGoTemplate: // go template metadataHandler = template.NewManifestFetchHandler(out, format.Template) if outputPath == "" { diff --git a/cmd/oras/internal/option/format.go b/cmd/oras/internal/option/format.go index 7f44184c0..e719e7abf 100644 --- a/cmd/oras/internal/option/format.go +++ b/cmd/oras/internal/option/format.go @@ -26,10 +26,10 @@ import ( const ( // format types - TypeJSON = "json" - TypeTree = "tree" - TypeTable = "table" - TypeGoTemplate = "go-template" + FormatTypeJSON = "json" + FormatTypeTree = "tree" + FormatTypeTable = "table" + FormatTypeGoTemplate = "go-template" ) // FormatType represents a custom type for formatting. @@ -38,7 +38,7 @@ type FormatType struct { Usage string } -// Format is a flag to format metadata into output. +// Format contains input and parsed options to format output. type Format struct { Type string Template string @@ -51,8 +51,8 @@ func (opts *Format) ApplyFlags(fs *pflag.FlagSet) { usage := "[Experimental] Format output using a custom template:" if len(opts.types) == 0 { opts.types = []FormatType{ - {Name: TypeJSON, Usage: "Print in JSON format"}, - {Name: TypeGoTemplate, Usage: "Print output using the given Go template"}, + {Name: FormatTypeJSON, Usage: "Print in JSON format"}, + {Name: FormatTypeGoTemplate, Usage: "Print output using the given Go template"}, } } @@ -78,8 +78,8 @@ func (opts *Format) Parse(_ *cobra.Command) error { return err } - if opts.Template != "" && opts.Type != TypeGoTemplate { - return fmt.Errorf("--template must be used with --format %s", TypeGoTemplate) + if opts.Template != "" && opts.Type != FormatTypeGoTemplate { + return fmt.Errorf("--template must be used with --format %s", FormatTypeGoTemplate) } if opts.Type == "" { // flag not specified diff --git a/cmd/oras/root/discover.go b/cmd/oras/root/discover.go index e7b305131..62640a521 100644 --- a/cmd/oras/root/discover.go +++ b/cmd/oras/root/discover.go @@ -100,11 +100,11 @@ 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") - opts.SetTypesAndDefault(option.TypeTree, []option.FormatType{ - {Name: option.TypeTree, Usage: "Get referrers recursively and print in tree format"}, - {Name: option.TypeTable, Usage: "Get direct referrers and output in table format"}, - {Name: option.TypeJSON, Usage: "Get direct referrers and output in JSON format"}, - {Name: option.TypeGoTemplate, Usage: "Print direct referrers using the given Go template"}, + 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"}, + {Name: option.FormatTypeJSON, Usage: "Get direct referrers and output in JSON format"}, + {Name: option.FormatTypeGoTemplate, Usage: "Print direct referrers using the given Go template"}, }) opts.EnableDistributionSpecFlag() option.ApplyFlags(&opts, cmd.Flags()) diff --git a/cmd/oras/root/manifest/fetch.go b/cmd/oras/root/manifest/fetch.go index 22be86196..f6c0ece4b 100644 --- a/cmd/oras/root/manifest/fetch.go +++ b/cmd/oras/root/manifest/fetch.go @@ -99,8 +99,8 @@ Example - Fetch raw manifest from an OCI layout archive file 'layout.tar': cmd.Flags().StringSliceVarP(&opts.mediaTypes, "media-type", "", nil, "accepted media types") cmd.Flags().StringVarP(&opts.outputPath, "output", "o", "", "file `path` to write the fetched manifest to, use - for stdout") opts.SetTypes([]option.FormatType{ - {Name: option.TypeJSON, Usage: "Print in prettified JSON format"}, - {Name: option.TypeGoTemplate, Usage: "Print using the given Go template"}, + {Name: option.FormatTypeJSON, Usage: "Print in prettified JSON format"}, + {Name: option.FormatTypeGoTemplate, Usage: "Print using the given Go template"}, }) option.ApplyFlags(&opts, cmd.Flags()) return oerrors.Command(cmd, &opts.Target)