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"
"io"
"strings"
"text/tabwriter"
"text/template"
"github.com/containers/common/pkg/report"
"github.com/containers/common/pkg/retry"
@ -53,8 +51,8 @@ See skopeo(1) section "IMAGE NAMES" for the expected format
`, strings.Join(transports.ListNames(), ", ")),
RunE: commandAction(opts.run),
Example: `skopeo inspect docker://registry.fedoraproject.org/fedora
skopeo inspect --config docker://docker.io/alpine
skopeo inspect --format "Name: {{.Name}} Digest: {{.Digest}}" docker://registry.access.redhat.com/ubi8`,
skopeo inspect --config docker://docker.io/alpine
skopeo inspect --format "Name: {{.Name}} Digest: {{.Digest}}" docker://registry.access.redhat.com/ubi8`,
ValidArgsFunction: autocompleteSupportedTransports,
}
adjustUsage(cmd)
@ -74,6 +72,7 @@ func (opts *inspectOptions) run(args []string, stdout io.Writer) (retErr error)
rawManifest []byte
src types.ImageSource
imgInspect *types.ImageInspectInfo
rpt *report.Formatter
data []any
)
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))
}
} 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)
err = printTmpl(stdout, row, data)
err = rpt.Execute(data)
}
if err != nil {
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
}
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 {
t, err := template.New("skopeo inspect").Parse(row)
rpt, err = report.New(stdout, "skopeo inspect").Parse(report.OriginUser, opts.format)
if err != nil {
return err
}
w := tabwriter.NewWriter(stdout, 8, 2, 2, ' ', 0)
if err := t.Execute(w, data); err != nil {
return err
}
return w.Flush()
defer rpt.Flush()
data = append(data, outputData)
return rpt.Execute(data)
}

View File

@ -42,6 +42,7 @@ Use docker daemon host at _host_ (`docker-daemon:` transport only)
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).
Supports the Go templating functions available at https://pkg.go.dev/github.com/containers/common/pkg/report#hdr-Template_Functions
**--help**, **-h**