-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcontext_save.go
More file actions
124 lines (107 loc) · 4.26 KB
/
context_save.go
File metadata and controls
124 lines (107 loc) · 4.26 KB
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
package context
import (
"encoding/base64"
"os"
"github.com/spf13/cobra"
stscobra "github.com/stackvista/stackstate-cli/internal/cobra"
"github.com/stackvista/stackstate-cli/internal/common"
"github.com/stackvista/stackstate-cli/internal/config"
"github.com/stackvista/stackstate-cli/internal/di"
)
const (
APIPathFlag = "api-path"
)
type SaveArgs struct {
Name string
URL string
APIToken string
ServiceToken string
APIPath string
CaCertPath string
CaCertBase64Data string
SkipValidate bool
SkipSSLFlag bool
}
func SaveCommand(cli *di.Deps) *cobra.Command {
args := &SaveArgs{}
cmd := &cobra.Command{
Use: "save",
Short: "Save a connection context to the CLI configuration",
Long: `Save a connection context to the CLI configuration file. The context stores the server URL and authentication credentials. After saving, this context becomes the current active context.`,
Example: `# save a context with an API token
sts context save --name production --url https://stackstate.example.com --api-token xxxx-xxxx
# save a context with a service token for CI/CD pipelines
sts context save --name ci --url https://stackstate.example.com --service-token xxxx-xxxx
# save a context with a custom CA certificate
sts context save --name production --url https://stackstate.example.com --api-token xxxx-xxxx --ca-cert /path/to/ca.crt`,
RunE: cli.CmdRunE(RunContextSaveCommand(args)),
}
common.AddNameFlagVarVal(cmd, &args.Name, "default", "Name of the context")
cmd.Flags().StringVar(&args.URL, common.URLFlag, "", common.URLFlagUse)
cmd.Flags().StringVar(&args.APIToken, common.APITokenFlag, "", common.APITokenFlagUse)
cmd.Flags().StringVar(&args.ServiceToken, common.ServiceTokenFlag, "", common.ServiceTokenFlagUse)
cmd.Flags().StringVar(&args.APIPath, APIPathFlag, "/api", "Specify the path of the API end-point, e.g. the part that comes after the URL")
cmd.Flags().BoolVar(&args.SkipValidate, "skip-validate", false, "Skip validation of the context")
cmd.Flags().StringVar(&args.CaCertPath, common.CaCertPathFlag, "", common.CaCertPathFlagUse)
cmd.Flags().StringVar(&args.CaCertBase64Data, common.CaCertBase64DataFlag, "", common.CaCertBase64DataFlagUse)
cmd.Flags().BoolVar(&args.SkipSSLFlag, common.SkipSSLFlag, false, common.SkipSSLFlagUse)
cmd.MarkFlagRequired(common.URLFlag) //nolint:errcheck
stscobra.MarkMutexFlags(cmd, []string{common.APITokenFlag, common.ServiceTokenFlag, common.K8sSATokenFlag}, "tokens", true)
return cmd
}
func RunContextSaveCommand(args *SaveArgs) func(cli *di.Deps, cmd *cobra.Command) common.CLIError {
return func(cli *di.Deps, cmd *cobra.Command) common.CLIError {
cfg, err := config.ReadConfig(cli.ConfigPath)
if err != nil {
cfg = config.EmptyConfig()
}
namedCtx := &config.NamedContext{
Name: args.Name,
Context: &config.StsContext{
URL: args.URL,
APIToken: args.APIToken,
ServiceToken: args.ServiceToken,
APIPath: args.APIPath,
SkipSSL: args.SkipSSLFlag,
},
}
// Use private CA only if SkipSSL is not enabled
if !args.SkipSSLFlag {
// Providing CA certificate from file takes precedence over providing from the command line argument.
if args.CaCertPath != "" {
data, serr := os.ReadFile(args.CaCertPath)
if serr != nil {
return common.NewReadFileError(serr, args.CaCertPath)
}
namedCtx.Context.CaCertBase64Data = base64.StdEncoding.EncodeToString(data)
namedCtx.Context.CaCertPath = ""
} else if args.CaCertBase64Data != "" {
namedCtx.Context.CaCertBase64Data = args.CaCertBase64Data
}
}
if !args.SkipValidate {
if _, err := ValidateContext(cli, cmd, namedCtx.Context); err != nil {
return err
}
}
existingCtx, err := cfg.GetContext(args.Name)
if err != nil {
cfg.Contexts = append(cfg.Contexts, namedCtx)
} else {
existingCtx.Context = namedCtx.Context
}
cfg.CurrentContext = args.Name
if err := config.WriteConfig(cli.ConfigPath, cfg); err != nil {
return common.NewWriteFileError(err, cli.ConfigPath)
}
if cli.IsJson() {
cli.Printer.PrintJson(map[string]interface{}{
"current-context": cfg.CurrentContext,
"context": namedCtx.Context,
})
} else {
cli.Printer.Successf("Saved context: '%s'", args.Name)
}
return nil
}
}