-
Notifications
You must be signed in to change notification settings - Fork 67
/
config.go
175 lines (150 loc) 路 4.24 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
package cmd
import (
"context"
"fmt"
"os"
"github.com/kubeshop/tracetest/cli/analytics"
"github.com/kubeshop/tracetest/cli/config"
"github.com/kubeshop/tracetest/cli/formatters"
"github.com/kubeshop/tracetest/cli/openapi"
"github.com/spf13/cobra"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
var (
cliLogger = &zap.Logger{}
cliConfig config.Config
openapiClient = &openapi.APIClient{}
versionText string
isVersionMatch bool
// only available in dev mode
isCloudEnabled = os.Getenv("TRACETEST_DEV") == "true"
)
type setupConfig struct {
shouldValidateConfig bool
shouldValidateVersionMismatch bool
}
type setupOption func(*setupConfig)
func SkipConfigValidation() setupOption {
return func(sc *setupConfig) {
sc.shouldValidateConfig = false
}
}
func SkipVersionMismatchCheck() setupOption {
return func(sc *setupConfig) {
sc.shouldValidateVersionMismatch = false
}
}
func setupCommand(options ...setupOption) func(cmd *cobra.Command, args []string) {
config := setupConfig{
shouldValidateConfig: true,
shouldValidateVersionMismatch: true,
}
for _, option := range options {
option(&config)
}
return func(cmd *cobra.Command, args []string) {
setupOutputFormat(cmd)
setupLogger(cmd, args)
loadConfig(cmd, args)
overrideConfig()
setupVersion()
setupResources()
setupRunners()
if config.shouldValidateConfig {
validateConfig(cmd, args)
}
if config.shouldValidateVersionMismatch {
validateVersionMismatch()
}
analytics.Init()
}
}
func overrideConfig() {
if overrideEndpoint != "" {
scheme, endpoint, _, err := config.ParseServerURL(overrideEndpoint)
if err != nil {
msg := fmt.Sprintf("cannot parse endpoint %s", overrideEndpoint)
cliLogger.Error(msg, zap.Error(err))
ExitCLI(1)
}
cliConfig.Scheme = scheme
cliConfig.Endpoint = endpoint
}
}
func setupRunners() {
c := config.GetAPIClient(cliConfig)
*openapiClient = *c
}
func setupOutputFormat(cmd *cobra.Command) {
o := formatters.Output(output)
if output == "" {
o = formatters.Pretty
}
if !formatters.ValidOutput(o) {
fmt.Fprintf(os.Stderr, "Invalid output format %s. Available formats are [%s]\n", output, outputFormatsString)
ExitCLI(1)
}
}
func loadConfig(cmd *cobra.Command, args []string) {
config, err := config.LoadConfig(configFile)
if err != nil {
cliLogger.Fatal("could not load config", zap.Error(err))
}
cliConfig = config
}
func validateConfig(cmd *cobra.Command, args []string) {
if cliConfig.IsEmpty() {
cliLogger.Warn("You haven't configured your CLI, some commands might fail!")
cliLogger.Warn("Run 'tracetest configure' to configure your CLI")
}
}
func setupLogger(cmd *cobra.Command, args []string) {
atom := zap.NewAtomicLevel()
if verbose {
atom.SetLevel(zap.DebugLevel)
} else {
atom.SetLevel(zap.WarnLevel)
}
encoderCfg := zapcore.EncoderConfig{
TimeKey: zapcore.OmitKey,
LevelKey: "level",
NameKey: zapcore.OmitKey,
CallerKey: zapcore.OmitKey,
FunctionKey: zapcore.OmitKey,
MessageKey: "message",
StacktraceKey: zapcore.OmitKey,
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: zapcore.CapitalColorLevelEncoder,
EncodeTime: zapcore.EpochTimeEncoder,
EncodeDuration: zapcore.SecondsDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
}
logger := zap.New(zapcore.NewCore(
zapcore.NewConsoleEncoder(encoderCfg),
zapcore.Lock(os.Stdout),
atom,
))
*cliLogger = *logger
}
func teardownCommand(cmd *cobra.Command, args []string) {
cliLogger.Sync()
}
func setupVersion() {
versionText, isVersionMatch = config.GetVersion(
context.Background(),
cliConfig,
config.GetAPIClient(cliConfig),
)
}
func validateVersionMismatch() {
if !isVersionMatch && os.Getenv("TRACETEST_DEV") == "" {
fmt.Fprintf(os.Stderr, versionText+`
鉁栵笍 Error: Version Mismatch
The CLI version and the server version are not compatible. To fix this, you'll need to make sure that both your CLI and server are using compatible versions.
We recommend upgrading both of them to the latest available version. Check out our documentation https://docs.tracetest.io/configuration/upgrade for simple instructions on how to upgrade.
Thank you for using Tracetest! We apologize for any inconvenience caused.
`)
ExitCLI(1)
}
}