Skip to content

Commit a0a6286

Browse files
feat: add --format flag to artifact inspect
Many commands support the `--format` flag which accept a go template to allow for formatting for certain values, but it is not yet implemented for artifact inspect command. Adding this feature will allow easy formatting in scripts as well as running it on a terminal. This feature is implemented for artifact inspect by taking reference from images and network commands implementation. Fixes: [#27112](#27112) Signed-off-by: Akash Yadav <[email protected]>
1 parent 7fecff5 commit a0a6286

File tree

2 files changed

+60
-17
lines changed

2 files changed

+60
-17
lines changed

cmd/podman/artifact/inspect.go

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ package artifact
22

33
import (
44
"github.com/containers/podman/v5/cmd/podman/common"
5+
"github.com/containers/podman/v5/cmd/podman/inspect"
56
"github.com/containers/podman/v5/cmd/podman/registry"
6-
"github.com/containers/podman/v5/cmd/podman/utils"
77
"github.com/containers/podman/v5/pkg/domain/entities"
88
"github.com/spf13/cobra"
99
)
@@ -13,11 +13,12 @@ var (
1313
Use: "inspect [ARTIFACT...]",
1414
Short: "Inspect an OCI artifact",
1515
Long: "Provide details on an OCI artifact",
16-
RunE: inspect,
16+
RunE: artifactInspect,
1717
Args: cobra.MinimumNArgs(1),
1818
ValidArgsFunction: common.AutocompleteArtifacts,
1919
Example: `podman artifact inspect quay.io/myimage/myartifact:latest`,
2020
}
21+
inspectOpts *entities.InspectOptions
2122
)
2223

2324
func init() {
@@ -26,25 +27,22 @@ func init() {
2627
Parent: artifactCmd,
2728
})
2829

29-
// TODO When things firm up on inspect looks, we can do a format implementation
30-
// flags := inspectCmd.Flags()
31-
// formatFlagName := "format"
32-
// flags.StringVar(&inspectFlag.format, formatFlagName, "", "Format volume output using JSON or a Go template")
30+
inspectOpts = new(entities.InspectOptions)
31+
32+
flags := inspectCmd.Flags()
33+
formatFlagName := "format"
34+
flags.StringVarP(&inspectOpts.Format, formatFlagName, "f", "json", "Format volume output using JSON or a Go template")
3335

3436
// This is something we wanted to do but did not seem important enough for initial PR
3537
// remoteFlagName := "remote"
3638
// flags.BoolVar(&inspectFlag.remote, remoteFlagName, false, "Inspect the image on a container image registry")
3739

3840
// TODO When the inspect structure has been defined, we need to uncomment and redirect this. Reminder, this
3941
// will also need to be reflected in the podman-artifact-inspect man page
40-
// _ = inspectCmd.RegisterFlagCompletionFunc(formatFlagName, common.AutocompleteFormat(&machine.InspectInfo{}))
42+
_ = inspectCmd.RegisterFlagCompletionFunc(formatFlagName, common.AutocompleteFormat(&entities.ArtifactInspectReport{}))
4143
}
4244

43-
func inspect(_ *cobra.Command, args []string) error {
44-
artifactOptions := entities.ArtifactInspectOptions{}
45-
inspectData, err := registry.ImageEngine().ArtifactInspect(registry.Context(), args[0], artifactOptions)
46-
if err != nil {
47-
return err
48-
}
49-
return utils.PrintGenericJSON(inspectData)
45+
func artifactInspect(_ *cobra.Command, args []string) error {
46+
inspectOpts.Type = common.ArtifactType
47+
return inspect.Inspect(args, *inspectOpts)
5048
}

docs/source/markdown/podman-artifact-inspect.1.md

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,60 @@ annotation using RFC3339Nano format, showing when the artifact was initially cre
2020

2121
## OPTIONS
2222

23-
#### **--help**
23+
#### **--format**, **-f**=*format*
2424

25-
Print usage statement.
25+
Format the output using the given Go template.
26+
The keys of the returned JSON can be used as the values for the --format flag (see examples below).
27+
28+
Valid placeholders for the Go template are listed below:
29+
30+
| **Placeholder** | **Description** |
31+
| ------------------------ | -------------------------------------------------- |
32+
| .Artifact ... | Artifact details (nested struct) |
33+
| .Digest | Artifact digest (sha256:+64-char hash) |
34+
| .Manifest ... | Artifact manifest details (struct) |
35+
| .Name | Artifact name (string) |
36+
| .TotalSizeBytes | Total Size of the artifact in bytes |
2637

2738
## EXAMPLES
2839

2940
Inspect an OCI image in the local store.
30-
```
41+
42+
```shell
3143
$ podman artifact inspect quay.io/myartifact/myml:latest
44+
[
45+
{
46+
"Manifest": {
47+
"schemaVersion": 2,
48+
"mediaType": "application/vnd.oci.image.manifest.v1+json",
49+
"config": {
50+
"mediaType": "application/vnd.oci.empty.v1+json",
51+
"digest": "sha256:44136fa355b3678a1146ad16f7e8649e94fb4fc21fe77e8310c060f61caaff8a",
52+
"size": 2,
53+
"data": "e30="
54+
},
55+
"layers": [
56+
{
57+
"mediaType": "text/plain; charset=utf-8",
58+
"digest": "sha256:f2ca1bb6c7e907d06dafe4687e579fce76b37e4e93b7605022da52e6ccc26fd2",
59+
"size": 5,
60+
"annotations": {
61+
"org.opencontainers.image.title": "foobar.txt"
62+
}
63+
}
64+
]
65+
},
66+
"Name": "quay.io/myartifact/mytxt:latest",
67+
"Digest": "sha256:6c28fa07a5b0a1cee29862c1f6ea38a66df982495b14da2c052427eb628ed8c6"
68+
}
69+
]
70+
```
71+
72+
Inspect artifact digest for the specified artifact:
73+
74+
```shell
75+
$ podman artifact inspect quay.io/myartifact/mytxt:latest --format {{.Digest}}
76+
sha256:6c28fa07a5b0a1cee29862c1f6ea38a66df982495b14da2c052427eb628ed8c6
3277
```
3378

3479
## SEE ALSO

0 commit comments

Comments
 (0)