-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcontext_validate.go
More file actions
62 lines (51 loc) · 1.58 KB
/
context_validate.go
File metadata and controls
62 lines (51 loc) · 1.58 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
package context
import (
"github.com/spf13/cobra"
"github.com/stackvista/stackstate-cli/internal/common"
"github.com/stackvista/stackstate-cli/internal/config"
"github.com/stackvista/stackstate-cli/internal/di"
)
type ValidateArgs struct {
Name string
}
func ValidateCommand(cli *di.Deps) *cobra.Command {
args := &ValidateArgs{}
cmd := &cobra.Command{
Use: "validate",
Short: "Validate that a context can connect to the server",
Long: "Validate a connection context by attempting to connect to the SUSE Observability server. Validates the current context if no name is specified.",
Example: `# validate the current context
sts context validate
# validate a specific context
sts context validate --name production`,
RunE: cli.CmdRunEWithConfig(RunValidateCommand(args)),
}
common.AddNameFlagVar(cmd, &args.Name, "Name of the context")
return cmd
}
func RunValidateCommand(args *ValidateArgs) func(cli *di.Deps, cmd *cobra.Command, cfg *config.Config) common.CLIError {
return func(cli *di.Deps, cmd *cobra.Command, cfg *config.Config) common.CLIError {
ctxName := cfg.CurrentContext
if args.Name != "" {
ctxName = args.Name
}
ctx, err := cfg.GetContext(ctxName)
if err != nil {
return common.NewNotFoundError(err)
}
serverInfo, cerr := ValidateContext(cli, cmd, ctx.Context)
if cerr != nil {
return cerr
}
if cli.IsJson() {
cli.Printer.PrintJson(map[string]interface{}{
"context": ctxName,
"server-info": serverInfo,
"connected": true,
})
} else {
cli.Printer.Successf("Context %s is valid.\n", ctxName)
}
return nil
}
}