Skip to content

Commit

Permalink
chore: Add format type text
Browse files Browse the repository at this point in the history
Signed-off-by: Terry Howe <[email protected]>
  • Loading branch information
TerryHowe committed Jun 8, 2024
1 parent 4be252f commit ce73b37
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
16 changes: 8 additions & 8 deletions cmd/oras/internal/display/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ func NewPushHandler(out io.Writer, format option.Format, tty *os.File, verbose b
var statusHandler status.PushHandler
if tty != nil {
statusHandler = status.NewTTYPushHandler(tty)
} else if format.Type == "" {
} else if format.Type == option.FormatTypeText.Name {
statusHandler = status.NewTextPushHandler(out, verbose)
} else {
statusHandler = status.NewDiscardHandler()
}

var metadataHandler metadata.PushHandler
switch format.Type {
case "":
case option.FormatTypeText.Name:
metadataHandler = text.NewPushHandler(out)
case option.FormatTypeJSON.Name:
metadataHandler = json.NewPushHandler(out)
Expand All @@ -64,15 +64,15 @@ func NewAttachHandler(out io.Writer, format option.Format, tty *os.File, verbose
var statusHandler status.AttachHandler
if tty != nil {
statusHandler = status.NewTTYAttachHandler(tty)
} else if format.Type == "" {
} else if format.Type == option.FormatTypeText.Name {
statusHandler = status.NewTextAttachHandler(out, verbose)
} else {
statusHandler = status.NewDiscardHandler()
}

var metadataHandler metadata.AttachHandler
switch format.Type {
case "":
case option.FormatTypeText.Name:
metadataHandler = text.NewAttachHandler(out)
case option.FormatTypeJSON.Name:
metadataHandler = json.NewAttachHandler(out)
Expand All @@ -89,15 +89,15 @@ func NewPullHandler(out io.Writer, format option.Format, path string, tty *os.Fi
var statusHandler status.PullHandler
if tty != nil {
statusHandler = status.NewTTYPullHandler(tty)
} else if format.Type == "" {
} else if format.Type == option.FormatTypeText.Name {
statusHandler = status.NewTextPullHandler(out, verbose)
} else {
statusHandler = status.NewDiscardHandler()
}

var metadataHandler metadata.PullHandler
switch format.Type {
case "":
case option.FormatTypeText.Name:
metadataHandler = text.NewPullHandler(out)
case option.FormatTypeJSON.Name:
metadataHandler = json.NewPullHandler(out, path)
Expand All @@ -113,7 +113,7 @@ 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.FormatTypeTree.Name, "":
case option.FormatTypeTree.Name, option.FormatTypeText.Name:
handler = tree.NewDiscoverHandler(out, path, desc, verbose)
case option.FormatTypeTable.Name:
handler = table.NewDiscoverHandler(out, rawReference, desc, verbose)
Expand All @@ -133,7 +133,7 @@ func NewManifestFetchHandler(out io.Writer, format option.Format, outputDescript
var contentHandler content.ManifestFetchHandler

switch format.Type {
case "":
case option.FormatTypeText.Name:
// raw
if outputDescriptor {
metadataHandler = descriptor.NewManifestFetchHandler(out, pretty)
Expand Down
8 changes: 6 additions & 2 deletions cmd/oras/internal/option/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ var (
Name: "tree",
Usage: "Get referrers recursively and print in tree format",
}
FormatTypeText = &FormatType{
Name: "",
Usage: "Print in text format",
}
)

// Format contains input and parsed options for formatted output flags.
Expand All @@ -74,14 +78,14 @@ type Format struct {
AllowedTypes []*FormatType
}

// ApplyFlag implements FlagProvider.ApplyFlag.
// ApplyFlags implements FlagProvider.ApplyFlag.
func (opts *Format) ApplyFlags(fs *pflag.FlagSet) {
buf := bytes.NewBufferString("[Experimental] Format output using a custom template:")
w := tabwriter.NewWriter(buf, 0, 0, 2, ' ', 0)
for _, t := range opts.AllowedTypes {
_, _ = fmt.Fprintf(w, "\n'%s':\t%s", t.Name, t.Usage)
}
w.Flush()
_ = w.Flush()
// apply flags
fs.StringVar(&opts.FormatFlag, "format", opts.FormatFlag, buf.String())
fs.StringVar(&opts.Template, "template", "", "[Experimental] Template string used to format output")
Expand Down

0 comments on commit ce73b37

Please sign in to comment.