This repository has been archived by the owner on Oct 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
delete.go
188 lines (179 loc) · 4.62 KB
/
delete.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package main
import (
"encoding/json"
"fmt"
"os"
"github.com/gepis/strge"
"github.com/gepis/strge/pkg/mflag"
)
var testDeleteImage = false
func deleteThing(flags *mflag.FlagSet, action string, m storage.Store, args []string) int {
if len(args) < 1 {
return 1
}
deleted := make(map[string]string)
for _, what := range args {
err := m.Delete(what)
if err != nil {
deleted[what] = err.Error()
} else {
deleted[what] = ""
}
}
if jsonOutput {
json.NewEncoder(os.Stdout).Encode(deleted)
} else {
for what, err := range deleted {
if err != "" {
fmt.Fprintf(os.Stderr, "%s: %s\n", what, err)
}
}
}
for _, err := range deleted {
if err != "" {
return 1
}
}
return 0
}
func deleteLayer(flags *mflag.FlagSet, action string, m storage.Store, args []string) int {
if len(args) < 1 {
return 1
}
deleted := make(map[string]string)
for _, what := range args {
err := m.DeleteLayer(what)
if err != nil {
deleted[what] = err.Error()
} else {
deleted[what] = ""
}
}
if jsonOutput {
json.NewEncoder(os.Stdout).Encode(deleted)
} else {
for what, err := range deleted {
if err != "" {
fmt.Fprintf(os.Stderr, "%s: %s\n", what, err)
}
}
}
for _, err := range deleted {
if err != "" {
return 1
}
}
return 0
}
type deletedImage struct {
DeletedLayers []string `json:"deleted-layers,omitifempty"`
Error string `json:"error,omitifempty"`
}
func deleteImage(flags *mflag.FlagSet, action string, m storage.Store, args []string) int {
if len(args) < 1 {
return 1
}
deleted := make(map[string]deletedImage)
for _, what := range args {
layers, err := m.DeleteImage(what, !testDeleteImage)
errText := ""
if err != nil {
errText = err.Error()
}
deleted[what] = deletedImage{
DeletedLayers: layers,
Error: errText,
}
}
if jsonOutput {
json.NewEncoder(os.Stdout).Encode(deleted)
} else {
for what, record := range deleted {
if record.Error != "" {
fmt.Fprintf(os.Stderr, "%s: %s\n", what, record.Error)
} else {
for _, layer := range record.DeletedLayers {
fmt.Fprintf(os.Stderr, "%s: %s\n", what, layer)
}
}
}
}
for _, record := range deleted {
if record.Error != "" {
return 1
}
}
return 0
}
func deleteContainer(flags *mflag.FlagSet, action string, m storage.Store, args []string) int {
if len(args) < 1 {
return 1
}
deleted := make(map[string]string)
for _, what := range args {
err := m.DeleteContainer(what)
if err != nil {
deleted[what] = err.Error()
} else {
deleted[what] = ""
}
}
if jsonOutput {
json.NewEncoder(os.Stdout).Encode(deleted)
} else {
for what, err := range deleted {
if err != "" {
fmt.Fprintf(os.Stderr, "%s: %s\n", what, err)
}
}
}
for _, err := range deleted {
if err != "" {
return 1
}
}
return 0
}
func init() {
commands = append(commands, command{
names: []string{"delete"},
optionsHelp: "[LayerOrImageOrContainerNameOrID [...]]",
usage: "Delete a layer or image or container, with no safety checks",
minArgs: 1,
action: deleteThing,
addFlags: func(flags *mflag.FlagSet, cmd *command) {
flags.BoolVar(&jsonOutput, []string{"-json", "j"}, jsonOutput, "Prefer JSON output")
},
})
commands = append(commands, command{
names: []string{"delete-layer", "deletelayer"},
optionsHelp: "[LayerNameOrID [...]]",
usage: "Delete a layer, with safety checks",
minArgs: 1,
action: deleteLayer,
addFlags: func(flags *mflag.FlagSet, cmd *command) {
flags.BoolVar(&jsonOutput, []string{"-json", "j"}, jsonOutput, "Prefer JSON output")
},
})
commands = append(commands, command{
names: []string{"delete-image", "deleteimage"},
optionsHelp: "[ImageNameOrID [...]]",
usage: "Delete an image, with safety checks",
minArgs: 1,
action: deleteImage,
addFlags: func(flags *mflag.FlagSet, cmd *command) {
flags.BoolVar(&testDeleteImage, []string{"-test", "t"}, jsonOutput, "Only test removal")
flags.BoolVar(&jsonOutput, []string{"-json", "j"}, jsonOutput, "Prefer JSON output")
},
})
commands = append(commands, command{
names: []string{"delete-container", "deletecontainer"},
optionsHelp: "[ContainerNameOrID [...]]",
usage: "Delete a container, with safety checks",
minArgs: 1,
action: deleteContainer,
addFlags: func(flags *mflag.FlagSet, cmd *command) {
flags.BoolVar(&jsonOutput, []string{"-json", "j"}, jsonOutput, "Prefer JSON output")
},
})
}