From e24ce35214bec070956f26f3ddeff6123f66228c Mon Sep 17 00:00:00 2001 From: Terry Howe Date: Mon, 20 May 2024 07:46:44 -0600 Subject: [PATCH] refactor: Get rid of deprecated PrintStatus method (#1378) Signed-off-by: Terry Howe --- cmd/oras/internal/display/status/deprecated.go | 5 ----- cmd/oras/root/cp.go | 15 ++++++++------- cmd/oras/root/cp_test.go | 14 +++++++++++--- 3 files changed, 19 insertions(+), 15 deletions(-) diff --git a/cmd/oras/internal/display/status/deprecated.go b/cmd/oras/internal/display/status/deprecated.go index f53875295..ccef66062 100644 --- a/cmd/oras/internal/display/status/deprecated.go +++ b/cmd/oras/internal/display/status/deprecated.go @@ -61,11 +61,6 @@ func Print(a ...any) error { return printer.Println(a...) } -// StatusPrinter returns a tracking function for transfer status. -func StatusPrinter(status string, verbose bool) PrintFunc { - return printer.StatusPrinter(status, verbose) -} - // PrintStatus prints transfer status. func PrintStatus(desc ocispec.Descriptor, status string, verbose bool) error { return printer.PrintStatus(desc, status, verbose) diff --git a/cmd/oras/root/cp.go b/cmd/oras/root/cp.go index 1f1036b69..7cec065a4 100644 --- a/cmd/oras/root/cp.go +++ b/cmd/oras/root/cp.go @@ -109,6 +109,7 @@ Example - Copy an artifact with multiple tags with concurrency tuned: func runCopy(cmd *cobra.Command, opts *copyOptions) error { ctx, logger := command.GetLogger(cmd, &opts.Common) + printer := status.NewPrinter(cmd.OutOrStdout()) // Prepare source src, err := opts.From.NewReadonlyTarget(ctx, opts.Common, logger) @@ -126,7 +127,7 @@ func runCopy(cmd *cobra.Command, opts *copyOptions) error { } ctx = registryutil.WithScopeHint(ctx, dst, auth.ActionPull, auth.ActionPush) - desc, err := doCopy(ctx, src, dst, opts) + desc, err := doCopy(ctx, printer, src, dst, opts) if err != nil { return err } @@ -151,7 +152,7 @@ func runCopy(cmd *cobra.Command, opts *copyOptions) error { return nil } -func doCopy(ctx context.Context, src oras.ReadOnlyGraphTarget, dst oras.GraphTarget, opts *copyOptions) (ocispec.Descriptor, error) { +func doCopy(ctx context.Context, printer *status.Printer, src oras.ReadOnlyGraphTarget, dst oras.GraphTarget, opts *copyOptions) (ocispec.Descriptor, error) { // Prepare copy options committed := &sync.Map{} extendedCopyOptions := oras.DefaultExtendedCopyOptions @@ -178,21 +179,21 @@ func doCopy(ctx context.Context, src oras.ReadOnlyGraphTarget, dst oras.GraphTar // none TTY output extendedCopyOptions.OnCopySkipped = func(ctx context.Context, desc ocispec.Descriptor) error { committed.Store(desc.Digest.String(), desc.Annotations[ocispec.AnnotationTitle]) - return status.PrintStatus(desc, promptExists, opts.Verbose) + return printer.PrintStatus(desc, promptExists, opts.Verbose) } extendedCopyOptions.PreCopy = func(ctx context.Context, desc ocispec.Descriptor) error { - return status.PrintStatus(desc, promptCopying, opts.Verbose) + return printer.PrintStatus(desc, promptCopying, opts.Verbose) } extendedCopyOptions.PostCopy = func(ctx context.Context, desc ocispec.Descriptor) error { committed.Store(desc.Digest.String(), desc.Annotations[ocispec.AnnotationTitle]) - if err := status.PrintSuccessorStatus(ctx, desc, dst, committed, status.StatusPrinter(promptSkipped, opts.Verbose)); err != nil { + if err := status.PrintSuccessorStatus(ctx, desc, dst, committed, printer.StatusPrinter(promptSkipped, opts.Verbose)); err != nil { return err } - return status.PrintStatus(desc, promptCopied, opts.Verbose) + return printer.PrintStatus(desc, promptCopied, opts.Verbose) } extendedCopyOptions.OnMounted = func(ctx context.Context, desc ocispec.Descriptor) error { committed.Store(desc.Digest.String(), desc.Annotations[ocispec.AnnotationTitle]) - return status.PrintStatus(desc, promptMounted, opts.Verbose) + return printer.PrintStatus(desc, promptMounted, opts.Verbose) } } else { // TTY output diff --git a/cmd/oras/root/cp_test.go b/cmd/oras/root/cp_test.go index 29301faf5..83144a49f 100644 --- a/cmd/oras/root/cp_test.go +++ b/cmd/oras/root/cp_test.go @@ -24,7 +24,9 @@ import ( "net/http" "net/http/httptest" "net/url" + "oras.land/oras/cmd/oras/internal/display/status" "os" + "strings" "testing" "github.com/opencontainers/go-digest" @@ -129,8 +131,10 @@ func Test_doCopy(t *testing.T) { opts.Verbose = true opts.From.Reference = memDesc.Digest.String() dst := memory.New() + builder := &strings.Builder{} + printer := status.NewPrinter(builder) // test - _, err = doCopy(context.Background(), memStore, dst, &opts) + _, err = doCopy(context.Background(), printer, memStore, dst, &opts) if err != nil { t.Fatal(err) } @@ -151,8 +155,10 @@ func Test_doCopy_skipped(t *testing.T) { opts.TTY = slave opts.Verbose = true opts.From.Reference = memDesc.Digest.String() + builder := &strings.Builder{} + printer := status.NewPrinter(builder) // test - _, err = doCopy(context.Background(), memStore, memStore, &opts) + _, err = doCopy(context.Background(), printer, memStore, memStore, &opts) if err != nil { t.Fatal(err) } @@ -184,8 +190,10 @@ func Test_doCopy_mounted(t *testing.T) { t.Fatal(err) } to.PlainHTTP = true + builder := &strings.Builder{} + printer := status.NewPrinter(builder) // test - _, err = doCopy(context.Background(), from, to, &opts) + _, err = doCopy(context.Background(), printer, from, to, &opts) if err != nil { t.Fatal(err) }