-
Notifications
You must be signed in to change notification settings - Fork 937
/
Copy pathimage_alias.go
325 lines (255 loc) · 7.37 KB
/
image_alias.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
package main
import (
"errors"
"sort"
"strings"
"github.com/spf13/cobra"
"github.com/canonical/lxd/shared/api"
cli "github.com/canonical/lxd/shared/cmd"
"github.com/canonical/lxd/shared/i18n"
)
type cmdImageAlias struct {
global *cmdGlobal
image *cmdImage
}
func (c *cmdImageAlias) command() *cobra.Command {
cmd := &cobra.Command{}
cmd.Use = usage("alias")
cmd.Short = i18n.G("Manage image aliases")
cmd.Long = cli.FormatSection(i18n.G("Description"), i18n.G(
`Manage image aliases`))
// Create
imageAliasCreateCmd := cmdImageAliasCreate{global: c.global, image: c.image, imageAlias: c}
cmd.AddCommand(imageAliasCreateCmd.command())
// Delete
imageAliasDeleteCmd := cmdImageAliasDelete{global: c.global, image: c.image, imageAlias: c}
cmd.AddCommand(imageAliasDeleteCmd.command())
// List
imageAliasListCmd := cmdImageAliasList{global: c.global, image: c.image, imageAlias: c}
cmd.AddCommand(imageAliasListCmd.command())
// Rename
imageAliasRenameCmd := cmdImageAliasRename{global: c.global, image: c.image, imageAlias: c}
cmd.AddCommand(imageAliasRenameCmd.command())
// Workaround for subcommand usage errors. See: https://github.com/spf13/cobra/issues/706
cmd.Args = cobra.NoArgs
cmd.Run = func(cmd *cobra.Command, args []string) { _ = cmd.Usage() }
return cmd
}
// Create.
type cmdImageAliasCreate struct {
global *cmdGlobal
image *cmdImage
imageAlias *cmdImageAlias
}
func (c *cmdImageAliasCreate) command() *cobra.Command {
cmd := &cobra.Command{}
cmd.Use = usage("create", i18n.G("[<remote>:]<alias> <fingerprint>"))
cmd.Short = i18n.G("Create aliases for existing images")
cmd.Long = cli.FormatSection(i18n.G("Description"), i18n.G(
`Create aliases for existing images`))
cmd.RunE = c.run
cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) > 1 {
return nil, cobra.ShellCompDirectiveNoFileComp
}
if len(args) == 0 {
return c.global.cmpRemotes(toComplete, true)
}
remote, _, found := strings.Cut(args[0], ":")
if !found {
remote = ""
}
return c.global.cmpImageFingerprintsFromRemote(toComplete, remote)
}
return cmd
}
func (c *cmdImageAliasCreate) run(cmd *cobra.Command, args []string) error {
// Quick checks.
exit, err := c.global.CheckArgs(cmd, args, 2, 2)
if exit {
return err
}
// Parse remote
resources, err := c.global.ParseServers(args[0])
if err != nil {
return err
}
resource := resources[0]
if resource.name == "" {
return errors.New(i18n.G("Alias name missing"))
}
// Create the alias
alias := api.ImageAliasesPost{}
alias.Name = resource.name
alias.Target = args[1]
return resource.server.CreateImageAlias(alias)
}
// Delete.
type cmdImageAliasDelete struct {
global *cmdGlobal
image *cmdImage
imageAlias *cmdImageAlias
}
func (c *cmdImageAliasDelete) command() *cobra.Command {
cmd := &cobra.Command{}
cmd.Use = usage("delete", i18n.G("[<remote>:]<alias>"))
cmd.Aliases = []string{"rm"}
cmd.Short = i18n.G("Delete image aliases")
cmd.Long = cli.FormatSection(i18n.G("Description"), i18n.G(
`Delete image aliases`))
cmd.RunE = c.run
cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) > 0 {
return nil, cobra.ShellCompDirectiveNoFileComp
}
return c.global.cmpImages(toComplete)
}
return cmd
}
func (c *cmdImageAliasDelete) run(cmd *cobra.Command, args []string) error {
// Quick checks.
exit, err := c.global.CheckArgs(cmd, args, 1, 1)
if exit {
return err
}
// Parse remote
resources, err := c.global.ParseServers(args[0])
if err != nil {
return err
}
resource := resources[0]
if resource.name == "" {
return errors.New(i18n.G("Alias name missing"))
}
// Delete the alias
return resource.server.DeleteImageAlias(resource.name)
}
// List.
type cmdImageAliasList struct {
global *cmdGlobal
image *cmdImage
imageAlias *cmdImageAlias
flagFormat string
}
func (c *cmdImageAliasList) command() *cobra.Command {
cmd := &cobra.Command{}
cmd.Use = usage("list", i18n.G("[<remote>:] [<filters>...]"))
cmd.Aliases = []string{"ls"}
cmd.Short = i18n.G("List image aliases")
cmd.Long = cli.FormatSection(i18n.G("Description"), i18n.G(
`List image aliases
Filters may be part of the image hash or part of the image alias name.
`))
cmd.Flags().StringVarP(&c.flagFormat, "format", "f", "table", i18n.G("Format (csv|json|table|yaml|compact)")+"``")
cmd.RunE = c.run
cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) > 0 {
return nil, cobra.ShellCompDirectiveNoFileComp
}
return c.global.cmpRemotes(toComplete, true)
}
return cmd
}
func (c *cmdImageAliasList) aliasShouldShow(filters []string, state *api.ImageAliasesEntry) bool {
if len(filters) == 0 {
return true
}
for _, filter := range filters {
if strings.Contains(state.Name, filter) || strings.Contains(state.Target, filter) {
return true
}
}
return false
}
func (c *cmdImageAliasList) run(cmd *cobra.Command, args []string) error {
// Quick checks.
exit, err := c.global.CheckArgs(cmd, args, 0, -1)
if exit {
return err
}
// Parse remote
remote := ""
if len(args) > 0 {
remote = args[0]
}
remoteName, name, err := c.global.conf.ParseRemote(remote)
if err != nil {
return err
}
remoteServer, err := c.global.conf.GetImageServer(remoteName)
if err != nil {
return err
}
// Process the filters
filters := []string{}
if name != "" {
filters = append(filters, name)
}
if len(args) > 1 {
filters = append(filters, args[1:]...)
}
// List the aliases
aliases, err := remoteServer.GetImageAliases()
if err != nil {
return err
}
// Render the table
data := [][]string{}
for _, alias := range aliases {
if !c.aliasShouldShow(filters, &alias) {
continue
}
if alias.Type == "" {
alias.Type = "container"
}
data = append(data, []string{alias.Name, alias.Target[0:12], strings.ToUpper(alias.Type), alias.Description})
}
sort.Sort(cli.StringList(data))
header := []string{
i18n.G("ALIAS"),
i18n.G("FINGERPRINT"),
i18n.G("TYPE"),
i18n.G("DESCRIPTION"),
}
return cli.RenderTable(c.flagFormat, header, data, aliases)
}
// Rename.
type cmdImageAliasRename struct {
global *cmdGlobal
image *cmdImage
imageAlias *cmdImageAlias
}
func (c *cmdImageAliasRename) command() *cobra.Command {
cmd := &cobra.Command{}
cmd.Use = usage("rename", i18n.G("[<remote>:]<alias> <new-name>"))
cmd.Aliases = []string{"mv"}
cmd.Short = i18n.G("Rename aliases")
cmd.Long = cli.FormatSection(i18n.G("Description"), i18n.G(
`Rename aliases`))
cmd.RunE = c.run
cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) > 0 {
return nil, cobra.ShellCompDirectiveNoFileComp
}
return c.global.cmpImages(toComplete)
}
return cmd
}
func (c *cmdImageAliasRename) run(cmd *cobra.Command, args []string) error {
// Quick checks.
exit, err := c.global.CheckArgs(cmd, args, 2, 2)
if exit {
return err
}
// Parse remote
resources, err := c.global.ParseServers(args[0])
if err != nil {
return err
}
resource := resources[0]
if resource.name == "" {
return errors.New(i18n.G("Alias name missing"))
}
// Rename the alias
return resource.server.RenameImageAlias(resource.name, api.ImageAliasesEntryPost{Name: args[1]})
}