Skip to content

Commit 1ead5cc

Browse files
authored
fix(core): do not require a config file if required env vars are set (#2550)
1 parent 87c712f commit 1ead5cc

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

internal/core/client.go

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package core
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"net/http"
78
"strings"
@@ -13,30 +14,36 @@ import (
1314

1415
// createClient creates a Scaleway SDK client.
1516
func createClient(ctx context.Context, httpClient *http.Client, buildInfo *BuildInfo, profileName string) (*scw.Client, error) {
16-
_, err := scw.MigrateLegacyConfig()
17-
if err != nil {
17+
if _, err := scw.MigrateLegacyConfig(); err != nil {
1818
return nil, err
1919
}
2020

21+
profile := scw.LoadEnvProfile()
22+
2123
// Default path is based on the following priority order:
2224
// * The config file's path provided via --config flag
2325
// * $SCW_CONFIG_PATH
2426
// * $XDG_CONFIG_HOME/scw/config.yaml
2527
// * $HOME/.config/scw/config.yaml
2628
// * $USERPROFILE/.config/scw/config.yaml
2729
config, err := scw.LoadConfigFromPath(ExtractConfigPath(ctx))
28-
if err != nil {
29-
return nil, err
30-
}
30+
switch {
31+
case errIsConfigFileNotFound(err):
32+
// no config file was found -> nop
3133

32-
activeProfile, err := config.GetProfile(profileName)
33-
if err != nil {
34+
case err != nil:
35+
// failed to read the config file -> fail
3436
return nil, err
35-
}
3637

37-
envProfile := scw.LoadEnvProfile()
38+
default:
39+
// found and loaded a config file -> merge with env
40+
activeProfile, err := config.GetProfile(profileName)
41+
if err != nil {
42+
return nil, err
43+
}
3844

39-
profile := scw.MergeProfiles(activeProfile, envProfile)
45+
profile = scw.MergeProfiles(activeProfile, profile)
46+
}
4047

4148
// If profile have a defaultZone but no defaultRegion we set the defaultRegion
4249
// to the one of the defaultZone
@@ -199,3 +206,8 @@ func validateClient(client *scw.Client) error {
199206

200207
return nil
201208
}
209+
210+
func errIsConfigFileNotFound(err error) bool {
211+
var target *scw.ConfigFileNotFoundError
212+
return errors.As(err, &target)
213+
}

0 commit comments

Comments
 (0)