Skip to content

Commit f9001d1

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 1671029 commit f9001d1

File tree

4 files changed

+67
-17
lines changed

4 files changed

+67
-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(cmd *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
}

cmd/podman/common/inspect.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,6 @@ const (
1313
PodType = "pod"
1414
// VolumeType is the volume type
1515
VolumeType = "volume"
16+
// ArtificatType is the artifact type
17+
ArtifactType = "artifact"
1618
)

cmd/podman/inspect/inspect.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,13 @@ func (i *inspector) inspect(namesOrIDs []string) error {
151151
for i := range volumeData {
152152
data = append(data, volumeData[i])
153153
}
154+
case common.ArtifactType:
155+
artifactOptions := entities.ArtifactInspectOptions{}
156+
inspectData, err := i.imageEngine.ArtifactInspect(ctx, namesOrIDs[0], artifactOptions)
157+
if err != nil {
158+
return err
159+
}
160+
data = append(data, inspectData)
154161
default:
155162
return fmt.Errorf("invalid type %q: must be %q, %q, %q, %q, %q, or %q", i.options.Type,
156163
common.ImageType, common.ContainerType, common.PodType, common.NetworkType, common.VolumeType, common.AllType)

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

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,58 @@ Inspect an artifact in the local store. The artifact can be referred to with ei
1515

1616
## OPTIONS
1717

18-
#### **--help**
18+
#### **--format**, **-f**=*format*
1919

20-
Print usage statement.
20+
Format the output using the given Go template.
21+
The keys of the returned JSON can be used as the values for the --format flag (see examples below).
22+
23+
Valid placeholders for the Go template are listed below:
24+
25+
| **Placeholder** | **Description** |
26+
| ------------------------ | -------------------------------------------------- |
27+
|.Manifest | Artifact manifest details (struct) |
28+
|.Name | Artifact name (string) |
29+
|.Digest | Artifact digest (sha256:+64-char hash) |
2130

2231
## EXAMPLES
2332

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

2972
## SEE ALSO

0 commit comments

Comments
 (0)