Skip to content

Commit 03cc9fb

Browse files
committed
Add --config option for arbitrary config file
1 parent 65d3dbe commit 03cc9fb

File tree

6 files changed

+59
-0
lines changed

6 files changed

+59
-0
lines changed

pkg/cli/cmd_help.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ Options:
4444
--sensitive=false Show sensitive data in outputs, i.e. API Token/Organization
4545
-v, --version=false Print version information and quit
4646
--region=par1 Change the default region (e.g. ams1)
47+
-c, --config=<config path> Option config file path (default to ~/.scwrc)
4748
4849
Commands:
4950
{{range .}}{{if not .Hidden}} {{.Name | printf "%-9s"}} {{.Description}}

pkg/cli/main.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ var (
3333
flQuiet = flag.Bool([]string{"q", "-quiet"}, false, "Enable quiet mode")
3434
flSensitive = flag.Bool([]string{"-sensitive"}, false, "Show sensitive data in outputs, i.e. API Token/Organization")
3535
flRegion = flag.String([]string{"-region"}, "par1", "Change the default region (e.g. ams1)")
36+
flConfig = flag.String([]string{"c", "-config"}, "", "Optional config file path")
3637
)
3738

3839
// Start is the entrypoint
@@ -47,6 +48,10 @@ func Start(rawArgs []string, streams *commands.Streams) (int, error) {
4748
}
4849
flag.CommandLine.Parse(rawArgs)
4950

51+
if *flConfig != "" {
52+
os.Setenv("SCW_CONFIG_PATH", *flConfig)
53+
}
54+
5055
config, cfgErr := config.GetConfig()
5156
if cfgErr != nil && !os.IsNotExist(cfgErr) {
5257
return 1, fmt.Errorf("unable to open .scwrc config file: %v", cfgErr)

pkg/config/config.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ func GetConfig() (*Config, error) {
8383

8484
// GetConfigFilePath returns the path to the Scaleway CLI config file
8585
func GetConfigFilePath() (string, error) {
86+
path := os.Getenv("SCW_CONFIG_PATH")
87+
if path != "" {
88+
return path, nil
89+
}
8690
path, err := GetHomeDir()
8791
if err != nil {
8892
return "", err

pkg/config/config_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@
55
package config
66

77
import (
8+
"os"
89
"strings"
910
"testing"
11+
"strconv"
12+
"math/rand"
1013

14+
"github.com/scaleway/scaleway-cli/pkg/scwversion"
1115
. "github.com/smartystreets/goconvey/convey"
1216
)
1317

@@ -23,6 +27,49 @@ func TestGetConfigFilePath(t *testing.T) {
2327
})
2428
}
2529

30+
func TestGetConfigFilePathEnv(t *testing.T) {
31+
Convey("Testing GetConfigFilePath() with env variable", t, func() {
32+
os.Setenv("SCW_CONFIG_PATH", "./config_testdata1")
33+
configPath, err := GetConfigFilePath()
34+
So(err, ShouldBeNil)
35+
So(configPath, ShouldEqual, "./config_testdata1")
36+
os.Unsetenv("SCW_CONFIG_PATH")
37+
})
38+
}
39+
40+
func TestGetConfig(t *testing.T) {
41+
Convey("Testing GetConfig() with env variable", t, func() {
42+
os.Setenv("SCW_CONFIG_PATH", "./config_testdata1")
43+
config, err := GetConfig()
44+
So(err, ShouldBeNil)
45+
So(config.Version, ShouldEqual, "test_version")
46+
So(config.Organization, ShouldEqual, "test_orgID")
47+
So(config.Token, ShouldEqual, "test_token")
48+
os.Unsetenv("SCW_CONFIG_PATH")
49+
})
50+
}
51+
52+
func TestSave(t *testing.T) {
53+
Convey("Testing SaveConfig() with env variable", t, func() {
54+
os.Setenv("SCW_CONFIG_PATH", "./config_testdata2")
55+
randOrg := strconv.FormatInt(rand.Int63(), 16)
56+
randToken := strconv.FormatInt(rand.Int63(), 16)
57+
cfg := &Config{
58+
Organization: strings.Trim(randOrg, "\n"),
59+
Token: strings.Trim(randToken, "\n"),
60+
}
61+
err := cfg.Save()
62+
So(err, ShouldBeNil)
63+
So(cfg.Version, ShouldEqual, scwversion.VERSION)
64+
So(cfg.Organization, ShouldEqual, randOrg)
65+
So(cfg.Token, ShouldEqual, randToken)
66+
os.Unsetenv("SCW_CONFIG_PATH")
67+
})
68+
}
69+
70+
71+
72+
2673
func TestGetHomeDir(t *testing.T) {
2774
Convey("Testing GetHomeDir()", t, func() {
2875
homedir, err := GetHomeDir()

pkg/config/config_testdata1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"organization":"test_orgID","token":"test_token","version":"test_version"}

pkg/config/config_testdata2

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"organization":"4d65822107fcfd52","token":"78629a0f5f3f164f","version":"v1.17+dev"}

0 commit comments

Comments
 (0)