-
Notifications
You must be signed in to change notification settings - Fork 11
/
config.go
274 lines (224 loc) · 7.32 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
// Copyright 2022 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"
"fmt"
"os"
"github.com/codefresh-io/cli-v2/pkg/log"
"github.com/codefresh-io/cli-v2/pkg/store"
"github.com/codefresh-io/cli-v2/pkg/util"
"github.com/spf13/cobra"
)
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())
return cmd
}
func NewConfigCreateContextCommand() *cobra.Command {
var (
apiKey string
url string
)
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")
}
return RunConfigCreateContext(cmd.Context(), args[0], apiKey, url)
},
}
cmd.Flags().StringVar(&apiKey, "api-key", "", "API key")
cmd.Flags().StringVar(&url, "url", store.Get().DefaultAPI, "Codefresh system custom url ")
die(cmd.MarkFlagRequired("api-key"))
return cmd
}
func RunConfigCreateContext(ctx context.Context, context, apiKey, url string) error {
if err := cfConfig.CreateContext(ctx, context, apiKey, url); err != nil {
return err
}
log.G().Infof("New context created: \"%s\"", context)
return RunConfigUseContext(ctx, context)
}
func NewConfigGetContextsCommand() *cobra.Command {
cmd := &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, args []string) error {
return RunConfigGetContexts(cmd.Context())
},
}
return cmd
}
func RunConfigGetContexts(ctx context.Context) error {
return cfConfig.Write(ctx, os.Stdout)
}
func NewConfigCurrentContextCommand() *cobra.Command {
cmd := &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, args []string) error {
return RunConfigCurrentContext(cmd.Context())
},
}
return cmd
}
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 {
cmd := &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])
},
}
return cmd
}
func RunConfigSetRuntime(ctx context.Context, runtime string) error {
_, err := cfConfig.NewClient().V2().Runtime().Get(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 {
cmd := &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, args []string) error {
return RunConfigGetRuntime(cmd.Context())
},
}
return cmd
}
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 {
cmd := &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])
},
}
return cmd
}
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 {
cmd := &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])
},
}
return cmd
}
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
}