Skip to content

Commit

Permalink
refactor: reduce printer instantiation & unify stdout/stderr usage (#…
Browse files Browse the repository at this point in the history
…1433)

Signed-off-by: Billy Zha <[email protected]>
  • Loading branch information
qweeah authored Jun 27, 2024
1 parent 46de891 commit f84d572
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 21 deletions.
4 changes: 2 additions & 2 deletions cmd/oras/internal/option/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,9 @@ func (opts *Remote) Parse(cmd *cobra.Command) error {
// optional cmd prompt.
func (opts *Remote) readSecret(cmd *cobra.Command) (err error) {
if cmd.Flags().Changed(identityTokenFlag) {
fmt.Fprintln(cmd.ErrOrStderr(), "WARNING! Using --identity-token via the CLI is insecure. Use --identity-token-stdin.")
cmd.PrintErrln("WARNING! Using --identity-token via the CLI is insecure. Use --identity-token-stdin.")
} else if cmd.Flags().Changed(passwordFlag) {
fmt.Fprintln(cmd.ErrOrStderr(), "WARNING! Using --password via the CLI is insecure. Use --password-stdin.")
cmd.PrintErrln("WARNING! Using --password via the CLI is insecure. Use --password-stdin.")
} else if opts.secretFromStdin {
// Prompt for credential
secret, err := io.ReadAll(os.Stdin)
Expand Down
11 changes: 5 additions & 6 deletions cmd/oras/root/blob/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ Example - Push blob 'hi.txt' into an OCI image layout folder 'layout-dir':
return errors.New("`--size` must be provided if the blob is read from stdin")
}
}
opts.Verbose = opts.Verbose && !opts.OutputDescriptor
return option.Parse(cmd, &opts)
},
RunE: func(cmd *cobra.Command, args []string) error {
Expand All @@ -102,8 +103,6 @@ Example - Push blob 'hi.txt' into an OCI image layout folder 'layout-dir':

func pushBlob(cmd *cobra.Command, opts *pushBlobOptions) (err error) {
ctx, logger := command.GetLogger(cmd, &opts.Common)
verbose := opts.Verbose && !opts.OutputDescriptor
printer := output.NewPrinter(cmd.OutOrStdout(), cmd.ErrOrStderr(), verbose)

target, err := opts.NewTarget(opts.Common, logger)
if err != nil {
Expand All @@ -122,9 +121,9 @@ func pushBlob(cmd *cobra.Command, opts *pushBlobOptions) (err error) {
return err
}
if exists {
err = printer.PrintStatus(desc, "Exists")
err = opts.PrintStatus(desc, "Exists")
} else {
err = opts.doPush(ctx, printer, target, desc, rc)
err = opts.doPush(ctx, opts.Printer, target, desc, rc)
}
if err != nil {
return err
Expand All @@ -138,8 +137,8 @@ func pushBlob(cmd *cobra.Command, opts *pushBlobOptions) (err error) {
return opts.Output(os.Stdout, descJSON)
}

_ = printer.Println("Pushed", opts.AnnotatedReference())
_ = printer.Println("Digest:", desc.Digest)
_ = opts.Println("Pushed", opts.AnnotatedReference())
_ = opts.Println("Digest:", desc.Digest)

return nil
}
Expand Down
5 changes: 2 additions & 3 deletions cmd/oras/root/discover.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package root
import (
"context"
"errors"
"fmt"

ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -85,7 +84,7 @@ Example - Discover referrers of the manifest tagged 'v1' in an OCI image layout
if cmd.Flags().Changed("output") {
switch opts.Format.Type {
case "tree", "json", "table":
_, _ = fmt.Fprintf(cmd.ErrOrStderr(), "[DEPRECATED] --output is deprecated, try `--format %s` instead\n", opts.Template)
cmd.PrintErrf("[DEPRECATED] --output is deprecated, try `--format %s` instead\n", opts.Template)
default:
return errors.New("output type can only be tree, table or json")
}
Expand Down Expand Up @@ -128,7 +127,7 @@ func runDiscover(cmd *cobra.Command, opts *discoverOptions) error {
return err
}

handler, err := display.NewDiscoverHandler(cmd.OutOrStdout(), opts.Format, opts.Path, opts.RawReference, desc, opts.Verbose)
handler, err := display.NewDiscoverHandler(opts.Printer, opts.Format, opts.Path, opts.RawReference, desc, opts.Verbose)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/oras/root/manifest/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ Example - Fetch raw manifest from an OCI layout archive file 'layout.tar':

func fetchManifest(cmd *cobra.Command, opts *fetchOptions) (fetchErr error) {
ctx, logger := command.GetLogger(cmd, &opts.Common)
metadataHandler, contentHandler, err := display.NewManifestFetchHandler(cmd.OutOrStdout(), opts.Format, opts.OutputDescriptor, opts.Pretty.Pretty, opts.outputPath)
metadataHandler, contentHandler, err := display.NewManifestFetchHandler(opts.Printer, opts.Format, opts.OutputDescriptor, opts.Pretty.Pretty, opts.outputPath)
if err != nil {
return err
}
Expand Down
16 changes: 7 additions & 9 deletions cmd/oras/root/manifest/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import (
oerrors "oras.land/oras/cmd/oras/internal/errors"
"oras.land/oras/cmd/oras/internal/manifest"
"oras.land/oras/cmd/oras/internal/option"
"oras.land/oras/cmd/oras/internal/output"
"oras.land/oras/internal/file"
)

Expand Down Expand Up @@ -96,6 +95,7 @@ Example - Push a manifest to an OCI image layout folder 'layout-dir' and tag wit
refs := strings.Split(args[0], ",")
opts.RawReference = refs[0]
opts.extraRefs = refs[1:]
opts.Verbose = opts.Verbose && !opts.OutputDescriptor
return option.Parse(cmd, &opts)
},
RunE: func(cmd *cobra.Command, args []string) error {
Expand All @@ -112,8 +112,6 @@ Example - Push a manifest to an OCI image layout folder 'layout-dir' and tag wit

func pushManifest(cmd *cobra.Command, opts pushOptions) error {
ctx, logger := command.GetLogger(cmd, &opts.Common)
verbose := opts.Verbose && !opts.OutputDescriptor
printer := output.NewPrinter(cmd.OutOrStdout(), cmd.ErrOrStderr(), verbose)
var target oras.Target
var err error
target, err = opts.NewTarget(opts.Common, logger)
Expand Down Expand Up @@ -158,17 +156,17 @@ func pushManifest(cmd *cobra.Command, opts pushOptions) error {
return err
}
if match {
if err := printer.PrintStatus(desc, "Exists"); err != nil {
if err := opts.PrintStatus(desc, "Exists"); err != nil {
return err
}
} else {
if err = printer.PrintStatus(desc, "Uploading"); err != nil {
if err = opts.PrintStatus(desc, "Uploading"); err != nil {
return err
}
if _, err := oras.TagBytes(ctx, target, mediaType, contentBytes, ref); err != nil {
return err
}
if err = printer.PrintStatus(desc, "Uploaded "); err != nil {
if err = opts.PrintStatus(desc, "Uploaded "); err != nil {
return err
}
}
Expand All @@ -189,14 +187,14 @@ func pushManifest(cmd *cobra.Command, opts pushOptions) error {
}
return opts.Output(os.Stdout, descJSON)
}
_ = printer.Println("Pushed", opts.AnnotatedReference())
_ = opts.Println("Pushed", opts.AnnotatedReference())
if len(opts.extraRefs) != 0 {
if _, err = oras.TagBytesN(ctx, status.NewTagStatusPrinter(printer, target), mediaType, contentBytes, opts.extraRefs, tagBytesNOpts); err != nil {
if _, err = oras.TagBytesN(ctx, status.NewTagStatusPrinter(opts.Printer, target), mediaType, contentBytes, opts.extraRefs, tagBytesNOpts); err != nil {
return err
}
}

_ = printer.Println("Digest:", desc.Digest)
_ = opts.Println("Digest:", desc.Digest)

return nil
}
Expand Down

0 comments on commit f84d572

Please sign in to comment.