Skip to content

Commit

Permalink
resolve comments
Browse files Browse the repository at this point in the history
Signed-off-by: Billy Zha <[email protected]>
  • Loading branch information
qweeah committed Apr 10, 2024
1 parent cb98a26 commit 6615478
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 21 deletions.
2 changes: 1 addition & 1 deletion cmd/oras/internal/display/content/manifest_fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 6 additions & 2 deletions cmd/oras/internal/display/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 "":
Expand All @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 3 additions & 1 deletion cmd/oras/internal/display/metadata/discard.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
4 changes: 2 additions & 2 deletions cmd/oras/internal/display/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
16 changes: 2 additions & 14 deletions cmd/oras/internal/option/pretty.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
}

0 comments on commit 6615478

Please sign in to comment.