Merge pull request #1822 from cblecker/report

Use common library reporter
This commit is contained in:
Daniel J Walsh 2023-02-23 12:32:55 -05:00 committed by GitHub
commit 508891281c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 17 deletions

View File

@ -6,8 +6,6 @@ import (
"fmt" "fmt"
"io" "io"
"strings" "strings"
"text/tabwriter"
"text/template"
"github.com/containers/common/pkg/report" "github.com/containers/common/pkg/report"
"github.com/containers/common/pkg/retry" "github.com/containers/common/pkg/retry"
@ -53,8 +51,8 @@ See skopeo(1) section "IMAGE NAMES" for the expected format
`, strings.Join(transports.ListNames(), ", ")), `, strings.Join(transports.ListNames(), ", ")),
RunE: commandAction(opts.run), RunE: commandAction(opts.run),
Example: `skopeo inspect docker://registry.fedoraproject.org/fedora Example: `skopeo inspect docker://registry.fedoraproject.org/fedora
skopeo inspect --config docker://docker.io/alpine skopeo inspect --config docker://docker.io/alpine
skopeo inspect --format "Name: {{.Name}} Digest: {{.Digest}}" docker://registry.access.redhat.com/ubi8`, skopeo inspect --format "Name: {{.Name}} Digest: {{.Digest}}" docker://registry.access.redhat.com/ubi8`,
ValidArgsFunction: autocompleteSupportedTransports, ValidArgsFunction: autocompleteSupportedTransports,
} }
adjustUsage(cmd) adjustUsage(cmd)
@ -74,6 +72,7 @@ func (opts *inspectOptions) run(args []string, stdout io.Writer) (retErr error)
rawManifest []byte rawManifest []byte
src types.ImageSource src types.ImageSource
imgInspect *types.ImageInspectInfo imgInspect *types.ImageInspectInfo
rpt *report.Formatter
data []any data []any
) )
ctx, cancel := opts.global.commandTimeoutContext() ctx, cancel := opts.global.commandTimeoutContext()
@ -158,9 +157,13 @@ func (opts *inspectOptions) run(args []string, stdout io.Writer) (retErr error)
fmt.Fprintf(stdout, "%s\n", string(out)) fmt.Fprintf(stdout, "%s\n", string(out))
} }
} else { } else {
row := "{{range . }}" + report.NormalizeFormat(opts.format) + "{{end}}" rpt, err = report.New(stdout, "skopeo inspect").Parse(report.OriginUser, opts.format)
if err != nil {
return err
}
defer rpt.Flush()
data = append(data, config) data = append(data, config)
err = printTmpl(stdout, row, data) err = rpt.Execute(data)
} }
if err != nil { if err != nil {
return fmt.Errorf("Error writing OCI-formatted configuration data to standard output: %w", err) return fmt.Errorf("Error writing OCI-formatted configuration data to standard output: %w", err)
@ -235,19 +238,12 @@ func (opts *inspectOptions) run(args []string, stdout io.Writer) (retErr error)
} }
return err return err
} }
row := "{{range . }}" + report.NormalizeFormat(opts.format) + "{{end}}"
data = append(data, outputData)
return printTmpl(stdout, row, data)
}
func printTmpl(stdout io.Writer, row string, data []any) error { rpt, err = report.New(stdout, "skopeo inspect").Parse(report.OriginUser, opts.format)
t, err := template.New("skopeo inspect").Parse(row)
if err != nil { if err != nil {
return err return err
} }
w := tabwriter.NewWriter(stdout, 8, 2, 2, ' ', 0) defer rpt.Flush()
if err := t.Execute(w, data); err != nil { data = append(data, outputData)
return err return rpt.Execute(data)
}
return w.Flush()
} }

View File

@ -42,6 +42,7 @@ Use docker daemon host at _host_ (`docker-daemon:` transport only)
Format the output using the given Go template. Format the output using the given Go template.
The keys of the returned JSON can be used as the values for the --format flag (see examples below). The keys of the returned JSON can be used as the values for the --format flag (see examples below).
Supports the Go templating functions available at https://pkg.go.dev/github.com/containers/common/pkg/report#hdr-Template_Functions
**--help**, **-h** **--help**, **-h**