|
1 | 1 | package core |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "bytes" |
4 | 5 | "context" |
5 | 6 | "fmt" |
6 | 7 | "os" |
7 | 8 | "path/filepath" |
| 9 | + "reflect" |
8 | 10 | "time" |
9 | 11 |
|
10 | 12 | iam "github.com/scaleway/scaleway-sdk-go/api/iam/v1alpha1" |
| 13 | + "github.com/scaleway/scaleway-sdk-go/scw" |
11 | 14 | ) |
12 | 15 |
|
13 | 16 | var ( |
14 | 17 | apiKeyExpireTime = 24 * time.Hour |
15 | 18 | lastChecksFileLocalName = "last-cli-checks" |
16 | 19 | ) |
17 | 20 |
|
| 21 | +const ( |
| 22 | + defaultCredentialSource = "environment variable" |
| 23 | +) |
| 24 | + |
18 | 25 | type AfterCommandCheckFunc func(ctx context.Context) |
19 | 26 |
|
20 | 27 | // wasFileModifiedLast24h checks whether the file has been updated during last 24 hours. |
@@ -105,3 +112,43 @@ func checkAPIKey(ctx context.Context) { |
105 | 112 | ExtractLogger(ctx).Warningf("Current api key expires in %s\n", expiresIn) |
106 | 113 | } |
107 | 114 | } |
| 115 | + |
| 116 | +// checkIfMultipleVariableSources return an informative message during the CLI initialization |
| 117 | +// if there are multiple sources of configuration that could confuse the user |
| 118 | +func checkIfMultipleVariableSources(ctx context.Context) { |
| 119 | + config, err := scw.LoadConfigFromPath(ExtractConfigPath(ctx)) |
| 120 | + if err != nil { |
| 121 | + return |
| 122 | + } |
| 123 | + |
| 124 | + activeProfile, err := config.GetActiveProfile() |
| 125 | + if err != nil { |
| 126 | + return |
| 127 | + } |
| 128 | + |
| 129 | + profileEnv := scw.LoadEnvProfile() |
| 130 | + |
| 131 | + vFile := reflect.ValueOf(activeProfile).Elem() |
| 132 | + vEnv := reflect.ValueOf(profileEnv).Elem() |
| 133 | + t := vFile.Type() |
| 134 | + |
| 135 | + var buffer bytes.Buffer |
| 136 | + buffer.WriteString("Checking multiple variable sources: \n") |
| 137 | + |
| 138 | + for i := range t.NumField() { |
| 139 | + valFile := vFile.Field(i) |
| 140 | + valEnv := vEnv.Field(i) |
| 141 | + |
| 142 | + if !valFile.IsNil() && !valEnv.IsNil() { |
| 143 | + if valFile.Elem().String() != valEnv.Elem().String() { |
| 144 | + buffer.WriteString(fmt.Sprintf( |
| 145 | + "- Variable '%s' is defined in both config.yaml and environment with different values. Using: %s.\n", |
| 146 | + t.Field(i).Name, |
| 147 | + defaultCredentialSource, |
| 148 | + )) |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + ExtractLogger(ctx).Warning(buffer.String()) |
| 154 | +} |
0 commit comments