Skip to content

Commit 3d25fe3

Browse files
committed
Add the ability to set the credentials via env variable
SCW_API_TOKEN = your token to access the api SCW_API_ORGID = your organization ID to access the api
1 parent da3e970 commit 3d25fe3

File tree

2 files changed

+32
-8
lines changed

2 files changed

+32
-8
lines changed

pkg/config/config.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"os"
1414
"path/filepath"
1515
"runtime"
16+
"strings"
1617

1718
"github.com/scaleway/scaleway-cli/pkg/scwversion"
1819
)
@@ -55,6 +56,18 @@ func (c *Config) Save(configPath string) error {
5556
// GetConfig returns the Scaleway CLI config file for the current user
5657
func GetConfig(scwrcPath string) (*Config, error) {
5758
var err error
59+
60+
orgid := os.Getenv("SCW_API_ORGID")
61+
token := os.Getenv("SCW_API_TOKEN")
62+
if token != "" && orgid != "" {
63+
cfg := Config{
64+
Organization: strings.Trim(orgid, "\n"),
65+
Token: strings.Trim(token, "\n"),
66+
Version: scwversion.VERSION,
67+
}
68+
return &cfg, nil
69+
}
70+
5871
if scwrcPath == "" {
5972
scwrcPath, err = GetConfigFilePath()
6073
if err != nil {

pkg/config/config_test.go

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ package config
66

77
import (
88
"math/rand"
9-
"time"
109
"os"
1110
"strconv"
1211
"strings"
1312
"testing"
13+
"time"
1414

1515
"github.com/scaleway/scaleway-cli/pkg/scwversion"
1616
. "github.com/smartystreets/goconvey/convey"
@@ -40,21 +40,32 @@ func TestGetConfigFilePathEnv(t *testing.T) {
4040

4141
func TestGetConfig(t *testing.T) {
4242
Convey("Testing GetConfig() with and without env variable", t, func() {
43-
os.Setenv("SCW_CONFIG_PATH", "./config_testdata1")
43+
rand.Seed(time.Now().UTC().UnixNano())
44+
randOrg := strconv.FormatInt(rand.Int63(), 16)
45+
randToken := strconv.FormatInt(rand.Int63(), 16)
4446
cfg := &Config{
45-
Organization: strings.Trim("test_orgID", "\n"),
46-
Token: strings.Trim("test_token", "\n"),
47+
Organization: strings.Trim(randOrg, "\n"),
48+
Token: strings.Trim(randToken, "\n"),
4749
}
50+
os.Setenv("SCW_CONFIG_PATH", "./config_testdata1")
4851
err := cfg.Save("")
4952
So(err, ShouldBeNil)
5053
cfg, err = GetConfig("./config_testdata1")
51-
So(cfg.Organization, ShouldEqual, "test_orgID")
52-
So(cfg.Token, ShouldEqual, "test_token")
54+
So(cfg.Organization, ShouldEqual, randOrg)
55+
So(cfg.Token, ShouldEqual, randToken)
5356
os.Unsetenv("SCW_CONFIG_PATH")
5457
cfg, err = GetConfig("./config_testdata1")
5558
So(err, ShouldBeNil)
56-
So(cfg.Organization, ShouldEqual, "test_orgID")
57-
So(cfg.Token, ShouldEqual, "test_token")
59+
So(cfg.Organization, ShouldEqual, randOrg)
60+
So(cfg.Token, ShouldEqual, randToken)
61+
os.Setenv("SCW_API_ORGID", randOrg)
62+
os.Setenv("SCW_API_TOKEN", randToken)
63+
cfg, err = GetConfig("")
64+
So(err, ShouldBeNil)
65+
So(cfg.Organization, ShouldEqual, randOrg)
66+
So(cfg.Token, ShouldEqual, randToken)
67+
os.Unsetenv("SCW_API_ORGID")
68+
os.Unsetenv("SCW_API_TOKEN")
5869
})
5970
}
6071

0 commit comments

Comments
 (0)