-
Notifications
You must be signed in to change notification settings - Fork 252
/
Copy pathcli.go
262 lines (239 loc) · 8.28 KB
/
cli.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
package config
import (
"fmt"
"strings"
"github.com/jfrog/jfrog-client-go/auth/cert"
"github.com/jfrog/jfrog-cli-core/v2/common/commands"
corecommon "github.com/jfrog/jfrog-cli-core/v2/docs/common"
"github.com/jfrog/jfrog-cli-core/v2/utils/coreutils"
"github.com/jfrog/jfrog-cli/docs/config/add"
"github.com/jfrog/jfrog-cli/docs/config/edit"
"github.com/jfrog/jfrog-cli/docs/config/remove"
"github.com/jfrog/jfrog-cli/docs/config/use"
"github.com/jfrog/jfrog-client-go/utils/errorutils"
"github.com/jfrog/jfrog-client-go/utils/io/fileutils"
"github.com/urfave/cli"
"github.com/jfrog/jfrog-cli/docs/config/exportcmd"
"github.com/jfrog/jfrog-cli/docs/config/importcmd"
"github.com/jfrog/jfrog-cli/docs/config/show"
"github.com/jfrog/jfrog-cli/utils/cliutils"
)
func GetCommands() []cli.Command {
return cliutils.GetSortedCommands(cli.CommandsByName{
{
Name: "add",
Usage: add.GetDescription(),
Flags: cliutils.GetCommandFlags(cliutils.AddConfig),
HelpName: corecommon.CreateUsage("c add", add.GetDescription(), add.Usage),
BashComplete: corecommon.CreateBashCompletionFunc(),
Action: addCmd,
},
{
Name: "edit",
Usage: edit.GetDescription(),
Flags: cliutils.GetCommandFlags(cliutils.EditConfig),
HelpName: corecommon.CreateUsage("c edit", edit.GetDescription(), edit.Usage),
BashComplete: corecommon.CreateBashCompletionFunc(commands.GetAllServerIds()...),
Action: editCmd,
},
{
Name: "show",
Aliases: []string{"s"},
Usage: show.GetDescription(),
HelpName: corecommon.CreateUsage("c show", show.GetDescription(), show.Usage),
BashComplete: corecommon.CreateBashCompletionFunc(commands.GetAllServerIds()...),
Action: showCmd,
},
{
Name: "remove",
Aliases: []string{"rm"},
Usage: remove.GetDescription(),
Flags: cliutils.GetCommandFlags(cliutils.DeleteConfig),
HelpName: corecommon.CreateUsage("c rm", remove.GetDescription(), remove.Usage),
BashComplete: corecommon.CreateBashCompletionFunc(commands.GetAllServerIds()...),
Action: deleteCmd,
},
{
Name: "import",
Aliases: []string{"im"},
Usage: importcmd.GetDescription(),
HelpName: corecommon.CreateUsage("c import", importcmd.GetDescription(), importcmd.Usage),
BashComplete: corecommon.CreateBashCompletionFunc(),
Action: importCmd,
},
{
Name: "export",
Aliases: []string{"ex"},
Usage: exportcmd.GetDescription(),
HelpName: corecommon.CreateUsage("c export", exportcmd.GetDescription(), exportcmd.Usage),
BashComplete: corecommon.CreateBashCompletionFunc(commands.GetAllServerIds()...),
Action: exportCmd,
},
{
Name: "use",
Usage: use.GetDescription(),
HelpName: corecommon.CreateUsage("c use", use.GetDescription(), use.Usage),
BashComplete: corecommon.CreateBashCompletionFunc(commands.GetAllServerIds()...),
Action: useCmd,
},
})
}
func addCmd(c *cli.Context) error {
if c.NArg() > 1 {
return cliutils.WrongNumberOfArgumentsHandler(c)
}
if c.Bool(cliutils.Overwrite) {
return addOrEdit(c, overwriteOperation)
}
return addOrEdit(c, addOperation)
}
func editCmd(c *cli.Context) error {
if c.NArg() != 1 {
return cliutils.WrongNumberOfArgumentsHandler(c)
}
return addOrEdit(c, editOperation)
}
type configOperation int
const (
// "config add" comment
addOperation configOperation = iota
// "config edit" comment
editOperation
// "config add with --overwrite" comment
overwriteOperation
)
func addOrEdit(c *cli.Context, operation configOperation) error {
configCommandConfiguration, err := CreateConfigCommandConfiguration(c)
if err != nil {
return err
}
var serverId string
if c.NArg() > 0 {
serverId = c.Args()[0]
if err := ValidateServerId(serverId); err != nil {
return err
}
if operation != overwriteOperation {
if err := validateServerExistence(serverId, operation); err != nil {
return err
}
}
}
err = validateConfigFlags(configCommandConfiguration)
if err != nil {
return err
}
configCmd := commands.NewConfigCommand(commands.AddOrEdit, serverId).SetDetails(configCommandConfiguration.ServerDetails).SetInteractive(configCommandConfiguration.Interactive).
SetEncPassword(configCommandConfiguration.EncPassword).SetUseBasicAuthOnly(configCommandConfiguration.BasicAuthOnly)
return configCmd.ExecAndReportUsage()
}
func showCmd(c *cli.Context) error {
if c.NArg() > 1 {
return cliutils.WrongNumberOfArgumentsHandler(c)
}
var serverId string
if c.NArg() == 1 {
serverId = c.Args()[0]
}
return commands.ShowConfig(serverId)
}
func deleteCmd(c *cli.Context) error {
if c.NArg() > 1 {
return cliutils.WrongNumberOfArgumentsHandler(c)
}
quiet := cliutils.GetQuietValue(c)
// Clear all configurations
if c.NArg() == 0 {
return commands.NewConfigCommand(commands.Clear, "").SetInteractive(!quiet).Run()
}
// Delete single configuration
serverId := c.Args()[0]
if !quiet && !coreutils.AskYesNo("Are you sure you want to delete \""+serverId+"\" configuration?", false) {
return nil
}
return commands.NewConfigCommand(commands.Delete, serverId).Run()
}
func importCmd(c *cli.Context) error {
if c.NArg() != 1 {
return cliutils.WrongNumberOfArgumentsHandler(c)
}
return commands.Import(c.Args()[0])
}
func exportCmd(c *cli.Context) error {
if c.NArg() > 1 {
return cliutils.WrongNumberOfArgumentsHandler(c)
}
// If no server Id was given, export the default server.
serverId := ""
if c.NArg() == 1 {
serverId = c.Args()[0]
}
return commands.Export(serverId)
}
func useCmd(c *cli.Context) error {
if c.NArg() != 1 {
return cliutils.WrongNumberOfArgumentsHandler(c)
}
serverId := c.Args()[0]
return commands.NewConfigCommand(commands.Use, serverId).Run()
}
func CreateConfigCommandConfiguration(c *cli.Context) (configCommandConfiguration *commands.ConfigCommandConfiguration, err error) {
configCommandConfiguration = new(commands.ConfigCommandConfiguration)
configCommandConfiguration.ServerDetails, err = cliutils.CreateServerDetailsFromFlags(c)
if err != nil {
return
}
configCommandConfiguration.EncPassword = c.BoolT(cliutils.EncPassword)
configCommandConfiguration.Interactive = cliutils.GetInteractiveValue(c)
configCommandConfiguration.BasicAuthOnly = c.Bool(cliutils.BasicAuthOnly)
return
}
func ValidateServerId(serverId string) error {
reservedIds := []string{"delete", "use", "show", "clear"}
for _, reservedId := range reservedIds {
if serverId == reservedId {
return fmt.Errorf("server can't have one of the following ID's: %s\n%s", strings.Join(reservedIds, ", "), cliutils.GetDocumentationMessage())
}
}
return nil
}
func validateServerExistence(serverId string, operation configOperation) error {
config, err := commands.GetConfig(serverId, false)
serverExist := err == nil && config.ServerId != ""
if operation == editOperation && !serverExist {
return errorutils.CheckErrorf("Server ID '%s' doesn't exist.", serverId)
} else if operation == addOperation && serverExist {
return errorutils.CheckErrorf("Server ID '%s' already exists.", serverId)
}
return nil
}
func validateConfigFlags(configCommandConfiguration *commands.ConfigCommandConfiguration) error {
// Validate the option is not used along with access token
if configCommandConfiguration.BasicAuthOnly && configCommandConfiguration.ServerDetails.AccessToken != "" {
return errorutils.CheckErrorf("the --%s option is only supported when username and password/API key are provided", cliutils.BasicAuthOnly)
}
if err := validatePathsExist(configCommandConfiguration.ServerDetails.SshKeyPath, configCommandConfiguration.ServerDetails.ClientCertPath, configCommandConfiguration.ServerDetails.ClientCertKeyPath); err != nil {
return err
}
if configCommandConfiguration.ServerDetails.ClientCertPath != "" {
_, err := cert.LoadCertificate(configCommandConfiguration.ServerDetails.ClientCertPath, configCommandConfiguration.ServerDetails.ClientCertKeyPath)
if err != nil {
return err
}
}
return nil
}
func validatePathsExist(paths ...string) error {
for _, path := range paths {
if path != "" {
exists, err := fileutils.IsFileExists(path, true)
if err != nil {
return err
}
if !exists {
return errorutils.CheckErrorf("file does not exit at " + path)
}
}
}
return nil
}