mirror of
https://github.com/containers/podman.git
synced 2025-10-19 04:03:23 +08:00
Merge pull request #5115 from QiWang19/images-format
images --format compatible with docker
This commit is contained in:
@ -21,16 +21,16 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type imagesTemplateParams struct {
|
type imagesTemplateParams struct {
|
||||||
Repository string
|
Repository string
|
||||||
Tag string
|
Tag string
|
||||||
ID string
|
ID string
|
||||||
Digest digest.Digest
|
Digest digest.Digest
|
||||||
Digests []digest.Digest
|
Digests []digest.Digest
|
||||||
Created string
|
CreatedAt time.Time
|
||||||
CreatedTime time.Time
|
CreatedSince string
|
||||||
Size string
|
Size string
|
||||||
ReadOnly bool
|
ReadOnly bool
|
||||||
History string
|
History string
|
||||||
}
|
}
|
||||||
|
|
||||||
type imagesJSONParams struct {
|
type imagesJSONParams struct {
|
||||||
@ -65,7 +65,7 @@ func (a imagesSorted) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
|||||||
type imagesSortedCreated struct{ imagesSorted }
|
type imagesSortedCreated struct{ imagesSorted }
|
||||||
|
|
||||||
func (a imagesSortedCreated) Less(i, j int) bool {
|
func (a imagesSortedCreated) Less(i, j int) bool {
|
||||||
return a.imagesSorted[i].CreatedTime.After(a.imagesSorted[j].CreatedTime)
|
return a.imagesSorted[i].CreatedAt.After(a.imagesSorted[j].CreatedAt)
|
||||||
}
|
}
|
||||||
|
|
||||||
type imagesSortedID struct{ imagesSorted }
|
type imagesSortedID struct{ imagesSorted }
|
||||||
@ -185,7 +185,17 @@ func imagesCmd(c *cliconfig.ImagesValues) error {
|
|||||||
history: c.History,
|
history: c.History,
|
||||||
}
|
}
|
||||||
|
|
||||||
opts.outputformat = opts.setOutputFormat()
|
outputformat := opts.setOutputFormat()
|
||||||
|
// These fields were renamed, so we need to provide backward compat for
|
||||||
|
// the old names.
|
||||||
|
if strings.Contains(outputformat, "{{.Created}}") {
|
||||||
|
outputformat = strings.Replace(outputformat, "{{.Created}}", "{{.CreatedSince}}", -1)
|
||||||
|
}
|
||||||
|
if strings.Contains(outputformat, "{{.CreatedTime}}") {
|
||||||
|
outputformat = strings.Replace(outputformat, "{{.CreatedTime}}", "{{.CreatedAt}}", -1)
|
||||||
|
}
|
||||||
|
opts.outputformat = outputformat
|
||||||
|
|
||||||
filteredImages, err := runtime.GetFilteredImages(filters, false)
|
filteredImages, err := runtime.GetFilteredImages(filters, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrapf(err, "unable to get images")
|
return errors.Wrapf(err, "unable to get images")
|
||||||
@ -216,7 +226,7 @@ func (i imagesOptions) setOutputFormat() string {
|
|||||||
if i.digests {
|
if i.digests {
|
||||||
format += "{{.Digest}}\t"
|
format += "{{.Digest}}\t"
|
||||||
}
|
}
|
||||||
format += "{{.ID}}\t{{.Created}}\t{{.Size}}\t"
|
format += "{{.ID}}\t{{.CreatedSince}}\t{{.Size}}\t"
|
||||||
if i.history {
|
if i.history {
|
||||||
format += "{{if .History}}{{.History}}{{else}}<none>{{end}}\t"
|
format += "{{if .History}}{{.History}}{{else}}<none>{{end}}\t"
|
||||||
}
|
}
|
||||||
@ -301,16 +311,16 @@ func getImagesTemplateOutput(ctx context.Context, images []*adapter.ContainerIma
|
|||||||
imageDigest = img.Digest()
|
imageDigest = img.Digest()
|
||||||
}
|
}
|
||||||
params := imagesTemplateParams{
|
params := imagesTemplateParams{
|
||||||
Repository: repo,
|
Repository: repo,
|
||||||
Tag: tag,
|
Tag: tag,
|
||||||
ID: imageID,
|
ID: imageID,
|
||||||
Digest: imageDigest,
|
Digest: imageDigest,
|
||||||
Digests: img.Digests(),
|
Digests: img.Digests(),
|
||||||
CreatedTime: createdTime,
|
CreatedAt: createdTime,
|
||||||
Created: units.HumanDuration(time.Since(createdTime)) + " ago",
|
CreatedSince: units.HumanDuration(time.Since(createdTime)) + " ago",
|
||||||
Size: sizeStr,
|
Size: sizeStr,
|
||||||
ReadOnly: img.IsReadOnly(),
|
ReadOnly: img.IsReadOnly(),
|
||||||
History: strings.Join(img.NamesHistory(), ", "),
|
History: strings.Join(img.NamesHistory(), ", "),
|
||||||
}
|
}
|
||||||
imagesOutput = append(imagesOutput, params)
|
imagesOutput = append(imagesOutput, params)
|
||||||
if opts.quiet { // Show only one image ID when quiet
|
if opts.quiet { // Show only one image ID when quiet
|
||||||
@ -384,6 +394,9 @@ func GenImageOutputMap() map[string]string {
|
|||||||
values[key] = "R/O"
|
values[key] = "R/O"
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
if value == "CreatedSince" {
|
||||||
|
value = "created"
|
||||||
|
}
|
||||||
values[key] = strings.ToUpper(splitCamelCase(value))
|
values[key] = strings.ToUpper(splitCamelCase(value))
|
||||||
}
|
}
|
||||||
return values
|
return values
|
||||||
|
@ -51,6 +51,18 @@ Filter output based on conditions provided
|
|||||||
|
|
||||||
Change the default output format. This can be of a supported type like 'json'
|
Change the default output format. This can be of a supported type like 'json'
|
||||||
or a Go template.
|
or a Go template.
|
||||||
|
Valid placeholders for the Go template are listed below:
|
||||||
|
|
||||||
|
| **Placeholder** | **Description** |
|
||||||
|
| --------------- | ----------------------------------------------------------------------------- |
|
||||||
|
| .ID | Image ID |
|
||||||
|
| .Repository | Image repository |
|
||||||
|
| .Tag | Image tag |
|
||||||
|
| .Digest | Image digest |
|
||||||
|
| .CreatedSince | Elapsed time since the image was created |
|
||||||
|
| .CreatedAt | Time when the image was created |
|
||||||
|
| .Size | Size of layer on disk |
|
||||||
|
| .History | History of the image layer |
|
||||||
|
|
||||||
**--history**
|
**--history**
|
||||||
|
|
||||||
|
@ -116,7 +116,8 @@ var _ = Describe("Podman images", func() {
|
|||||||
})
|
})
|
||||||
|
|
||||||
It("podman images in GO template format", func() {
|
It("podman images in GO template format", func() {
|
||||||
session := podmanTest.Podman([]string{"images", "--format={{.ID}}"})
|
formatStr := "{{.ID}}\t{{.Created}}\t{{.CreatedAt}}\t{{.CreatedSince}}\t{{.CreatedTime}}"
|
||||||
|
session := podmanTest.Podman([]string{"images", fmt.Sprintf("--format=%s", formatStr)})
|
||||||
session.WaitWithDefaultTimeout()
|
session.WaitWithDefaultTimeout()
|
||||||
Expect(session.ExitCode()).To(Equal(0))
|
Expect(session.ExitCode()).To(Equal(0))
|
||||||
})
|
})
|
||||||
@ -280,7 +281,7 @@ RUN apk update && apk add man
|
|||||||
return session.OutputToStringArray()
|
return session.OutputToStringArray()
|
||||||
}
|
}
|
||||||
|
|
||||||
sortedArr := sortValueTest("created", 0, "CreatedTime")
|
sortedArr := sortValueTest("created", 0, "CreatedAt")
|
||||||
Expect(sort.SliceIsSorted(sortedArr, func(i, j int) bool { return sortedArr[i] > sortedArr[j] })).To(BeTrue())
|
Expect(sort.SliceIsSorted(sortedArr, func(i, j int) bool { return sortedArr[i] > sortedArr[j] })).To(BeTrue())
|
||||||
|
|
||||||
sortedArr = sortValueTest("id", 0, "ID")
|
sortedArr = sortValueTest("id", 0, "ID")
|
||||||
|
Reference in New Issue
Block a user