-
Notifications
You must be signed in to change notification settings - Fork 11
/
config.go
369 lines (307 loc) · 10.6 KB
/
config.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
// Copyright 2024 The Codefresh Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package commands
import (
"context"
"errors"
"fmt"
"os"
cfgit "github.com/codefresh-io/cli-v2/pkg/git"
"github.com/codefresh-io/cli-v2/pkg/log"
"github.com/codefresh-io/cli-v2/pkg/store"
"github.com/codefresh-io/cli-v2/pkg/util"
platmodel "github.com/codefresh-io/go-sdk/pkg/model/platform"
"github.com/spf13/cobra"
)
type (
createContextOpts struct {
apiKey string
caCert string
name string
url string
}
updateGitOpsSettingsOpts struct {
gitProvider cfgit.ProviderType
gitApiURL string
sharedConfigRepo string
inferred bool
}
)
func NewConfigCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "config",
Short: "Manage Codefresh authentication contexts",
PersistentPreRunE: cfConfig.Load,
Args: cobra.NoArgs, // Workaround for subcommand usage errors. See: https://github.com/spf13/cobra/issues/706
Long: util.Doc(`By default, Codefresh authentication contexts are persisted at $HOME/.cfconfig.
You can create, delete and list authentication contexts using the following
commands, respectively:
<BIN> config create-context <NAME> --api-key <key>
<BIN> config delete-context <NAME>
<BIN> config get-contexts
`),
Run: func(cmd *cobra.Command, args []string) {
cmd.HelpFunc()(cmd, args)
exit(1)
},
}
cmd.AddCommand(newConfigGetContextsCommand())
cmd.AddCommand(newConfigCurrentContextCommand())
cmd.AddCommand(newConfigUseContextCommand())
cmd.AddCommand(newConfigCreateContextCommand())
cmd.AddCommand(newConfigDeleteContextCommand())
cmd.AddCommand(newConfigSetRuntimeCommand())
cmd.AddCommand(newConfigGetRuntimeCommand())
cmd.AddCommand(newResetIscRepoURLCommand())
cmd.AddCommand(newUpdateGitOpsSettingsCommand())
return cmd
}
func newConfigCreateContextCommand() *cobra.Command {
opts := &createContextOpts{}
cmd := &cobra.Command{
Use: "create-context NAME",
Short: "Create a new Codefresh authentication context",
Args: cobra.MaximumNArgs(1),
Example: util.Doc(`
# Create a new context named 'test':
<BIN> config create-context test --api-key TOKEN`),
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return fmt.Errorf("must provide context name to use")
}
opts.name = args[0]
return runConfigCreateContext(cmd.Context(), opts)
},
}
cmd.Flags().StringVar(&opts.apiKey, "api-key", "", "API key")
cmd.Flags().StringVar(&opts.caCert, "ca-cert", "", "Codefresh Platform certificate file (for on-prem)")
cmd.Flags().StringVar(&opts.url, "url", store.Get().DefaultAPI, "Codefresh system custom url ")
die(cmd.MarkFlagRequired("api-key"))
return cmd
}
func runConfigCreateContext(ctx context.Context, opts *createContextOpts) error {
if err := cfConfig.CreateContext(ctx, opts.name, opts.apiKey, opts.url, opts.caCert); err != nil {
return err
}
log.G().Infof("New context created: \"%s\"", opts.name)
return runConfigUseContext(ctx, opts.name)
}
func newConfigGetContextsCommand() *cobra.Command {
return &cobra.Command{
Use: "get-contexts",
Aliases: []string{"view"},
Args: cobra.NoArgs,
Short: "Lists all Codefresh authentication contexts",
Example: util.Doc(`
# List all authentication contexts:
<BIN> config get-contexts`),
RunE: func(cmd *cobra.Command, _ []string) error {
return runConfigGetContexts(cmd.Context())
},
}
}
func runConfigGetContexts(ctx context.Context) error {
return cfConfig.Write(ctx, os.Stdout)
}
func newConfigCurrentContextCommand() *cobra.Command {
return &cobra.Command{
Use: "current-context",
Short: "Shows the currently selected Codefresh authentication context",
Args: cobra.NoArgs,
Example: util.Doc(`
# Shows the current context:
<BIN> config current-context`),
RunE: func(cmd *cobra.Command, _ []string) error {
return runConfigCurrentContext(cmd.Context())
},
}
}
func runConfigCurrentContext(ctx context.Context) error {
cur := cfConfig.GetCurrentContext()
if cur.Name == "" {
log.G(ctx).Fatal(util.Doc("no currently selected context, use '<BIN> config use-context' to select a context"))
}
log.G(ctx).Info(cur.Name)
return nil
}
func newConfigSetRuntimeCommand() *cobra.Command {
return &cobra.Command{
Use: "set-runtime RUNTIME",
Short: "Sets the default runtime name to use for the current authentication context",
Args: cobra.MaximumNArgs(1),
Example: util.Doc(`
# Sets the default runtime to 'runtime-2':
<BIN> config set-runtime runtime-2`),
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return fmt.Errorf("must provide runtime name to use")
}
return runConfigSetRuntime(cmd.Context(), args[0])
},
}
}
func runConfigSetRuntime(ctx context.Context, runtime string) error {
_, err := getRuntime(ctx, runtime)
if err != nil {
return err
}
cur := cfConfig.GetCurrentContext()
if cur.Name == "" {
log.G(ctx).Fatal(util.Doc("no currently selected context, use '<BIN> config use-context' to select a context"))
}
cur.DefaultRuntime = runtime
if err := cfConfig.Save(); err != nil {
return fmt.Errorf("failed to save config: %w", err)
}
log.G(ctx).Infof("default runtime set to: %s", runtime)
return nil
}
func newConfigGetRuntimeCommand() *cobra.Command {
return &cobra.Command{
Use: "get-runtime",
Short: "Gets the default runtime for the current authentication context",
Args: cobra.NoArgs,
Example: util.Doc(`
# Prints the default runtime:
<BIN> config get-runtime`),
RunE: func(cmd *cobra.Command, _ []string) error {
return runConfigGetRuntime(cmd.Context())
},
}
}
func runConfigGetRuntime(ctx context.Context) error {
cur := cfConfig.GetCurrentContext()
if cur.DefaultRuntime == "" {
return fmt.Errorf(util.Doc("no default runtime is set for current context, use '<BIN> config set-runtime' to set one"))
}
log.G(ctx).Infof("default runtime set to: %s", cur.DefaultRuntime)
return nil
}
func newConfigUseContextCommand() *cobra.Command {
return &cobra.Command{
Use: "use-context CONTEXT",
Short: "Switch the current authentication context",
Args: cobra.MaximumNArgs(1),
Example: util.Doc(`
# Switch to another authentication context:
<BIN> config use-context test`),
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return fmt.Errorf("must provide context name to use")
}
return runConfigUseContext(cmd.Context(), args[0])
},
}
}
func runConfigUseContext(ctx context.Context, context string) error {
if err := cfConfig.UseContext(ctx, context); err != nil {
return err
}
log.G().Infof("Switched to context: %s", context)
return nil
}
func newConfigDeleteContextCommand() *cobra.Command {
return &cobra.Command{
Use: "delete-context CONTEXT",
Short: "Delete the specified authentication context",
Args: cobra.MaximumNArgs(1),
Example: util.Doc(`
# Deleting an authentication context name 'test':
<BIN> config delete-context test`),
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return fmt.Errorf("must provide context name to use")
}
return runConfigDeleteContext(cmd.Context(), args[0])
},
}
}
func runConfigDeleteContext(ctx context.Context, context string) error {
if err := cfConfig.DeleteContext(context); err != nil {
return err
}
log.G(ctx).Infof("Deleted context: %s", context)
return nil
}
func newResetIscRepoURLCommand() *cobra.Command {
return &cobra.Command{
Use: "reset-shared-config-repo",
Deprecated: "use update-gitops-settings command instead",
Hidden: true,
PreRunE: func(cmd *cobra.Command, _ []string) error {
return errors.New("command removed")
},
}
}
func newUpdateGitOpsSettingsCommand() *cobra.Command {
opts := &updateGitOpsSettingsOpts{}
cmd := &cobra.Command{
Use: "update-gitops-settings",
Short: "Updates the account's GitOps settings (gitProvider|gitApiUrl|sharedConfigRepo) if possible",
Args: cobra.NoArgs,
PersistentPreRunE: cfConfig.RequireAuthentication,
PreRunE: func(_ *cobra.Command, _ []string) error {
return updateGitOpsSettingsPreRunHandler(opts)
},
RunE: func(cmd *cobra.Command, _ []string) error {
ctx := cmd.Context()
if opts.inferred {
finalParameters := map[string]string{
"git provider": string(opts.gitProvider),
"git API url": opts.gitApiURL,
"shared config repo": opts.sharedConfigRepo,
}
if err := getApprovalFromUser(ctx, finalParameters, "Update GitOps Settings"); err != nil {
return err
}
}
return runUpdateGitOpsSettings(ctx, opts)
},
}
cmd.Flags().Var(&opts.gitProvider, "git-provider", "The git provider, one of: bitbucket|bitbucket-server|github|gitlab")
cmd.Flags().StringVar(&opts.gitApiURL, "git-api-url", "", "Your git server's API URL")
cmd.Flags().StringVar(&opts.sharedConfigRepo, "shared-config-repo", "", "URL to the shared configurations repo")
cmd.Flags().BoolVar(&store.Get().Silent, "silent", false, "Disables the command wizard")
util.Die(cobra.MarkFlagRequired(cmd.Flags(), "shared-config-repo"))
return cmd
}
func updateGitOpsSettingsPreRunHandler(opts *updateGitOpsSettingsOpts) error {
baseURL := opts.sharedConfigRepo
if opts.gitApiURL != "" {
baseURL = opts.gitApiURL
}
provider, err := cfgit.GetProvider(opts.gitProvider, baseURL, "")
if err != nil {
return err
}
if opts.gitProvider == "" {
opts.gitProvider = provider.Type()
opts.inferred = true
}
if opts.gitApiURL == "" {
opts.gitApiURL = provider.ApiURL()
opts.inferred = true
} else if opts.gitApiURL != provider.ApiURL() && provider.IsCloud() {
return fmt.Errorf("supplied git-api-url \"%s\" does not match inferred git-api-url \"%s\" from %s cloud", opts.gitApiURL, provider.ApiURL(), provider.Type())
}
return nil
}
func runUpdateGitOpsSettings(ctx context.Context, opts *updateGitOpsSettingsOpts) error {
apGitProvider, err := cliToModelGitProvider(string(opts.gitProvider))
if err != nil {
return err
}
platGitProvider := platmodel.GitProviders(apGitProvider)
return cfConfig.NewClient().GraphQL().Account().UpdateCsdpSettings(ctx, platGitProvider, opts.gitApiURL, opts.sharedConfigRepo)
}