Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: reduce printer instantiation & unify stdout/stderr usage #1433

Merged
merged 2 commits into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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 @@
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 @@

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 @@
return err
}
if match {
if err := printer.PrintStatus(desc, "Exists"); err != nil {
if err := opts.PrintStatus(desc, "Exists"); err != nil {

Check warning on line 159 in cmd/oras/root/manifest/push.go

View check run for this annotation

Codecov / codecov/patch

cmd/oras/root/manifest/push.go#L159

Added line #L159 was not covered by tests
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 @@
}
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 {

Check warning on line 192 in cmd/oras/root/manifest/push.go

View check run for this annotation

Codecov / codecov/patch

cmd/oras/root/manifest/push.go#L192

Added line #L192 was not covered by tests
return err
}
}

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

return nil
}
Expand Down
Loading