Skip to content

Commit

Permalink
refactor: Move printer to output package (#1394)
Browse files Browse the repository at this point in the history
Signed-off-by: Terry Howe <[email protected]>
  • Loading branch information
Terry Howe authored Jun 12, 2024
1 parent 8027e16 commit 3291d32
Show file tree
Hide file tree
Showing 11 changed files with 31 additions and 25 deletions.
5 changes: 3 additions & 2 deletions cmd/oras/internal/display/status/deprecated.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ limitations under the License.
package status

import (
"oras.land/oras/cmd/oras/internal/output"
"sync"

ocispec "github.com/opencontainers/image-spec/specs-go/v1"
Expand All @@ -28,7 +29,7 @@ import (

// NewTagStatusHintPrinter creates a wrapper type for printing
// tag status and hint.
func NewTagStatusHintPrinter(printer *Printer, target oras.Target, refPrefix string) oras.Target {
func NewTagStatusHintPrinter(printer *output.Printer, target oras.Target, refPrefix string) oras.Target {
var printHint sync.Once
var printHintErr error
onTagging := func(desc ocispec.Descriptor, tag string) error {
Expand All @@ -45,7 +46,7 @@ func NewTagStatusHintPrinter(printer *Printer, target oras.Target, refPrefix str
}

// NewTagStatusPrinter creates a wrapper type for printing tag status.
func NewTagStatusPrinter(printer *Printer, target oras.Target) oras.Target {
func NewTagStatusPrinter(printer *output.Printer, target oras.Target) oras.Target {
return listener.NewTagListener(target, nil, func(desc ocispec.Descriptor, tag string) error {
return printer.Println("Tagged", tag)
})
Expand Down
11 changes: 6 additions & 5 deletions cmd/oras/internal/display/status/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package status
import (
"context"
"io"
"oras.land/oras/cmd/oras/internal/output"
"sync"

ocispec "github.com/opencontainers/image-spec/specs-go/v1"
Expand All @@ -28,14 +29,14 @@ import (
// TextPushHandler handles text status output for push events.
type TextPushHandler struct {
verbose bool
printer *Printer
printer *output.Printer
}

// NewTextPushHandler returns a new handler for push command.
func NewTextPushHandler(out io.Writer, verbose bool) PushHandler {
return &TextPushHandler{
verbose: verbose,
printer: NewPrinter(out),
printer: output.NewPrinter(out),
}
}

Expand Down Expand Up @@ -75,7 +76,7 @@ func (ph *TextPushHandler) UpdateCopyOptions(opts *oras.CopyGraphOptions, fetche
}
opts.PostCopy = func(ctx context.Context, desc ocispec.Descriptor) error {
committed.Store(desc.Digest.String(), desc.Annotations[ocispec.AnnotationTitle])
if err := PrintSuccessorStatus(ctx, desc, fetcher, committed, ph.printer.StatusPrinter(promptSkipped, ph.verbose)); err != nil {
if err := output.PrintSuccessorStatus(ctx, desc, fetcher, committed, ph.printer.StatusPrinter(promptSkipped, ph.verbose)); err != nil {
return err
}
return ph.printer.PrintStatus(desc, promptUploaded, ph.verbose)
Expand All @@ -90,7 +91,7 @@ func NewTextAttachHandler(out io.Writer, verbose bool) AttachHandler {
// TextPullHandler handles text status output for pull events.
type TextPullHandler struct {
verbose bool
printer *Printer
printer *output.Printer
}

// TrackTarget implements PullHander.
Expand Down Expand Up @@ -127,6 +128,6 @@ func (ph *TextPullHandler) OnNodeSkipped(desc ocispec.Descriptor) error {
func NewTextPullHandler(out io.Writer, verbose bool) PullHandler {
return &TextPullHandler{
verbose: verbose,
printer: NewPrinter(out),
printer: output.NewPrinter(out),
}
}
3 changes: 2 additions & 1 deletion cmd/oras/internal/display/status/tty.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package status

import (
"context"
"oras.land/oras/cmd/oras/internal/output"
"os"
"sync"

Expand Down Expand Up @@ -76,7 +77,7 @@ func (ph *TTYPushHandler) UpdateCopyOptions(opts *oras.CopyGraphOptions, fetcher
}
opts.PostCopy = func(ctx context.Context, desc ocispec.Descriptor) error {
committed.Store(desc.Digest.String(), desc.Annotations[ocispec.AnnotationTitle])
return PrintSuccessorStatus(ctx, desc, fetcher, committed, func(d ocispec.Descriptor) error {
return output.PrintSuccessorStatus(ctx, desc, fetcher, committed, func(d ocispec.Descriptor) error {
return ph.tracked.Prompt(d, promptSkipped)
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package status
package output

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package status
package output

import (
"fmt"
Expand Down
6 changes: 3 additions & 3 deletions cmd/oras/root/blob/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ import (
"errors"
"fmt"
"io"
"oras.land/oras/cmd/oras/internal/output"
"os"

ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/spf13/cobra"
"oras.land/oras-go/v2"
"oras.land/oras/cmd/oras/internal/argument"
"oras.land/oras/cmd/oras/internal/command"
"oras.land/oras/cmd/oras/internal/display/status"
"oras.land/oras/cmd/oras/internal/display/status/track"
oerrors "oras.land/oras/cmd/oras/internal/errors"
"oras.land/oras/cmd/oras/internal/option"
Expand Down Expand Up @@ -103,7 +103,7 @@ 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)
printer := status.NewPrinter(cmd.OutOrStdout())
printer := output.NewPrinter(cmd.OutOrStdout())

target, err := opts.NewTarget(opts.Common, logger)
if err != nil {
Expand Down Expand Up @@ -145,7 +145,7 @@ func pushBlob(cmd *cobra.Command, opts *pushBlobOptions) (err error) {

return nil
}
func (opts *pushBlobOptions) doPush(ctx context.Context, printer *status.Printer, t oras.Target, desc ocispec.Descriptor, r io.Reader) error {
func (opts *pushBlobOptions) doPush(ctx context.Context, printer *output.Printer, t oras.Target, desc ocispec.Descriptor, r io.Reader) error {
if opts.TTY == nil {
// none TTY output
if err := printer.PrintStatus(desc, "Uploading", opts.Verbose); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions cmd/oras/root/blob/push_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ package blob
import (
"bytes"
"context"
"oras.land/oras/cmd/oras/internal/display/status"
"oras.land/oras/cmd/oras/internal/output"
"os"
"testing"

Expand All @@ -40,7 +40,7 @@ func Test_pushBlobOptions_doPush(t *testing.T) {
src := memory.New()
content := []byte("test")
r := bytes.NewReader(content)
printer := status.NewPrinter(os.Stdout)
printer := output.NewPrinter(os.Stdout)
desc := ocispec.Descriptor{
MediaType: "application/octet-stream",
Digest: digest.FromBytes(content),
Expand Down
9 changes: 5 additions & 4 deletions cmd/oras/root/cp.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"context"
"encoding/json"
"fmt"
"oras.land/oras/cmd/oras/internal/output"
"slices"
"strings"
"sync"
Expand Down Expand Up @@ -109,7 +110,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())
printer := output.NewPrinter(cmd.OutOrStdout())

// Prepare source
src, err := opts.From.NewReadonlyTarget(ctx, opts.Common, logger)
Expand Down Expand Up @@ -152,7 +153,7 @@ func runCopy(cmd *cobra.Command, opts *copyOptions) error {
return nil
}

func doCopy(ctx context.Context, printer *status.Printer, src oras.ReadOnlyGraphTarget, dst oras.GraphTarget, opts *copyOptions) (ocispec.Descriptor, error) {
func doCopy(ctx context.Context, printer *output.Printer, src oras.ReadOnlyGraphTarget, dst oras.GraphTarget, opts *copyOptions) (ocispec.Descriptor, error) {
// Prepare copy options
committed := &sync.Map{}
extendedCopyOptions := oras.DefaultExtendedCopyOptions
Expand Down Expand Up @@ -186,7 +187,7 @@ func doCopy(ctx context.Context, printer *status.Printer, src oras.ReadOnlyGraph
}
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, printer.StatusPrinter(promptSkipped, opts.Verbose)); err != nil {
if err := output.PrintSuccessorStatus(ctx, desc, dst, committed, printer.StatusPrinter(promptSkipped, opts.Verbose)); err != nil {
return err
}
return printer.PrintStatus(desc, promptCopied, opts.Verbose)
Expand All @@ -209,7 +210,7 @@ func doCopy(ctx context.Context, printer *status.Printer, src oras.ReadOnlyGraph
}
extendedCopyOptions.PostCopy = func(ctx context.Context, desc ocispec.Descriptor) error {
committed.Store(desc.Digest.String(), desc.Annotations[ocispec.AnnotationTitle])
return status.PrintSuccessorStatus(ctx, desc, tracked, committed, func(desc ocispec.Descriptor) error {
return output.PrintSuccessorStatus(ctx, desc, tracked, committed, func(desc ocispec.Descriptor) error {
return tracked.Prompt(desc, promptSkipped)
})
}
Expand Down
8 changes: 4 additions & 4 deletions cmd/oras/root/cp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"net/http"
"net/http/httptest"
"net/url"
"oras.land/oras/cmd/oras/internal/display/status"
"oras.land/oras/cmd/oras/internal/output"
"os"
"strings"
"testing"
Expand Down Expand Up @@ -132,7 +132,7 @@ func Test_doCopy(t *testing.T) {
opts.From.Reference = memDesc.Digest.String()
dst := memory.New()
builder := &strings.Builder{}
printer := status.NewPrinter(builder)
printer := output.NewPrinter(builder)
// test
_, err = doCopy(context.Background(), printer, memStore, dst, &opts)
if err != nil {
Expand All @@ -156,7 +156,7 @@ func Test_doCopy_skipped(t *testing.T) {
opts.Verbose = true
opts.From.Reference = memDesc.Digest.String()
builder := &strings.Builder{}
printer := status.NewPrinter(builder)
printer := output.NewPrinter(builder)
// test
_, err = doCopy(context.Background(), printer, memStore, memStore, &opts)
if err != nil {
Expand Down Expand Up @@ -191,7 +191,7 @@ func Test_doCopy_mounted(t *testing.T) {
}
to.PlainHTTP = true
builder := &strings.Builder{}
printer := status.NewPrinter(builder)
printer := output.NewPrinter(builder)
// test
_, err = doCopy(context.Background(), printer, from, to, &opts)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion cmd/oras/root/manifest/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"context"
"errors"
"fmt"
"oras.land/oras/cmd/oras/internal/output"
"os"
"strings"

Expand Down Expand Up @@ -111,7 +112,7 @@ 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)
printer := status.NewPrinter(cmd.OutOrStdout())
printer := output.NewPrinter(cmd.OutOrStdout())
var target oras.Target
var err error
target, err = opts.NewTarget(opts.Common, logger)
Expand Down
3 changes: 2 additions & 1 deletion cmd/oras/root/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package root
import (
"errors"
"fmt"
"oras.land/oras/cmd/oras/internal/output"

"github.com/spf13/cobra"
"oras.land/oras-go/v2"
Expand Down Expand Up @@ -98,7 +99,7 @@ Example - Tag the manifest 'v1.0.1' to 'v1.0.2' in an OCI image layout folder 'l

func tagManifest(cmd *cobra.Command, opts *tagOptions) error {
ctx, logger := command.GetLogger(cmd, &opts.Common)
printer := status.NewPrinter(cmd.OutOrStdout())
printer := output.NewPrinter(cmd.OutOrStdout())
target, err := opts.NewTarget(opts.Common, logger)
if err != nil {
return err
Expand Down

0 comments on commit 3291d32

Please sign in to comment.