-
Notifications
You must be signed in to change notification settings - Fork 937
/
Copy pathalias.go
247 lines (198 loc) · 7.03 KB
/
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
package main
import (
"fmt"
"sort"
"github.com/spf13/cobra"
cli "github.com/canonical/lxd/shared/cmd"
"github.com/canonical/lxd/shared/i18n"
)
type cmdAlias struct {
global *cmdGlobal
}
// Command is a method of the cmdAlias structure that returns a new cobra Command for managing command aliases.
// This includes commands for adding, listing, renaming, and removing aliases, along with their usage and descriptions.
func (c *cmdAlias) command() *cobra.Command {
cmd := &cobra.Command{}
cmd.Use = usage("alias")
cmd.Short = i18n.G("Manage command aliases")
cmd.Long = cli.FormatSection(i18n.G("Description"), i18n.G(
`Manage command aliases`))
// Add
aliasAddCmd := cmdAliasAdd{global: c.global, alias: c}
cmd.AddCommand(aliasAddCmd.command())
// List
aliasListCmd := cmdAliasList{global: c.global, alias: c}
cmd.AddCommand(aliasListCmd.command())
// Rename
aliasRenameCmd := cmdAliasRename{global: c.global, alias: c}
cmd.AddCommand(aliasRenameCmd.command())
// Remove
aliasRemoveCmd := cmdAliasRemove{global: c.global, alias: c}
cmd.AddCommand(aliasRemoveCmd.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
}
// Add.
type cmdAliasAdd struct {
global *cmdGlobal
alias *cmdAlias
}
// Command is a method of the cmdAliasAdd structure that returns a new cobra Command for adding new command aliases.
// It specifies the command usage, description, and examples, and links it to the RunE method for execution logic.
func (c *cmdAliasAdd) command() *cobra.Command {
cmd := &cobra.Command{}
cmd.Use = usage("add", i18n.G("<alias> <target>"))
cmd.Short = i18n.G("Add new aliases")
cmd.Long = cli.FormatSection(i18n.G("Description"), i18n.G(
`Add new aliases`))
cmd.Example = cli.FormatSection("", i18n.G(
`lxc alias add list "list -c ns46S"
Overwrite the "list" command to pass -c ns46S.`))
cmd.RunE = c.run
return cmd
}
// Run is a method of the cmdAliasAdd structure. It implements the logic to add a new alias command.
// The function checks for valid arguments, verifies if the alias already exists, and if not, adds the new alias to the configuration.
func (c *cmdAliasAdd) run(cmd *cobra.Command, args []string) error {
conf := c.global.conf
// Quick checks.
exit, err := c.global.CheckArgs(cmd, args, 2, 2)
if exit {
return err
}
// Look for an existing alias
_, ok := conf.Aliases[args[0]]
if ok {
return fmt.Errorf(i18n.G("Alias %s already exists"), args[0])
}
// Add the new alias
conf.Aliases[args[0]] = args[1]
// Save the config
return conf.SaveConfig(c.global.confPath)
}
// List.
type cmdAliasList struct {
global *cmdGlobal
alias *cmdAlias
flagFormat string
}
// Command is a method of the cmdAliasList structure that returns a new cobra Command for listing command aliases.
// It specifies the command usage, description, aliases, and output formatting options, and links it to the RunE method for execution logic.
func (c *cmdAliasList) command() *cobra.Command {
cmd := &cobra.Command{}
cmd.Use = usage("list")
cmd.Aliases = []string{"ls"}
cmd.Short = i18n.G("List aliases")
cmd.Long = cli.FormatSection(i18n.G("Description"), i18n.G(
`List aliases`))
cmd.Flags().StringVarP(&c.flagFormat, "format", "f", "table", i18n.G("Format (csv|json|table|yaml|compact)")+"``")
cmd.RunE = c.run
return cmd
}
// Run is a method of the cmdAliasList structure. It implements the logic to list existing command aliases.
// The function checks for valid arguments, collects all the aliases, sorts them, and renders them in the specified format.
func (c *cmdAliasList) run(cmd *cobra.Command, args []string) error {
conf := c.global.conf
// Quick checks.
exit, err := c.global.CheckArgs(cmd, args, 0, 0)
if exit {
return err
}
// List the aliases
data := [][]string{}
for k, v := range conf.Aliases {
data = append(data, []string{k, v})
}
sort.Sort(cli.SortColumnsNaturally(data))
header := []string{
i18n.G("ALIAS"),
i18n.G("TARGET"),
}
return cli.RenderTable(c.flagFormat, header, data, conf.Aliases)
}
// Rename.
type cmdAliasRename struct {
global *cmdGlobal
alias *cmdAlias
}
// Command is a method of the cmdAliasRename structure. It returns a new cobra.Command object.
// This command allows a user to rename existing aliases in the CLI application.
func (c *cmdAliasRename) command() *cobra.Command {
cmd := &cobra.Command{}
cmd.Use = usage("rename", i18n.G("<old alias> <new alias>"))
cmd.Aliases = []string{"mv"}
cmd.Short = i18n.G("Rename aliases")
cmd.Long = cli.FormatSection(i18n.G("Description"), i18n.G(
`Rename aliases`))
cmd.Example = cli.FormatSection("", i18n.G(
`lxc alias rename list my-list
Rename existing alias "list" to "my-list".`))
cmd.RunE = c.run
return cmd
}
// Run is a method of the cmdAliasRename structure. It takes a cobra command and a slice of strings as arguments.
// This method checks the validity of arguments, ensures the existence of the old alias, verifies the non-existence of the new alias, and then proceeds to rename the alias in the configuration.
func (c *cmdAliasRename) run(cmd *cobra.Command, args []string) error {
conf := c.global.conf
// Quick checks.
exit, err := c.global.CheckArgs(cmd, args, 2, 2)
if exit {
return err
}
// Check for the existing alias
target, ok := conf.Aliases[args[0]]
if !ok {
return fmt.Errorf(i18n.G("Alias %s doesn't exist"), args[0])
}
// Check for the new alias
_, ok = conf.Aliases[args[1]]
if ok {
return fmt.Errorf(i18n.G("Alias %s already exists"), args[1])
}
// Rename the alias
conf.Aliases[args[1]] = target
delete(conf.Aliases, args[0])
// Save the config
return conf.SaveConfig(c.global.confPath)
}
// Remove.
type cmdAliasRemove struct {
global *cmdGlobal
alias *cmdAlias
}
// Command is a method of the cmdAliasRemove structure. It configures and returns a cobra.Command object.
// This command enables the removal of a given alias from the command line interface.
func (c *cmdAliasRemove) command() *cobra.Command {
cmd := &cobra.Command{}
cmd.Use = usage("remove", i18n.G("<alias>"))
cmd.Aliases = []string{"rm"}
cmd.Short = i18n.G("Remove aliases")
cmd.Long = cli.FormatSection(i18n.G("Description"), i18n.G(
`Remove aliases`))
cmd.Example = cli.FormatSection("", i18n.G(
`lxc alias remove my-list
Remove the "my-list" alias.`))
cmd.RunE = c.run
return cmd
}
// Run is a method of the cmdAliasRemove structure that executes the actual operation of the alias removal command.
// It takes as input the name of the alias to be removed and updates the global configuration file to reflect this change.
func (c *cmdAliasRemove) run(cmd *cobra.Command, args []string) error {
conf := c.global.conf
// Quick checks.
exit, err := c.global.CheckArgs(cmd, args, 1, 1)
if exit {
return err
}
// Look for the alias
_, ok := conf.Aliases[args[0]]
if !ok {
return fmt.Errorf(i18n.G("Alias %s doesn't exist"), args[0])
}
// Delete the alias
delete(conf.Aliases, args[0])
// Save the config
return conf.SaveConfig(c.global.confPath)
}