From 6615478377234df3ae7afae893590d9d9ce82ef8 Mon Sep 17 00:00:00 2001 From: Billy Zha Date: Wed, 10 Apr 2024 02:06:57 +0000 Subject: [PATCH] resolve comments Signed-off-by: Billy Zha --- .../internal/display/content/manifest_fetch.go | 2 +- cmd/oras/internal/display/handler.go | 8 ++++++-- .../metadata/descriptor/manifest_fetch.go | 2 +- cmd/oras/internal/display/metadata/discard.go | 4 +++- cmd/oras/internal/display/utils/utils.go | 4 ++-- cmd/oras/internal/option/pretty.go | 16 ++-------------- 6 files changed, 15 insertions(+), 21 deletions(-) diff --git a/cmd/oras/internal/display/content/manifest_fetch.go b/cmd/oras/internal/display/content/manifest_fetch.go index b6c9b5d81..8a826337e 100644 --- a/cmd/oras/internal/display/content/manifest_fetch.go +++ b/cmd/oras/internal/display/content/manifest_fetch.go @@ -41,7 +41,7 @@ func (h *manifestFetch) OnContentFetched(desc ocispec.Descriptor, manifest []byt defer f.Close() out = f } - return utils.Output(out, manifest, h.pretty) + return utils.PrintJSON(out, manifest, h.pretty) } // NewManifestFetchHandler creates a new handler. diff --git a/cmd/oras/internal/display/handler.go b/cmd/oras/internal/display/handler.go index 29cc2d921..884b7427e 100644 --- a/cmd/oras/internal/display/handler.go +++ b/cmd/oras/internal/display/handler.go @@ -98,9 +98,9 @@ func NewPullHandler(format string, path string, tty *os.File, out io.Writer, ver } // NewManifestFetchHandler returns a manifest fetch handler. -func NewManifestFetchHandler(out io.Writer, format string, outputDescriptor bool, pretty bool, outputPath string) (metadata.ManifestFetchHandler, content.ManifestFetchHandler) { +func NewManifestFetchHandler(out io.Writer, format string, outputDescriptor, pretty bool, outputPath string) (metadata.ManifestFetchHandler, content.ManifestFetchHandler) { var metadataHandler metadata.ManifestFetchHandler - var contentHandler content.ManifestFetchHandler = content.NewManifestFetchHandler(out, pretty, outputPath) + var contentHandler content.ManifestFetchHandler switch format { case "": @@ -123,5 +123,9 @@ func NewManifestFetchHandler(out io.Writer, format string, outputDescriptor bool contentHandler = content.NewDiscardHandler() } } + + if contentHandler == nil { + contentHandler = content.NewManifestFetchHandler(out, pretty, outputPath) + } return metadataHandler, contentHandler } diff --git a/cmd/oras/internal/display/metadata/descriptor/manifest_fetch.go b/cmd/oras/internal/display/metadata/descriptor/manifest_fetch.go index db7f9ede8..a55407494 100644 --- a/cmd/oras/internal/display/metadata/descriptor/manifest_fetch.go +++ b/cmd/oras/internal/display/metadata/descriptor/manifest_fetch.go @@ -37,7 +37,7 @@ func (h *manifestFetchHandler) OnFetched(_ string, desc ocispec.Descriptor, _ [] if err != nil { return fmt.Errorf("invalid descriptor: %w", err) } - return utils.Output(h.out, descBytes, h.pretty) + return utils.PrintJSON(h.out, descBytes, h.pretty) } // NewManifestFetchHandler creates a new handler. diff --git a/cmd/oras/internal/display/metadata/discard.go b/cmd/oras/internal/display/metadata/discard.go index 2e2f0a382..4b1941f59 100644 --- a/cmd/oras/internal/display/metadata/discard.go +++ b/cmd/oras/internal/display/metadata/discard.go @@ -25,4 +25,6 @@ func NewDiscardHandler() ManifestFetchHandler { } // OnFetched implements ManifestFetchHandler. -func (discard) OnFetched(string, ocispec.Descriptor, []byte) error { return nil } +func (discard) OnFetched(string, ocispec.Descriptor, []byte) error { + return nil +} diff --git a/cmd/oras/internal/display/utils/utils.go b/cmd/oras/internal/display/utils/utils.go index 8cde3dd43..776be54df 100644 --- a/cmd/oras/internal/display/utils/utils.go +++ b/cmd/oras/internal/display/utils/utils.go @@ -39,8 +39,8 @@ const ( PullPromptDownloaded = "Downloaded " ) -// Output writes the data to the output stream, optionally prettifying it. -func Output(out io.Writer, data []byte, pretty bool) error { +// PrintJSON writes the data to the output stream, optionally prettifying it. +func PrintJSON(out io.Writer, data []byte, pretty bool) error { if pretty { buf := bytes.NewBuffer(nil) if err := json.Indent(buf, data, "", " "); err != nil { diff --git a/cmd/oras/internal/option/pretty.go b/cmd/oras/internal/option/pretty.go index de889522e..6013be141 100644 --- a/cmd/oras/internal/option/pretty.go +++ b/cmd/oras/internal/option/pretty.go @@ -16,12 +16,10 @@ limitations under the License. package option import ( - "bytes" - "encoding/json" - "fmt" "io" "github.com/spf13/pflag" + "oras.land/oras/cmd/oras/internal/display/utils" ) // Pretty option struct. @@ -37,15 +35,5 @@ func (opts *Pretty) ApplyFlags(fs *pflag.FlagSet) { // Output outputs the prettified content if `--pretty` flag is used. Otherwise // outputs the original content. func (opts *Pretty) Output(w io.Writer, content []byte) error { - if opts.Pretty { - buf := bytes.NewBuffer(nil) - if err := json.Indent(buf, content, "", " "); err != nil { - return fmt.Errorf("failed to prettify: %w", err) - } - buf.WriteByte('\n') - content = buf.Bytes() - } - - _, err := w.Write(content) - return err + return utils.PrintJSON(w, content, opts.Pretty) }