-
Notifications
You must be signed in to change notification settings - Fork 232
/
exists.go
78 lines (73 loc) · 1.91 KB
/
exists.go
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
package main
import (
"fmt"
"github.com/containers/storage"
"github.com/containers/storage/pkg/mflag"
)
var (
existLayer = false
existImage = false
existContainer = false
existQuiet = false
)
func exist(flags *mflag.FlagSet, action string, m storage.Store, args []string) (int, error) {
if len(args) < 1 {
return 1, nil
}
anyMissing := false
existDict := make(map[string]bool)
for _, what := range args {
exists := m.Exists(what)
existDict[what] = exists
if existContainer {
if c, err := m.Container(what); c == nil || err != nil {
exists = false
}
}
if existImage {
if i, err := m.Image(what); i == nil || err != nil {
exists = false
}
}
if existLayer {
if l, err := m.Layer(what); l == nil || err != nil {
exists = false
}
}
if !exists {
anyMissing = true
}
}
if jsonOutput {
if _, err := outputJSON(existDict); err != nil {
return 1, err
}
} else {
if !existQuiet {
for what, exists := range existDict {
fmt.Printf("%s: %v\n", what, exists)
}
}
}
if anyMissing {
return 1, nil
}
return 0, nil
}
func init() {
commands = append(commands, command{
names: []string{"exists"},
optionsHelp: "[LayerOrImageOrContainerNameOrID [...]]",
usage: "Check if a layer or image or container exists",
minArgs: 1,
maxArgs: -1,
action: exist,
addFlags: func(flags *mflag.FlagSet, cmd *command) {
flags.BoolVar(&existQuiet, []string{"-quiet", "q"}, existQuiet, "Don't print names")
flags.BoolVar(&existLayer, []string{"-layer", "l"}, existQuiet, "Only succeed if the match is a layer")
flags.BoolVar(&existImage, []string{"-image", "i"}, existQuiet, "Only succeed if the match is an image")
flags.BoolVar(&existContainer, []string{"-container", "c"}, existQuiet, "Only succeed if the match is a container")
flags.BoolVar(&jsonOutput, []string{"-json", "j"}, jsonOutput, "Prefer JSON output")
},
})
}