-
Notifications
You must be signed in to change notification settings - Fork 121
/
main.go
302 lines (280 loc) · 6.62 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
package main
import (
"fmt"
"html/template"
"os"
"github.com/mlabouardy/nexus-cli/registry"
"github.com/urfave/cli"
)
const (
CREDENTIALS_TEMPLATES = `# 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.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",
},
},
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",
},
},
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(CREDENTIALS_TEMPLATES)
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 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)
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 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)
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)
r.DeleteImageByTag(imgName, tag)
}
} else {
fmt.Printf("Only %d images are available\n", len(tags))
}
}
} 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
}
}
fmt.Printf("%d %s\n", totalSize, imgName)
}
return nil
}