-
Notifications
You must be signed in to change notification settings - Fork 121
/
Copy pathmain.go
332 lines (308 loc) · 7.77 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
package main
import (
"fmt"
"html/template"
"log"
"os"
"regexp"
"strings"
"github.com/moepi/nexus-cli/registry"
"github.com/urfave/cli"
)
const (
credentialsTemplates = `# 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-2"
app.Authors = []cli.Author{
cli.Author{
Name: "Mohamed Labouardy",
Email: "[email protected]",
},
}
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",
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.StringSliceFlag{
Name: "expression, e",
Usage: "Filter tags by regular expression",
},
cli.BoolFlag{
Name: "invert, v",
Usage: "Invert filter results",
},
},
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 images",
Flags: []cli.Flag{
cli.StringFlag{
Name: "name, n",
},
cli.StringFlag{
Name: "tag, t",
},
cli.StringFlag{
Name: "keep, k",
},
cli.StringSliceFlag{
Name: "expression, e",
Usage: "Filter tags by regular expression",
},
cli.BoolFlag{
Name: "invert, v",
Usage: "Invert results filter expressions",
},
},
Action: func(c *cli.Context) error {
return deleteImages(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(credentialsTemplates)
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)
}
for _, image := range images {
fmt.Println(image)
}
fmt.Printf("Total images: %d\n", len(images))
return nil
}
func filterTagsByRegex(tags []string, expressions []string, invert bool) ([]string, error) {
var retTags []string
if len(expressions) == 0 {
return tags, nil
}
for _, tag := range tags {
tagMiss := false
for _, expression := range expressions {
var expressionBool = !invert
if strings.HasPrefix(expression, "!") {
expressionBool = invert
expression = strings.Trim(expression, "!")
}
retVal, err := regexp.MatchString(expression, tag)
if err != nil {
return retTags, err
}
if retVal != expressionBool {
tagMiss = true
break
}
}
// tag must match all expression, so continue with next tag on match
if !tagMiss {
retTags = append(retTags, tag)
}
}
return retTags, 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)
// filter tags by expressions
tags, err = filterTagsByRegex(tags, c.StringSlice("expression"), c.Bool("invert"))
if err != nil {
log.Fatal(err)
}
compareStringNumber := func(str1, str2 string) bool {
return extractNumberFromString(str1) < extractNumberFromString(str2)
}
Compare(compareStringNumber).Sort(tags)
if err != nil {
return cli.NewExitError(err.Error(), 1)
}
for _, tag := range tags {
fmt.Println(tag)
}
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 deleteImages(c *cli.Context) error {
var imgName = c.String("name")
var tag = c.String("tag")
var keep = c.Int("keep")
var invert = c.Bool("invert")
// Show help if no image name is present
if imgName == "" {
fmt.Fprintf(c.App.Writer, "You should specify the image name\n")
cli.ShowSubcommandHelp(c)
return nil
}
r, err := registry.NewRegistry()
if err != nil {
return cli.NewExitError(err.Error(), 1)
}
// if a specific tag is provided, ignore all other options
if tag != "" {
err = r.DeleteImageByTag(imgName, tag)
if err != nil {
return cli.NewExitError(err.Error(), 1)
}
return nil
}
// Get list of tags and filter them by all expressions provided
tags, err := r.ListTagsByImage(imgName)
tags, err = filterTagsByRegex(tags, c.StringSlice("expression"), invert)
if err != nil {
fmt.Fprintf(c.App.Writer, "Could not filter tags by regular expressions: %s\n", err)
return err
}
// if no keep is specified, all flags are unset. Show help and exit.
if c.IsSet("keep") == false && len(c.StringSlice("expression")) == 0 {
fmt.Fprintf(c.App.Writer, "You should either specify use tag / filter expressions, or specify how many images you want to keep\n")
cli.ShowSubcommandHelp(c)
return fmt.Errorf("You should either specify use tag / filter expressions, or specify how many images you want to keep")
}
if len(tags) == 0 && !c.IsSet("keep") {
fmt.Fprintf(c.App.Writer, "No images selected for deletion\n")
return fmt.Errorf("No images selected for deletion")
}
// Remove images by using keep flag
compareStringNumber := func(str1, str2 string) bool {
return extractNumberFromString(str1) < extractNumberFromString(str2)
}
Compare(compareStringNumber).Sort(tags)
if err != nil {
return cli.NewExitError(err.Error(), 1)
}
if len(tags) >= keep {
for _, tag := range tags[:len(tags)-keep] {
fmt.Printf("%s:%s image will be deleted ...\n", imgName, tag)
err = r.DeleteImageByTag(imgName, tag)
if err != nil {
return cli.NewExitError(err.Error(), 1)
}
}
} else {
fmt.Printf("Only %d images are available\n", len(tags))
}
return nil
}