diff --git a/cmd/oras/internal/option/remote.go b/cmd/oras/internal/option/remote.go index 0ccd75ef0..d15f0ed27 100644 --- a/cmd/oras/internal/option/remote.go +++ b/cmd/oras/internal/option/remote.go @@ -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) diff --git a/cmd/oras/root/blob/push.go b/cmd/oras/root/blob/push.go index 73cd30ea2..6bb126357 100644 --- a/cmd/oras/root/blob/push.go +++ b/cmd/oras/root/blob/push.go @@ -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 { @@ -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 { @@ -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 @@ -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 } diff --git a/cmd/oras/root/discover.go b/cmd/oras/root/discover.go index 505334bd0..90f1f3c7f 100644 --- a/cmd/oras/root/discover.go +++ b/cmd/oras/root/discover.go @@ -18,7 +18,6 @@ package root import ( "context" "errors" - "fmt" ocispec "github.com/opencontainers/image-spec/specs-go/v1" "github.com/spf13/cobra" @@ -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") } @@ -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 } diff --git a/cmd/oras/root/manifest/fetch.go b/cmd/oras/root/manifest/fetch.go index 7f6e388be..d1755dd66 100644 --- a/cmd/oras/root/manifest/fetch.go +++ b/cmd/oras/root/manifest/fetch.go @@ -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 } diff --git a/cmd/oras/root/manifest/push.go b/cmd/oras/root/manifest/push.go index 13dcf0a6c..2c11a2dad 100644 --- a/cmd/oras/root/manifest/push.go +++ b/cmd/oras/root/manifest/push.go @@ -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" ) @@ -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 { @@ -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) @@ -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 } } @@ -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 }