forked from peterfromthehill/nexus-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
executable file
·357 lines (336 loc) · 7.67 KB
/
main.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
package main
import (
"fmt"
"html/template"
"os"
"regexp"
"github.com/mlabouardy/nexus-cli/registry"
"github.com/urfave/cli"
)
const (
credentialTemplate = `# Nexus Credentials
nexus_host = "{{ .Host }}"
nexus_username = "{{ .Username }}"
nexus_password = "{{ .Password }}"
nexus_repository = "{{ .Repository }}"`
)
func main() {
app := cli.NewApp()
app.Name = "Nexus CLI"
app.Usage = "Manage Docker Private Registry on Nexus"
app.Version = "1.0.0-beta"
app.Authors = []cli.Author{
cli.Author{
Name: "Mohamed Labouardy",
Email: "[email protected]",
},
}
app.Flags = []cli.Flag{
cli.BoolTFlag{
Name: "silent",
Usage: "Computable output",
},
}
app.Commands = []cli.Command{
{
Name: "configure",
Usage: "Configure Nexus Credentials",
Action: func(c *cli.Context) error {
return setNexusCredentials(c)
},
},
{
Name: "image",
Usage: "Manage Docker Images",
Subcommands: []cli.Command{
{
Name: "ls",
Usage: "List all images in repository",
Flags: []cli.Flag{
cli.StringFlag{
Name: "regex-filter",
Value: ".*",
Usage: "Regex filter",
},
},
Action: func(c *cli.Context) error {
return listImages(c)
},
},
{
Name: "tags",
Usage: "Display all image tags",
Flags: []cli.Flag{
cli.StringFlag{
Name: "name, n",
Usage: "List tags by image name",
},
cli.StringFlag{
Name: "regex-filter",
Value: ".*",
Usage: "Regex filter",
},
},
Action: func(c *cli.Context) error {
return listTagsByImage(c)
},
},
{
Name: "info",
Usage: "Show image details",
Flags: []cli.Flag{
cli.StringFlag{
Name: "name, n",
},
cli.StringFlag{
Name: "tag, t",
},
},
Action: func(c *cli.Context) error {
return showImageInfo(c)
},
},
{
Name: "delete",
Usage: "Delete an image",
Flags: []cli.Flag{
cli.StringFlag{
Name: "name, n",
},
cli.StringFlag{
Name: "tag, t",
},
cli.StringFlag{
Name: "keep, k",
},
cli.StringFlag{
Name: "regex-filter",
Value: ".*",
Usage: "Regex filter",
},
cli.BoolTFlag{
Name: "dry",
},
},
Action: func(c *cli.Context) error {
return deleteImage(c)
},
},
{
Name: "size",
Usage: "Show total size of image including all tags",
Flags: []cli.Flag{
cli.StringFlag{
Name: "name, n",
},
},
Action: func(c *cli.Context) error {
return showTotalImageSize(c)
},
},
},
},
}
app.CommandNotFound = func(c *cli.Context, command string) {
fmt.Fprintf(c.App.Writer, "Wrong command %q !", command)
}
app.Run(os.Args)
}
func setNexusCredentials(c *cli.Context) error {
var hostname, repository, username, password string
fmt.Print("Enter Nexus Host: ")
fmt.Scan(&hostname)
fmt.Print("Enter Nexus Repository Name: ")
fmt.Scan(&repository)
fmt.Print("Enter Nexus Username: ")
fmt.Scan(&username)
fmt.Print("Enter Nexus Password: ")
fmt.Scan(&password)
data := struct {
Host string
Username string
Password string
Repository string
}{
hostname,
username,
password,
repository,
}
tmpl, err := template.New(".credentials").Parse(credentialTemplate)
if err != nil {
return cli.NewExitError(err.Error(), 1)
}
f, err := os.Create(".credentials")
if err != nil {
return cli.NewExitError(err.Error(), 1)
}
err = tmpl.Execute(f, data)
if err != nil {
return cli.NewExitError(err.Error(), 1)
}
return nil
}
func listImages(c *cli.Context) error {
r, err := registry.NewRegistry()
if err != nil {
return cli.NewExitError(err.Error(), 1)
}
images, err := r.ListImages()
if err != nil {
return cli.NewExitError(err.Error(), 1)
}
regexFilter := c.String("regex-filter")
for _, image := range images {
matched, err := regexp.MatchString(regexFilter, image)
if err != nil {
return cli.NewExitError(err.Error(), 1)
}
if matched {
fmt.Println(image)
}
}
if c.GlobalBoolT("silenter") {
fmt.Printf("Total images: %d\n", len(images))
}
return nil
}
func listTagsByImage(c *cli.Context) error {
var imgName = c.String("name")
r, err := registry.NewRegistry()
if err != nil {
return cli.NewExitError(err.Error(), 1)
}
if imgName == "" {
cli.ShowSubcommandHelp(c)
}
tags, err := r.ListTagsByImage(imgName)
Sort(tags)
if err != nil {
return cli.NewExitError(err.Error(), 1)
}
regexFilter := c.String("regex-filter")
for _, tag := range tags {
matched, err := regexp.MatchString(regexFilter, tag)
if err != nil {
return cli.NewExitError(err.Error(), 1)
}
if matched {
fmt.Println(tag)
}
}
if c.Bool("silent") {
fmt.Printf("There are %d images for %s\n", len(tags), imgName)
}
return nil
}
func showImageInfo(c *cli.Context) error {
var imgName = c.String("name")
var tag = c.String("tag")
r, err := registry.NewRegistry()
if err != nil {
return cli.NewExitError(err.Error(), 1)
}
if imgName == "" || tag == "" {
cli.ShowSubcommandHelp(c)
}
manifest, err := r.ImageManifest(imgName, tag)
if err != nil {
return cli.NewExitError(err.Error(), 1)
}
fmt.Printf("Image: %s:%s\n", imgName, tag)
fmt.Printf("Size: %d\n", manifest.Config.Size)
fmt.Println("Layers:")
for _, layer := range manifest.Layers {
fmt.Printf("\t%s\t%d\n", layer.Digest, layer.Size)
}
return nil
}
func deleteImage(c *cli.Context) error {
var imgName = c.String("name")
var tag = c.String("tag")
var keep = c.Int("keep")
if imgName == "" {
fmt.Fprintf(c.App.Writer, "You should specify the image name\n")
cli.ShowSubcommandHelp(c)
} else {
r, err := registry.NewRegistry()
if err != nil {
return cli.NewExitError(err.Error(), 1)
}
if tag == "" {
if keep == 0 {
fmt.Fprintf(c.App.Writer, "You should either specify the tag or how many images you want to keep\n")
cli.ShowSubcommandHelp(c)
} else {
tags, err := r.ListTagsByImage(imgName)
Sort(tags)
if err != nil {
return cli.NewExitError(err.Error(), 1)
}
regexFilter := c.String("regex-filter")
var filteredTags []string
for _, tag := range tags {
matched, err := regexp.MatchString(regexFilter, tag)
if err != nil {
return cli.NewExitError(err.Error(), 1)
}
if matched {
filteredTags = append(filteredTags, tag)
}
}
if len(filteredTags) >= keep {
for _, tag := range filteredTags[:len(filteredTags)-keep] {
if c.GlobalBool("silent") {
fmt.Printf("%s:%s image will be deleted ...\n", imgName, tag)
}
if !c.Bool("dry") {
r.DeleteImageByTag(imgName, tag)
}
}
} else {
fmt.Printf("Only %d images are available\n", len(filteredTags))
}
}
} else {
err = r.DeleteImageByTag(imgName, tag)
if err != nil {
return cli.NewExitError(err.Error(), 1)
}
}
}
return nil
}
func showTotalImageSize(c *cli.Context) error {
var imgName = c.String("name")
var totalSize (int64) = 0
if imgName == "" {
cli.ShowSubcommandHelp(c)
} else {
r, err := registry.NewRegistry()
if err != nil {
return cli.NewExitError(err.Error(), 1)
}
tags, err := r.ListTagsByImage(imgName)
if err != nil {
return cli.NewExitError(err.Error(), 1)
}
for _, tag := range tags {
manifest, err := r.ImageManifest(imgName, tag)
if err != nil {
return cli.NewExitError(err.Error(), 1)
}
sizeInfo := make(map[string]int64)
for _, layer := range manifest.Layers {
sizeInfo[layer.Digest] = layer.Size
}
for _, size := range sizeInfo {
totalSize += size
}
}
if c.Bool("silent") {
fmt.Printf("%d %s\n", totalSize, imgName)
}
}
return nil
}