Skip to content

Commit

Permalink
Create printer object to manage output
Browse files Browse the repository at this point in the history
Signed-off-by: Terry Howe <[email protected]>
  • Loading branch information
TerryHowe committed Mar 17, 2024
1 parent f1d319f commit 63d4954
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
26 changes: 21 additions & 5 deletions cmd/oras/internal/display/status/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,33 @@ import (
"oras.land/oras-go/v2/registry"
)

var printLock sync.Mutex
type Printer struct {
printLock sync.Mutex
verbose bool
}

var printer Printer

// NewPrinter creates a printer object
func NewPrinter(verbose bool) *Printer {
printer = Printer{verbose: verbose}
return &printer
}

// Print objects to display concurrent-safely.
func (p *Printer) Print(a ...any) error {
p.printLock.Lock()
defer p.printLock.Unlock()
_, err := fmt.Println(a...)
return err
}

// PrintFunc is the function type returned by StatusPrinter.
type PrintFunc func(ocispec.Descriptor) error

// Print objects to display concurrent-safely.
func Print(a ...any) error {
printLock.Lock()
defer printLock.Unlock()
_, err := fmt.Println(a...)
return err
return printer.Print(a...)
}

// StatusPrinter returns a tracking function for transfer status.
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 @@ -152,6 +152,7 @@ func pushManifest(cmd *cobra.Command, opts pushOptions) error {
return err
}
verbose := opts.Verbose && !opts.OutputDescriptor
printer := status.NewPrinter(verbose)
if match {
if err := status.PrintStatus(desc, "Exists", verbose); err != nil {
return err
Expand Down Expand Up @@ -184,7 +185,7 @@ func pushManifest(cmd *cobra.Command, opts pushOptions) error {
}
return opts.Output(os.Stdout, descJSON)
}
status.Print("Pushed", opts.AnnotatedReference())
printer.Print("Pushed", opts.AnnotatedReference())
if len(opts.extraRefs) != 0 {
if _, err = oras.TagBytesN(ctx, status.NewTagStatusPrinter(target), mediaType, contentBytes, opts.extraRefs, tagBytesNOpts); err != nil {
return err
Expand Down

0 comments on commit 63d4954

Please sign in to comment.