-
Notifications
You must be signed in to change notification settings - Fork 57
/
global.go
206 lines (183 loc) · 6.9 KB
/
global.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package global
import (
"io"
"github.com/fastly/cli/pkg/api"
"github.com/fastly/cli/pkg/auth"
"github.com/fastly/cli/pkg/config"
fsterr "github.com/fastly/cli/pkg/errors"
"github.com/fastly/cli/pkg/github"
"github.com/fastly/cli/pkg/lookup"
"github.com/fastly/cli/pkg/manifest"
)
// DefaultAPIEndpoint is the default Fastly API endpoint.
const DefaultAPIEndpoint = "https://api.fastly.com"
// DefaultAccountEndpoint is the default Fastly Accounts endpoint.
const DefaultAccountEndpoint = "https://accounts.fastly.com"
// APIClientFactory creates a Fastly API client (modeled as an api.Interface)
// from a user-provided API token. It exists as a type in order to parameterize
// the Run helper with it: in the real CLI, we can use NewClient from the Fastly
// API client library via RealClient; in tests, we can provide a mock API
// interface via MockClient.
type APIClientFactory func(token, apiEndpoint string, debugMode bool) (api.Interface, error)
// Versioners represents all supported versioner types.
type Versioners struct {
CLI github.AssetVersioner
Viceroy github.AssetVersioner
WasmTools github.AssetVersioner
}
// Data holds global-ish configuration data from all sources: environment
// variables, config files, and flags. It has methods to give each parameter to
// the components that need it, including the place the parameter came from,
// which is a requirement.
//
// If the same parameter is defined in multiple places, it is resolved according
// to the following priority order: the config file (lowest priority), env vars,
// and then explicit flags (highest priority).
//
// This package and its types are only meant for parameters that are applicable
// to most/all subcommands (e.g. API token) and are consistent for a given user
// (e.g. an email address). Otherwise, parameters should be defined in specific
// command structs, and parsed as flags.
type Data struct {
// APIClient is a Fastly API client instance.
APIClient api.Interface
// APIClientFactory is a factory function for creating an api.Interface type.
APIClientFactory APIClientFactory
// Args are the command line arguments provided by the user.
Args []string
// AuthServer is an instance of the authentication server type.
// Used for interacting with Fastly's SSO/OAuth authentication provider.
AuthServer auth.Runner
// Config is an instance of the CLI configuration data.
Config config.File
// ConfigPath is the path to the CLI's application configuration.
ConfigPath string
// Env is all the data that is provided by the environment.
Env config.Environment
// ErrLog provides an interface for recording errors to disk.
ErrLog fsterr.LogInterface
// ExecuteWasmTools is a function that executes the wasm-tools binary.
ExecuteWasmTools func(bin string, args []string) error
// Flags are all the global CLI flags.
Flags Flags
// HTTPClient is a HTTP client.
HTTPClient api.HTTPClient
// Input is the standard input for accepting input from the user.
Input io.Reader
// Manifest represents the fastly.toml manifest file and associated flags.
Manifest *manifest.Data
// Opener is a function that can open a browser window.
Opener func(string) error
// Output is the output for displaying information (typically os.Stdout)
Output io.Writer
// RTSClient is a Fastly API client instance for the Real Time Stats endpoints.
RTSClient api.RealtimeStatsInterface
// SkipAuthPrompt is used to indicate to the `sso` command that the
// interactive prompt can be skipped. This is for scenarios where the command
// is executed directly by the user.
SkipAuthPrompt bool
// Versioners contains multiple software versioning checkers.
// e.g. Check for latest CLI or Viceroy version.
Versioners Versioners
}
// Token yields the Fastly API token.
//
// Order of precedence:
// - The --token flag.
// - The FASTLY_API_TOKEN environment variable.
// - The --profile flag's associated token.
// - The `profile` manifest field's associated profile token.
// - The 'default' profile associated token (if there is one).
func (d *Data) Token() (string, lookup.Source) {
// --token
if d.Flags.Token != "" {
return d.Flags.Token, lookup.SourceFlag
}
// FASTLY_API_TOKEN
if d.Env.APIToken != "" {
return d.Env.APIToken, lookup.SourceEnvironment
}
// --profile
if d.Flags.Profile != "" {
for k, v := range d.Config.Profiles {
if k == d.Flags.Profile {
return v.Token, lookup.SourceFile
}
}
}
// `profile` field in fastly.toml
if d.Manifest.File.Profile != "" {
for k, v := range d.Config.Profiles {
if k == d.Manifest.File.Profile {
return v.Token, lookup.SourceFile
}
}
}
// [profile] section in app config
for _, v := range d.Config.Profiles {
if v.Default {
return v.Token, lookup.SourceFile
}
}
return "", lookup.SourceUndefined
}
// Verbose yields the verbose flag, which can only be set via flags.
func (d *Data) Verbose() bool {
return d.Flags.Verbose
}
// APIEndpoint yields the API endpoint.
func (d *Data) APIEndpoint() (string, lookup.Source) {
if d.Flags.Endpoint != "" {
return d.Flags.Endpoint, lookup.SourceFlag
}
if d.Env.Endpoint != "" {
return d.Env.Endpoint, lookup.SourceEnvironment
}
if d.Config.Fastly.APIEndpoint != DefaultAPIEndpoint && d.Config.Fastly.APIEndpoint != "" {
return d.Config.Fastly.APIEndpoint, lookup.SourceFile
}
return DefaultAPIEndpoint, lookup.SourceDefault // this method should not fail
}
// AccountEndpoint yields the Accounts endpoint.
func (d *Data) AccountEndpoint() (string, lookup.Source) {
if d.Flags.Account != "" {
return d.Flags.Account, lookup.SourceFlag
}
if d.Env.Account != "" {
return d.Env.Account, lookup.SourceEnvironment
}
if d.Config.Fastly.AccountEndpoint != DefaultAccountEndpoint && d.Config.Fastly.AccountEndpoint != "" {
return d.Config.Fastly.AccountEndpoint, lookup.SourceFile
}
return DefaultAccountEndpoint, lookup.SourceDefault // this method should not fail
}
// Flags represents all of the configuration parameters that can be set with
// explicit flags. Consumers should bind their flag values to these fields
// directly.
//
// IMPORTANT: Kingpin doesn't support global flags.
// We hack a solution in ../app/run.go (`configureKingpin` function).
type Flags struct {
// AcceptDefaults auto-resolves prompts with a default defined.
AcceptDefaults bool
// Account is the authentication host address.
Account string
// AutoYes auto-resolves Yes/No prompts by answering "Yes".
AutoYes bool
// Debug enables the CLI's debug mode.
Debug bool
// Endpoint is the Fastly API address.
Endpoint string
// NonInteractive auto-resolves all prompts.
NonInteractive bool
// Profile indicates the profile to use (consequently the 'token' used).
Profile string
// Quiet silences all output except direct command output.
Quiet bool
// SSO enables to SSO authentication tokens for the current profile.
SSO bool
// Token is an override for a profile (when passed SSO is disabled).
Token string
// Verbose prints additional output.
Verbose bool
}