-
Notifications
You must be signed in to change notification settings - Fork 152
/
context.go
222 lines (182 loc) · 5.43 KB
/
context.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package core
import (
"context"
"io"
"net/http"
"os"
"path"
"github.com/scaleway/scaleway-cli/v2/internal/alias"
cliConfig "github.com/scaleway/scaleway-cli/v2/internal/config"
"github.com/scaleway/scaleway-cli/v2/internal/platform"
"github.com/scaleway/scaleway-sdk-go/scw"
)
// meta store globally available variables like sdk client or global Flags.
type meta struct {
BinaryName string
ProfileFlag string
ConfigPathFlag string
Logger *Logger
BuildInfo *BuildInfo
Client *scw.Client
Commands *Commands
OverrideEnv map[string]string
OverrideExec OverrideExecFunc
CliConfig *cliConfig.Config
Platform platform.Platform
command *Command
stdout io.Writer
stderr io.Writer
stdin io.Reader
result interface{}
httpClient *http.Client
isClientFromBootstrapConfig bool
betaMode bool
}
type contextKey int
const (
metaContextKey contextKey = iota
)
// injectMeta creates a new ctx based on the given one with injected meta and returns it.
func injectMeta(ctx context.Context, meta *meta) context.Context {
return context.WithValue(ctx, metaContextKey, meta)
}
// extractMeta extracts meta from a given context.
func extractMeta(ctx context.Context) *meta {
return ctx.Value(metaContextKey).(*meta)
}
// injectSDKConfig add config to a meta context
func injectConfig(ctx context.Context, config *scw.Config) {
extractMeta(ctx).Platform.SetScwConfig(config)
}
func extractConfig(ctx context.Context) *scw.Config {
m := extractMeta(ctx)
if m.Platform != nil {
return m.Platform.ScwConfig()
}
return nil
}
func ExtractCommands(ctx context.Context) *Commands {
return extractMeta(ctx).Commands
}
func ExtractCliConfig(ctx context.Context) *cliConfig.Config {
return extractMeta(ctx).CliConfig
}
func ExtractAliases(ctx context.Context) *alias.Config {
return ExtractCliConfig(ctx).Alias
}
func GetOrganizationIDFromContext(ctx context.Context) string {
client := ExtractClient(ctx)
organizationID, _ := client.GetDefaultOrganizationID()
return organizationID
}
func GetProjectIDFromContext(ctx context.Context) string {
client := ExtractClient(ctx)
projectID, exists := client.GetDefaultProjectID()
if !exists {
panic("no default project ID found")
}
return projectID
}
func ExtractClient(ctx context.Context) *scw.Client {
return extractMeta(ctx).Client
}
func ExtractLogger(ctx context.Context) *Logger {
return extractMeta(ctx).Logger
}
func ExtractBuildInfo(ctx context.Context) *BuildInfo {
return extractMeta(ctx).BuildInfo
}
func ExtractBetaMode(ctx context.Context) bool {
return extractMeta(ctx).betaMode
}
func ExtractEnv(ctx context.Context, envKey string) string {
meta := extractMeta(ctx)
if value, exist := meta.OverrideEnv[envKey]; exist {
return value
}
if envKey == "HOME" {
homeDir, _ := os.UserHomeDir()
return homeDir
}
return os.Getenv(envKey)
}
func ExtractUserHomeDir(ctx context.Context) string {
return ExtractEnv(ctx, "HOME")
}
func ExtractCacheDir(ctx context.Context) string {
env := ExtractEnv(ctx, scw.ScwCacheDirEnv)
if env != "" {
return env
}
return scw.GetCacheDirectory()
}
func ExtractBinaryName(ctx context.Context) string {
return extractMeta(ctx).BinaryName
}
func ExtractStdin(ctx context.Context) io.Reader {
return extractMeta(ctx).stdin
}
func ExtractProfileName(ctx context.Context) string {
// Handle profile flag -p
if extractMeta(ctx).ProfileFlag != "" {
return extractMeta(ctx).ProfileFlag
}
// Handle SCW_PROFILE env variable
if env := ExtractEnv(ctx, scw.ScwActiveProfileEnv); env != "" {
return env
}
// Handle active_profile in config file
configPath := ExtractConfigPath(ctx)
config, err := scw.LoadConfigFromPath(configPath)
if err == nil && config.ActiveProfile != nil {
return *config.ActiveProfile
}
// Return default profile name
return scw.DefaultProfileName
}
func ExtractHTTPClient(ctx context.Context) *http.Client {
return extractMeta(ctx).httpClient
}
func ExtractConfigPath(ctx context.Context) string {
meta := extractMeta(ctx)
if meta.ConfigPathFlag != "" {
return extractMeta(ctx).ConfigPathFlag
}
// This is only useful for test when we override home environment variable
if home := meta.OverrideEnv["HOME"]; home != "" {
return path.Join(home, ".config", "scw", "config.yaml")
}
return scw.GetConfigPath()
}
func ExtractCliConfigPath(ctx context.Context) string {
meta := extractMeta(ctx)
// This is only useful for test when we override home environment variable
if home := meta.OverrideEnv["HOME"]; home != "" {
return path.Join(home, ".config", "scw", cliConfig.DefaultConfigFileName)
}
configPath, _ := cliConfig.FilePath()
return configPath
}
func ReloadClient(ctx context.Context) error {
var err error
meta := extractMeta(ctx)
meta.Client, err = meta.Platform.CreateClient(meta.httpClient, ExtractConfigPath(ctx), ExtractProfileName(ctx))
return err
}
func ExtractConfigPathFlag(ctx context.Context) string {
return extractMeta(ctx).ConfigPathFlag
}
func ExtractProfileFlag(ctx context.Context) string {
return extractMeta(ctx).ProfileFlag
}
// GetDocGenContext returns a minimal context that can be used by scw-doc-gen
func GetDocGenContext() context.Context {
ctx := context.Background()
client, _ := scw.NewClient(
scw.WithDefaultZone(scw.ZoneFrPar1),
scw.WithDefaultRegion(scw.RegionFrPar),
)
return injectMeta(ctx, &meta{
Client: client,
})
}