Skip to content

Commit

Permalink
feat: surpress output when template is set
Browse files Browse the repository at this point in the history
Signed-off-by: Billy Zha <[email protected]>
  • Loading branch information
qweeah committed Nov 23, 2023
1 parent 1054471 commit 60cbcc4
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 19 deletions.
29 changes: 19 additions & 10 deletions cmd/oras/internal/display/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,33 @@ import (
"oras.land/oras-go/v2/registry"
)

var printLock sync.Mutex
var (
printLock sync.Mutex
to io.Writer
)

// PrintFunc is the function type returned by StatusPrinter.
type PrintFunc func(ocispec.Descriptor) error
func init() {
to = os.Stdout
}

// PrintErr prints into stderr.
func PrintErr(a ...any) error {
printLock.Lock()
defer printLock.Unlock()
_, err := fmt.Fprintln(os.Stderr, a...)
return err
// Set sets the output writer for printing.
func Set(template string, tty io.Writer) {
if template != "" {
to = tty
}
}

// 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 {
if to == nil {
return nil
}
printLock.Lock()
defer printLock.Unlock()
_, err := fmt.Println(a...)
_, err := fmt.Fprintln(to, a...)
return err
}

Expand Down
10 changes: 6 additions & 4 deletions cmd/oras/internal/option/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,18 @@ import (
)

type Format struct {
template string
Template string
}

// ApplyFlag implements FlagProvider.ApplyFlag
func (opts *Format) ApplyFlags(fs *pflag.FlagSet) {
fs.StringVar(&opts.template, "format", "", `Format output with go template syntax`)
fs.StringVar(&opts.Template, "format", "", `Format output with go template syntax`)
}

func (opts *Format) WriteTo(w io.Writer, data interface{}) error {
switch opts.template {
switch opts.Template {
case "":
return nil
case "json":
// output json
// write marshalled data
Expand All @@ -50,7 +52,7 @@ func (opts *Format) WriteTo(w io.Writer, data interface{}) error {
// go templating
var err error
t := template.New("out").Funcs(sprig.FuncMap())
t, err = t.Parse(opts.template)
t, err = t.Parse(opts.Template)
if err != nil {
return err
}
Expand Down
5 changes: 3 additions & 2 deletions cmd/oras/root/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ Example - Attach file to the manifest tagged 'v1' in an OCI image layout folder
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
display.Set(opts.Template, opts.TTY)
return runAttach(cmd.Context(), opts)
},
}
Expand Down Expand Up @@ -181,8 +182,8 @@ func runAttach(ctx context.Context, opts attachOptions) error {
if !strings.HasSuffix(opts.RawReference, digest) {
opts.RawReference = fmt.Sprintf("%s@%s", opts.Path, subject.Digest)
}
display.PrintErr("Attached to", opts.AnnotatedReference())
display.PrintErr("Digest:", root.Digest)
display.Print("Attached to", opts.AnnotatedReference())
display.Print("Digest:", root.Digest)

// Export manifest
if err = opts.ExportManifest(ctx, store, root); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/oras/root/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func loadFiles(ctx context.Context, store *file.Store, annotations map[string]ma
files = append(files, file)
}
if len(files) == 0 {
display.PrintErr("Uploading empty artifact")
display.Print("Uploading empty artifact")
}
return files, nil
}
5 changes: 3 additions & 2 deletions cmd/oras/root/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ Example - Push file "hi.txt" into an OCI image layout folder 'layout-dir' with t
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
display.Set(opts.Template, opts.TTY)
return runPush(cmd.Context(), opts)
},
}
Expand Down Expand Up @@ -213,7 +214,7 @@ func runPush(ctx context.Context, opts pushOptions) error {
if err != nil {
return err
}
display.PrintErr("Pushed", opts.AnnotatedReference())
display.Print("Pushed", opts.AnnotatedReference())

if len(opts.extraRefs) != 0 {
contentBytes, err := content.FetchAll(ctx, memoryStore, root)
Expand All @@ -227,7 +228,7 @@ func runPush(ctx context.Context, opts pushOptions) error {
}
}

display.PrintErr("Digest:", root.Digest)
display.Print("Digest:", root.Digest)

// Export manifest
if err := opts.ExportManifest(ctx, memoryStore, root); err != nil {
Expand Down

0 comments on commit 60cbcc4

Please sign in to comment.