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 9, 2024
1 parent 97b8cc0 commit ae6b4f2
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 28 deletions.
24 changes: 12 additions & 12 deletions cmd/oras/internal/display/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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()

Check warning on line 124 in cmd/oras/internal/display/handler.go

View check run for this annotation

Codecov / codecov/patch

cmd/oras/internal/display/handler.go#L124

Added line #L124 was not covered by tests
Expand All @@ -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 == "" {
Expand Down
18 changes: 9 additions & 9 deletions cmd/oras/internal/option/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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"},
}
}

Expand All @@ -78,8 +78,8 @@ func (opts *Format) Parse(_ *cobra.Command) error {
return err

Check warning on line 78 in cmd/oras/internal/option/format.go

View check run for this annotation

Codecov / codecov/patch

cmd/oras/internal/option/format.go#L78

Added line #L78 was not covered by tests
}

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)

Check warning on line 82 in cmd/oras/internal/option/format.go

View check run for this annotation

Codecov / codecov/patch

cmd/oras/internal/option/format.go#L82

Added line #L82 was not covered by tests
}
if opts.Type == "" {
// flag not specified
Expand Down
10 changes: 5 additions & 5 deletions cmd/oras/root/discover.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
4 changes: 2 additions & 2 deletions cmd/oras/root/manifest/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit ae6b4f2

Please sign in to comment.