-
Notifications
You must be signed in to change notification settings - Fork 67
/
config.go
181 lines (144 loc) 路 4.26 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
package config
import (
"encoding/json"
"fmt"
"os"
"path"
"path/filepath"
"strings"
"github.com/goware/urlx"
"github.com/spf13/viper"
"gopkg.in/yaml.v2"
)
var (
Version = "dev"
Env = "dev"
DefaultCloudEndpoint = "http://app.tracetest.io"
DefaultCloudDomain = "tracetest.io"
DefaultCloudPath = "/"
)
type Config struct {
Scheme string `yaml:"scheme"`
Endpoint string `yaml:"endpoint"`
ServerPath *string `yaml:"serverPath,omitempty"`
OrganizationID string `yaml:"organizationID,omitempty"`
EnvironmentID string `yaml:"environmentID,omitempty"`
Token string `yaml:"token,omitempty"`
Jwt string `yaml:"jwt,omitempty"`
AgentApiKey string `yaml:"-"`
EndpointOverriden bool `yaml:"-"`
// cloud config
CloudAPIEndpoint string `yaml:"-"`
AgentEndpoint string `yaml:"agentEndpoint,omitempty"`
UIEndpoint string `yaml:"uIEndpoint,omitempty"`
}
func (c Config) OAuthEndpoint() string {
return fmt.Sprintf("%s%s", c.URL(), c.Path())
}
func (c Config) URL() string {
if c.Scheme == "" || c.Endpoint == "" {
return ""
}
return fmt.Sprintf("%s://%s", c.Scheme, strings.TrimSuffix(c.Endpoint, "/"))
}
func (c Config) UI() string {
if c.UIEndpoint != "" && !c.EndpointOverriden {
return fmt.Sprintf("%s/organizations/%s/environments/%s", strings.TrimSuffix(c.UIEndpoint, "/"), c.OrganizationID, c.EnvironmentID)
}
return c.URL()
}
func (c Config) Path() string {
pathPrefix := "/api"
if c.ServerPath != nil {
pathPrefix = *c.ServerPath
}
if pathPrefix == "/" {
return ""
}
return pathPrefix
}
func (c Config) IsEmpty() bool {
thisConfigJson, _ := json.Marshal(c)
emptyConfigJson, _ := json.Marshal(Config{})
return string(thisConfigJson) == string(emptyConfigJson)
}
func LoadConfig(configFile string) (Config, error) {
config, err := loadConfig(configFile)
if err != nil {
return Config{}, err
}
if !config.IsEmpty() {
return config, nil
}
if config.CloudAPIEndpoint == "" {
config.CloudAPIEndpoint = DefaultCloudEndpoint
}
homePath, err := os.UserHomeDir()
if err != nil {
return Config{}, fmt.Errorf("could not get user home path")
}
globalConfigPath := filepath.Join(homePath, ".tracetest/config.yml")
return loadConfig(globalConfigPath)
}
func loadConfig(configFile string) (Config, error) {
viper.SetConfigFile(configFile)
viper.SetConfigType("yaml")
viper.SetEnvPrefix("tracetest")
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
viper.AutomaticEnv()
if err := viper.ReadInConfig(); err != nil {
// No configuration file found, return empty config
return Config{}, nil
}
config := Config{}
if err := viper.Unmarshal(&config); err != nil {
return Config{}, fmt.Errorf("could not unmarshal config: %w", err)
}
return config, nil
}
func ValidateServerURL(serverURL string) error {
if !strings.HasPrefix(serverURL, "http://") && !strings.HasPrefix(serverURL, "https://") {
return fmt.Errorf(`the server URL must start with the scheme, either "http://" or "https://"`)
}
return nil
}
func ParseServerURL(serverURL string) (scheme, endpoint string, serverPath *string, err error) {
url, err := urlx.Parse(serverURL)
if err != nil {
return "", "", nil, fmt.Errorf("could not parse server URL: %w", err)
}
var path *string
if url.Path != "" {
path = &url.Path
}
return url.Scheme, url.Host, path, nil
}
func Save(config Config) error {
configPath, err := GetConfigurationPath()
if err != nil {
return fmt.Errorf("could not get configuration path: %w", err)
}
configYml, err := yaml.Marshal(config)
if err != nil {
return fmt.Errorf("could not marshal configuration into yml: %w", err)
}
if _, err := os.Stat(configPath); os.IsNotExist(err) {
os.MkdirAll(filepath.Dir(configPath), 0700) // Ensure folder exists
}
err = os.WriteFile(configPath, configYml, 0755)
if err != nil {
return fmt.Errorf("could not write file: %w", err)
}
return nil
}
func GetConfigurationPath() (string, error) {
configPath := "./config.yml"
if _, err := os.Stat("config.yml"); os.IsNotExist(err) {
homePath, err := os.UserHomeDir()
if err != nil {
return "", fmt.Errorf("could not get user home dir: %w", err)
}
configPath = path.Join(homePath, ".tracetest/config.yml")
}
return configPath, nil
}