File: images.go

package info (click to toggle)
golang-github-containers-storage 1.59.1%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 4,184 kB
  • sloc: sh: 630; ansic: 389; makefile: 143; awk: 12
file content (98 lines) | stat: -rw-r--r-- 2,471 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package main

import (
	"fmt"

	"github.com/containers/storage"
	"github.com/containers/storage/pkg/mflag"
	digest "github.com/opencontainers/go-digest"
)

var imagesQuiet = false

func images(flags *mflag.FlagSet, action string, m storage.Store, args []string) (int, error) {
	images, err := m.Images()
	if err != nil {
		return 1, err
	}
	if jsonOutput {
		return outputJSON(images)
	}
	for _, image := range images {
		fmt.Printf("%s\n", image.ID)
		if imagesQuiet {
			continue
		}
		for _, name := range image.Names {
			fmt.Printf("\tname: %s\n", name)
		}
		for _, digest := range image.Digests {
			fmt.Printf("\tdigest: %s\n", digest.String())
		}
		for _, name := range image.BigDataNames {
			fmt.Printf("\tdata: %s\n", name)
		}
	}
	return 0, nil
}

func imagesByDigest(flags *mflag.FlagSet, action string, m storage.Store, args []string) (int, error) {
	images := []*storage.Image{}
	for _, arg := range args {
		d := digest.Digest(arg)
		if err := d.Validate(); err != nil {
			return 1, err
		}
		matched, err := m.ImagesByDigest(d)
		if err != nil {
			return 1, err
		}
		images = append(images, matched...)
	}
	if jsonOutput {
		return outputJSON(images)
	}
	for _, image := range images {
		fmt.Printf("%s\n", image.ID)
		if imagesQuiet {
			continue
		}
		for _, name := range image.Names {
			fmt.Printf("\tname: %s\n", name)
		}
		for _, digest := range image.Digests {
			fmt.Printf("\tdigest: %s\n", digest.String())
		}
		for _, name := range image.BigDataNames {
			fmt.Printf("\tdata: %s\n", name)
		}
	}
	return 0, nil
}

func init() {
	commands = append(commands, command{
		names:       []string{"images"},
		optionsHelp: "[options [...]]",
		usage:       "List images",
		action:      images,
		minArgs:     0,
		maxArgs:     0,
		addFlags: func(flags *mflag.FlagSet, cmd *command) {
			flags.BoolVar(&jsonOutput, []string{"-json", "j"}, jsonOutput, "Prefer JSON output")
			flags.BoolVar(&imagesQuiet, []string{"-quiet", "q"}, imagesQuiet, "Only print IDs")
		},
	})
	commands = append(commands, command{
		names:       []string{"images-by-digest"},
		optionsHelp: "[options [...]] DIGEST",
		usage:       "List images by digest",
		action:      imagesByDigest,
		minArgs:     1,
		maxArgs:     1,
		addFlags: func(flags *mflag.FlagSet, cmd *command) {
			flags.BoolVar(&jsonOutput, []string{"-json", "j"}, jsonOutput, "Prefer JSON output")
			flags.BoolVar(&imagesQuiet, []string{"-quiet", "q"}, imagesQuiet, "Only print IDs")
		},
	})
}