Skip to content

Commit da3e970

Browse files
committed
Add more unit test and change the way the configPath option is stored
1 parent 03cc9fb commit da3e970

File tree

10 files changed

+80
-42
lines changed

10 files changed

+80
-42
lines changed

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,10 @@ profile.out
1212
.DS_STORE
1313
.vendor
1414
/.vscode
15+
16+
# Vim Swapfiles
17+
[._]*.s[a-v][a-z]
18+
[._]*.sw[a-p]
19+
[._]s[a-rt-v][a-z]
20+
[._]ss[a-gi-z]
21+
[._]sw[a-p]

pkg/cli/command.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,19 @@ type Command struct {
5151
// API is the interface used to communicate with Scaleway's API
5252
API *api.ScalewayAPI
5353

54+
// ConfigPath for -c, --config parameter
55+
ConfigPath string
56+
5457
streams *commands.Streams
5558
}
5659

5760
// GetContext returns a standard context, with real stdin, stdout, stderr, a configured API and raw arguments
5861
func (c *Command) GetContext(rawArgs []string) commands.CommandContext {
5962
ctx := commands.CommandContext{
60-
Env: os.Environ(),
61-
RawArgs: rawArgs,
62-
API: c.API,
63+
Env: os.Environ(),
64+
RawArgs: rawArgs,
65+
API: c.API,
66+
ConfigPath: c.ConfigPath,
6367
}
6468

6569
if c.streams != nil {

pkg/cli/main.go

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,7 @@ func Start(rawArgs []string, streams *commands.Streams) (int, error) {
4848
}
4949
flag.CommandLine.Parse(rawArgs)
5050

51-
if *flConfig != "" {
52-
os.Setenv("SCW_CONFIG_PATH", *flConfig)
53-
}
54-
55-
config, cfgErr := config.GetConfig()
51+
config, cfgErr := config.GetConfig(*flConfig)
5652
if cfgErr != nil && !os.IsNotExist(cfgErr) {
5753
return 1, fmt.Errorf("unable to open .scwrc config file: %v", cfgErr)
5854
}
@@ -98,12 +94,13 @@ func Start(rawArgs []string, streams *commands.Streams) (int, error) {
9894
if err != nil {
9995
return 1, fmt.Errorf("usage: scw %s", cmd.UsageLine)
10096
}
97+
cmd.ConfigPath = *flConfig
10198
switch cmd.Name() {
10299
case "login", "help", "version":
103100
// commands that don't need API
104101
case "_userdata":
105102
// commands that may need API
106-
api, _ := getScalewayAPI(*flRegion)
103+
api, _ := getScalewayAPI(*flRegion, *flConfig)
107104
cmd.API = api
108105
default:
109106
// commands that do need API
@@ -114,7 +111,7 @@ func Start(rawArgs []string, streams *commands.Streams) (int, error) {
114111
return 1, nil
115112
}
116113
}
117-
api, errGet := getScalewayAPI(*flRegion)
114+
api, errGet := getScalewayAPI(*flRegion, *flConfig)
118115
if errGet != nil {
119116
return 1, fmt.Errorf("unable to initialize scw api: %v", errGet)
120117
}
@@ -123,7 +120,7 @@ func Start(rawArgs []string, streams *commands.Streams) (int, error) {
123120
// clean cache between versions
124121
if cmd.API != nil && config.Version != scwversion.VERSION {
125122
cmd.API.ClearCache()
126-
config.Save()
123+
config.Save(*flConfig)
127124
}
128125
err = cmd.Exec(cmd, cmd.Flag.Args())
129126
switch err {
@@ -145,9 +142,9 @@ func Start(rawArgs []string, streams *commands.Streams) (int, error) {
145142
}
146143

147144
// getScalewayAPI returns a ScalewayAPI using the user config file
148-
func getScalewayAPI(region string) (*api.ScalewayAPI, error) {
145+
func getScalewayAPI(region string, configPath string) (*api.ScalewayAPI, error) {
149146
// We already get config globally, but whis way we can get explicit error when trying to create a ScalewayAPI object
150-
config, err := config.GetConfig()
147+
config, err := config.GetConfig(configPath)
151148
if err != nil {
152149
return nil, err
153150
}

pkg/commands/command.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@ type Streams struct {
2323
type CommandContext struct {
2424
Streams
2525

26-
Env []string
27-
RawArgs []string
28-
API *api.ScalewayAPI
26+
Env []string
27+
RawArgs []string
28+
API *api.ScalewayAPI
29+
ConfigPath string
2930
}
3031

3132
// Getenv returns the equivalent of os.Getenv for the CommandContext.Env

pkg/commands/login.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ func uploadSSHKeys(apiConnection *api.ScalewayAPI, newKey string) {
268268

269269
// RunLogin is the handler for 'scw login'
270270
func RunLogin(ctx CommandContext, args LoginArgs) error {
271-
if config, cfgErr := config.GetConfig(); cfgErr == nil {
271+
if config, cfgErr := config.GetConfig(ctx.ConfigPath); cfgErr == nil {
272272
if TestConnection, err := api.NewScalewayAPI(config.Organization, config.Token, scwversion.UserAgent(), "", clilogger.SetupLogger); err == nil {
273273
if user, err := TestConnection.GetUser(); err == nil {
274274
fmt.Println("You are already logged as", user.Fullname)
@@ -317,7 +317,7 @@ func RunLogin(ctx CommandContext, args LoginArgs) error {
317317
fmt.Println("You can list your existing servers using `scw ps` or create a new one using `scw run ubuntu-xenial`.")
318318
fmt.Println("You can get a list of all available commands using `scw -h` and get more usage examples on github.com/scaleway/scaleway-cli.")
319319
fmt.Println("Happy cloud riding.")
320-
return cfg.Save()
320+
return cfg.Save(ctx.ConfigPath)
321321
}
322322

323323
func promptUser(prompt string, output *string, echo bool) error {

pkg/commands/test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func RealAPIContext() *CommandContext {
3939
if os.Getenv("TEST_WITH_REAL_API") == "0" {
4040
return nil
4141
}
42-
config, err := config.GetConfig()
42+
config, err := config.GetConfig("")
4343
if err != nil {
4444
logrus.Warnf("RealAPIContext: failed to call config.GetConfig(): %v", err)
4545
return nil

pkg/config/config.go

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,14 @@ type Config struct {
3030
}
3131

3232
// Save write the config file
33-
func (c *Config) Save() error {
34-
scwrcPath, err := GetConfigFilePath()
35-
if err != nil {
36-
return fmt.Errorf("Unable to get scwrc config file path: %s", err)
33+
func (c *Config) Save(configPath string) error {
34+
scwrcPath := configPath
35+
var err error
36+
if configPath == "" {
37+
scwrcPath, err = GetConfigFilePath()
38+
if err != nil {
39+
return fmt.Errorf("Unable to get scwrc config file path: %s", err)
40+
}
3741
}
3842
scwrc, err := os.OpenFile(scwrcPath, os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0600)
3943
if err != nil {
@@ -49,10 +53,13 @@ func (c *Config) Save() error {
4953
}
5054

5155
// GetConfig returns the Scaleway CLI config file for the current user
52-
func GetConfig() (*Config, error) {
53-
scwrcPath, err := GetConfigFilePath()
54-
if err != nil {
55-
return nil, err
56+
func GetConfig(scwrcPath string) (*Config, error) {
57+
var err error
58+
if scwrcPath == "" {
59+
scwrcPath, err = GetConfigFilePath()
60+
if err != nil {
61+
return nil, err
62+
}
5663
}
5764

5865
// Don't check permissions on Windows, Go knows nothing about them on this platform
@@ -63,7 +70,7 @@ func GetConfig() (*Config, error) {
6370
if errStat == nil {
6471
perm := stat.Mode().Perm()
6572
if perm&0066 != 0 {
66-
return nil, fmt.Errorf("permissions %#o for .scwrc are too open", perm)
73+
return nil, fmt.Errorf("permissions %#o for %s are too open", perm, scwrcPath)
6774
}
6875
}
6976
}

pkg/config/config_test.go

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
package config
66

77
import (
8+
"math/rand"
9+
"time"
810
"os"
11+
"strconv"
912
"strings"
1013
"testing"
11-
"strconv"
12-
"math/rand"
1314

1415
"github.com/scaleway/scaleway-cli/pkg/scwversion"
1516
. "github.com/smartystreets/goconvey/convey"
@@ -38,37 +39,60 @@ func TestGetConfigFilePathEnv(t *testing.T) {
3839
}
3940

4041
func TestGetConfig(t *testing.T) {
41-
Convey("Testing GetConfig() with env variable", t, func() {
42+
Convey("Testing GetConfig() with and without env variable", t, func() {
4243
os.Setenv("SCW_CONFIG_PATH", "./config_testdata1")
43-
config, err := GetConfig()
44+
cfg := &Config{
45+
Organization: strings.Trim("test_orgID", "\n"),
46+
Token: strings.Trim("test_token", "\n"),
47+
}
48+
err := cfg.Save("")
4449
So(err, ShouldBeNil)
45-
So(config.Version, ShouldEqual, "test_version")
46-
So(config.Organization, ShouldEqual, "test_orgID")
47-
So(config.Token, ShouldEqual, "test_token")
50+
cfg, err = GetConfig("./config_testdata1")
51+
So(cfg.Organization, ShouldEqual, "test_orgID")
52+
So(cfg.Token, ShouldEqual, "test_token")
4853
os.Unsetenv("SCW_CONFIG_PATH")
54+
cfg, err = GetConfig("./config_testdata1")
55+
So(err, ShouldBeNil)
56+
So(cfg.Organization, ShouldEqual, "test_orgID")
57+
So(cfg.Token, ShouldEqual, "test_token")
4958
})
5059
}
5160

5261
func TestSave(t *testing.T) {
53-
Convey("Testing SaveConfig() with env variable", t, func() {
62+
Convey("Testing SaveConfig() with and without env variable", t, func() {
5463
os.Setenv("SCW_CONFIG_PATH", "./config_testdata2")
64+
rand.Seed(time.Now().UTC().UnixNano())
5565
randOrg := strconv.FormatInt(rand.Int63(), 16)
5666
randToken := strconv.FormatInt(rand.Int63(), 16)
5767
cfg := &Config{
5868
Organization: strings.Trim(randOrg, "\n"),
5969
Token: strings.Trim(randToken, "\n"),
6070
}
61-
err := cfg.Save()
71+
err := cfg.Save("")
72+
So(err, ShouldBeNil)
73+
cfg, err = GetConfig("")
6274
So(err, ShouldBeNil)
6375
So(cfg.Version, ShouldEqual, scwversion.VERSION)
6476
So(cfg.Organization, ShouldEqual, randOrg)
6577
So(cfg.Token, ShouldEqual, randToken)
6678
os.Unsetenv("SCW_CONFIG_PATH")
67-
})
68-
}
69-
7079

80+
randOrg = strconv.FormatInt(rand.Int63(), 16)
81+
randToken = strconv.FormatInt(rand.Int63(), 16)
82+
cfg = &Config{
83+
Organization: strings.Trim(randOrg, "\n"),
84+
Token: strings.Trim(randToken, "\n"),
85+
}
86+
err = cfg.Save("./config_testdata2")
87+
So(err, ShouldBeNil)
88+
cfg, err = GetConfig("./config_testdata2")
89+
So(err, ShouldBeNil)
90+
So(cfg.Version, ShouldEqual, scwversion.VERSION)
91+
So(cfg.Organization, ShouldEqual, randOrg)
92+
So(cfg.Token, ShouldEqual, randToken)
7193

94+
})
95+
}
7296

7397
func TestGetHomeDir(t *testing.T) {
7498
Convey("Testing GetHomeDir()", t, func() {

pkg/config/config_testdata1

Lines changed: 0 additions & 1 deletion
This file was deleted.

pkg/config/config_testdata2

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)