Skip to content

Commit e7f31b9

Browse files
author
Mélanie Marques
committed
feat: add warning message if multiple variable sources
1 parent 3a1f808 commit e7f31b9

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

core/bootstrap.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,12 @@ func Bootstrap(config *BootstrapConfig) (exitCode int, result any, err error) {
236236
// Run checks after command has been executed
237237
defer func() { // if we plan to remove defer, do not forget logger is not set until cobra pre init func
238238
// Check CLI new version and api key expiration date
239-
runAfterCommandChecks(ctx, config.BuildInfo.checkVersion, checkAPIKey)
239+
runAfterCommandChecks(
240+
ctx,
241+
config.BuildInfo.checkVersion,
242+
checkAPIKey,
243+
checkIfMultipleVariableSources,
244+
)
240245
}()
241246

242247
if !config.DisableAliases {

core/checks.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,27 @@
11
package core
22

33
import (
4+
"bytes"
45
"context"
56
"fmt"
67
"os"
78
"path/filepath"
9+
"reflect"
810
"time"
911

1012
iam "github.com/scaleway/scaleway-sdk-go/api/iam/v1alpha1"
13+
"github.com/scaleway/scaleway-sdk-go/scw"
1114
)
1215

1316
var (
1417
apiKeyExpireTime = 24 * time.Hour
1518
lastChecksFileLocalName = "last-cli-checks"
1619
)
1720

21+
const (
22+
defaultCredentialSource = "environment variable"
23+
)
24+
1825
type AfterCommandCheckFunc func(ctx context.Context)
1926

2027
// wasFileModifiedLast24h checks whether the file has been updated during last 24 hours.
@@ -105,3 +112,43 @@ func checkAPIKey(ctx context.Context) {
105112
ExtractLogger(ctx).Warningf("Current api key expires in %s\n", expiresIn)
106113
}
107114
}
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

Comments
 (0)