-
Notifications
You must be signed in to change notification settings - Fork 221
/
config.go
166 lines (133 loc) · 4.48 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
package config
import (
"strings"
"sync"
"github.com/spf13/pflag"
"github.com/superfly/flyctl/internal/env"
"github.com/superfly/flyctl/internal/flag"
)
const (
// FileName denotes the name of the config file.
FileName = "config.yml"
envKeyPrefix = "FLY_"
apiBaseURLEnvKey = envKeyPrefix + "API_BASE_URL"
AccessTokenEnvKey = envKeyPrefix + "ACCESS_TOKEN"
AccessTokenFileKey = "access_token"
WireGuardStateFileKey = "wire_guard_state"
APITokenEnvKey = envKeyPrefix + "API_TOKEN"
orgEnvKey = envKeyPrefix + "ORG"
registryHostEnvKey = envKeyPrefix + "REGISTRY_HOST"
organizationEnvKey = envKeyPrefix + "ORGANIZATION"
regionEnvKey = envKeyPrefix + "REGION"
verboseOutputEnvKey = envKeyPrefix + "VERBOSE"
jsonOutputEnvKey = envKeyPrefix + "JSON"
logGQLEnvKey = envKeyPrefix + "LOG_GQL_ERRORS"
localOnlyEnvKey = envKeyPrefix + "LOCAL_ONLY"
defaultAPIBaseURL = "https://api.fly.io"
defaultRegistryHost = "registry.fly.io"
)
// Config wraps the functionality of the configuration file.
//
// Instances of Config are safe for concurrent use.
type Config struct {
mu sync.RWMutex
// APIBaseURL denotes the base URL of the API.
APIBaseURL string
// RegistryHost denotes the docker registry host.
RegistryHost string
// VerboseOutput denotes whether the user wants the output to be verbose.
VerboseOutput bool
// JSONOutput denotes whether the user wants the output to be JSON.
JSONOutput bool
// LogGQLErrors denotes whether the user wants the log GraphQL errors.
LogGQLErrors bool
// Organization denotes the organizational slug the user has selected.
Organization string
// Region denotes the region slug the user has selected.
Region string
// LocalOnly denotes whether the user wants only local operations.
LocalOnly bool
// AccessToken denotes the user's access token.
AccessToken string
}
// New returns a new instance of Config populated with default values.
func New() *Config {
return &Config{
APIBaseURL: defaultAPIBaseURL,
RegistryHost: defaultRegistryHost,
}
}
// ApplyEnv sets the properties of cfg which may be set via environment
// variables to the values these variables contain.
//
// ApplyEnv does not change the dirty state of config.
func (cfg *Config) ApplyEnv() {
cfg.mu.Lock()
defer cfg.mu.Unlock()
cfg.AccessToken = env.FirstOrDefault(cfg.AccessToken,
AccessTokenEnvKey, APITokenEnvKey)
// trim whitespace since it causes http errors when passsed to Docker auth
cfg.AccessToken = strings.TrimSpace(cfg.AccessToken)
cfg.VerboseOutput = env.IsTruthy(verboseOutputEnvKey) || cfg.VerboseOutput
cfg.JSONOutput = env.IsTruthy(jsonOutputEnvKey) || cfg.JSONOutput
cfg.LogGQLErrors = env.IsTruthy(logGQLEnvKey) || cfg.LogGQLErrors
cfg.LocalOnly = env.IsTruthy(localOnlyEnvKey) || cfg.LocalOnly
cfg.Organization = env.FirstOrDefault(cfg.Organization,
orgEnvKey, organizationEnvKey)
cfg.Region = env.FirstOrDefault(cfg.Region, regionEnvKey)
cfg.RegistryHost = env.FirstOrDefault(cfg.RegistryHost, registryHostEnvKey)
cfg.APIBaseURL = env.FirstOrDefault(cfg.APIBaseURL, apiBaseURLEnvKey)
}
// ApplyFile sets the properties of cfg which may be set via configuration file
// to the values the file at the given path contains.
func (cfg *Config) ApplyFile(path string) (err error) {
cfg.mu.Lock()
defer cfg.mu.Unlock()
var w struct {
AccessToken string `yaml:"access_token"`
}
if err = unmarshal(path, &w); err == nil {
cfg.AccessToken = w.AccessToken
}
return
}
// ApplyFlags sets the properties of cfg which may be set via command line flags
// to the values the flags of the given FlagSet may contain.
func (cfg *Config) ApplyFlags(fs *pflag.FlagSet) {
cfg.mu.Lock()
defer cfg.mu.Unlock()
applyStringFlags(fs, map[string]*string{
flag.AccessTokenName: &cfg.AccessToken,
flag.OrgName: &cfg.Organization,
flag.RegionName: &cfg.Region,
})
applyBoolFlags(fs, map[string]*bool{
flag.VerboseName: &cfg.VerboseOutput,
flag.JSONOutputName: &cfg.JSONOutput,
flag.LocalOnlyName: &cfg.LocalOnly,
})
}
func applyStringFlags(fs *pflag.FlagSet, flags map[string]*string) {
for name, dst := range flags {
if !fs.Changed(name) {
continue
}
if v, err := fs.GetString(name); err != nil {
panic(err)
} else {
*dst = v
}
}
}
func applyBoolFlags(fs *pflag.FlagSet, flags map[string]*bool) {
for name, dst := range flags {
if !fs.Changed(name) {
continue
}
if v, err := fs.GetBool(name); err != nil {
panic(err)
} else {
*dst = v
}
}
}