diff --git a/cmd/podman/artifact/list.go b/cmd/podman/artifact/list.go index dbb7cc2aad1..2d943006b16 100644 --- a/cmd/podman/artifact/list.go +++ b/cmd/podman/artifact/list.go @@ -3,12 +3,14 @@ package artifact import ( "fmt" "os" + "time" "github.com/containers/podman/v5/cmd/podman/common" "github.com/containers/podman/v5/cmd/podman/registry" "github.com/containers/podman/v5/cmd/podman/validate" "github.com/containers/podman/v5/pkg/domain/entities" "github.com/docker/go-units" + imgspec "github.com/opencontainers/image-spec/specs-go/v1" "github.com/spf13/cobra" "go.podman.io/common/pkg/completion" "go.podman.io/common/pkg/report" @@ -36,6 +38,7 @@ type listFlagType struct { } type artifactListOutput struct { + Created string Digest string Repository string Size string @@ -44,7 +47,7 @@ type artifactListOutput struct { } var ( - defaultArtifactListOutputFormat = "{{range .}}{{.Repository}}\t{{.Tag}}\t{{.Digest}}\t{{.Size}}\n{{end -}}" + defaultArtifactListOutputFormat = "{{range .}}{{.Repository}}\t{{.Tag}}\t{{.Digest}}\t{{.Created}}\t{{.Size}}\n{{end -}}" ) func init() { @@ -106,7 +109,18 @@ func outputTemplate(cmd *cobra.Command, lrs []*entities.ArtifactListReport) erro artifactHash = artifactDigest.Encoded() } + // Get the created time from the manifest annotations + created := "" + if lr.Artifact.Manifest != nil && lr.Artifact.Manifest.Annotations != nil { + if createdStr, ok := lr.Artifact.Manifest.Annotations[imgspec.AnnotationCreated]; ok { + if createdTime, err := time.Parse(time.RFC3339Nano, createdStr); err == nil { + created = units.HumanDuration(time.Since(createdTime)) + " ago" + } + } + } + artifacts = append(artifacts, artifactListOutput{ + Created: created, Digest: artifactHash, Repository: named.Name(), Size: units.HumanSize(float64(lr.Artifact.TotalSizeBytes())), @@ -116,10 +130,11 @@ func outputTemplate(cmd *cobra.Command, lrs []*entities.ArtifactListReport) erro } headers := report.Headers(artifactListOutput{}, map[string]string{ - "REPOSITORY": "REPOSITORY", - "Tag": "TAG", - "Size": "SIZE", + "Created": "CREATED", "Digest": "DIGEST", + "Repository": "REPOSITORY", + "Size": "SIZE", + "Tag": "TAG", }) rpt := report.New(os.Stdout, cmd.Name()) diff --git a/docs/source/markdown/podman-artifact-ls.1.md.in b/docs/source/markdown/podman-artifact-ls.1.md.in index a2590d7334d..0c99efa3ef5 100644 --- a/docs/source/markdown/podman-artifact-ls.1.md.in +++ b/docs/source/markdown/podman-artifact-ls.1.md.in @@ -18,6 +18,7 @@ Print results with a Go template. | **Placeholder** | **Description** | |-----------------|------------------------------------------------| +| .Created | Artifact creation time in human readable units | | .Digest | The computed digest of the artifact's manifest | | .Repository | Repository name of the artifact | | .Size | Size artifact in human readable units | @@ -33,24 +34,24 @@ Print results with a Go template. List artifacts in the local store ``` $ podman artifact ls -REPOSITORY TAG DIGEST SIZE -quay.io/artifact/foobar1 latest ab609fad386d 2.097GB -quay.io/artifact/foobar2 special cd734b558ceb 12.58MB +REPOSITORY TAG DIGEST CREATED SIZE +quay.io/artifact/foobar1 latest ab609fad386d 3 weeks ago 2.097GB +quay.io/artifact/foobar2 special cd734b558ceb 2 days ago 12.58MB ``` List artifacts in the local store without truncating the digest ``` $ podman artifact ls --no-trunc -REPOSITORY TAG DIGEST SIZE -quay.io/artifact/foobar1 latest ab609fad386df1433f461b0643d9cf575560baf633809dcc9c190da6cc3a3c29 2.097GB -quay.io/artifact/foobar2 special cd734b558ceb8ccc0281ca76530e1dea1eb479407d3163f75fb601bffb6f73d0 12.58MB +REPOSITORY TAG DIGEST CREATED SIZE +quay.io/artifact/foobar1 latest ab609fad386df1433f461b0643d9cf575560baf633809dcc9c190da6cc3a3c29 3 weeks ago 2.097GB +quay.io/artifact/foobar2 special cd734b558ceb8ccc0281ca76530e1dea1eb479407d3163f75fb601bffb6f73d0 2 days ago 12.58MB ``` List artifacts in the local store without the title header ``` $ podman artifact ls --noheading -quay.io/artifact/foobar1 latest ab609fad386d 2.097GB -quay.io/artifact/foobar2 special cd734b558ceb 12.58MB +quay.io/artifact/foobar1 latest ab609fad386d 3 weeks ago 2.097GB +quay.io/artifact/foobar2 special cd734b558ceb 2 days ago 12.58MB ``` List artifact digests and size using a --format diff --git a/test/e2e/artifact_test.go b/test/e2e/artifact_test.go index cf59e3f3c5d..e231cf4c95e 100644 --- a/test/e2e/artifact_test.go +++ b/test/e2e/artifact_test.go @@ -78,6 +78,19 @@ var _ = Describe("Podman artifact", func() { // Verify if the virtual size values are present in the output Expect(virtualSizes).To(ContainElement("4192")) Expect(virtualSizes).To(ContainElement("10240")) + + // Check if .Created is reported and is not "" + createdFormatSession := podmanTest.PodmanExitCleanly("artifact", "ls", "--format", "{{.Created}}") + createdTimes := createdFormatSession.OutputToStringArray() + + // Should list 2 lines (without the header) + Expect(createdTimes).To(HaveLen(2)) + + // Verify the created times are not "" and end with " ago" + for _, created := range createdTimes { + Expect(created).ToNot(Equal("")) + Expect(created).To(HaveSuffix(" ago")) + } }) It("podman artifact simple add", func() {