-
Notifications
You must be signed in to change notification settings - Fork 67
/
configure_cmd.go
101 lines (83 loc) 路 2.79 KB
/
configure_cmd.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
package cmd
import (
"context"
"net/url"
"github.com/kubeshop/tracetest/cli/config"
"github.com/spf13/cobra"
)
var configParams = &configureParameters{}
var (
configurator = config.NewConfigurator(resources)
)
var configureCmd = &cobra.Command{
GroupID: cmdGroupConfig.ID,
Use: "configure",
Short: "Configure your tracetest CLI",
Long: "Configure your tracetest CLI",
PreRun: setupLogger,
Run: WithResultHandler(WithParamsHandler(configParams)(func(cmd *cobra.Command, _ []string) (string, error) {
ctx := context.Background()
flags := config.ConfigFlags{
CI: configParams.CI,
}
config, err := config.LoadConfig("")
if err != nil {
return "", err
}
if flagProvided(cmd, "endpoint") {
flags.Endpoint = configParams.Endpoint
}
if flagProvided(cmd, "token") {
flags.Token = configParams.Token
}
if flagProvided(cmd, "environment") {
flags.EnvironmentID = configParams.EnvironmentID
}
if flagProvided(cmd, "organization") {
flags.OrganizationID = configParams.OrganizationID
}
err = configurator.Start(ctx, config, flags)
return "", err
})),
PostRun: teardownCommand,
}
func flagProvided(cmd *cobra.Command, name string) bool {
return cmd.Flags().Lookup(name).Changed
}
func init() {
configureCmd.PersistentFlags().BoolVarP(&configParams.Global, "global", "g", false, "configuration will be saved in your home dir")
configureCmd.PersistentFlags().StringVarP(&configParams.Endpoint, "endpoint", "e", "", "set the value for the endpoint, so the CLI won't ask for this value")
configureCmd.PersistentFlags().StringVarP(&configParams.Token, "token", "t", "", "set authetication with token, so the CLI won't ask you for authentication")
configureCmd.PersistentFlags().StringVarP(&configParams.EnvironmentID, "environment", "", "", "set environmentID, so the CLI won't ask you for it")
configureCmd.PersistentFlags().StringVarP(&configParams.OrganizationID, "organization", "", "", "set organizationID, so the CLI won't ask you for it")
configureCmd.PersistentFlags().BoolVarP(&configParams.CI, "ci", "", false, "if cloud is used, don't ask for authentication")
rootCmd.AddCommand(configureCmd)
}
type configureParameters struct {
Endpoint string
Global bool
CI bool
Token string
OrganizationID string
EnvironmentID string
}
func (p configureParameters) Validate(cmd *cobra.Command, args []string) []error {
var errors []error
if cmd.Flags().Lookup("endpoint").Changed {
if p.Endpoint == "" {
errors = append(errors, paramError{
Parameter: "endpoint",
Message: "endpoint cannot be empty",
})
} else {
_, err := url.Parse(p.Endpoint)
if err != nil {
errors = append(errors, paramError{
Parameter: "endpoint",
Message: "endpoint is not a valid URL",
})
}
}
}
return errors
}