Skip to content

Commit 4f102f2

Browse files
committed
Moved config to dedicated package
1 parent 77979a1 commit 4f102f2

File tree

11 files changed

+151
-137
lines changed

11 files changed

+151
-137
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ FPM_ARGS ?= \
2424

2525
NAME = scw
2626
SRC = cmd/scw
27-
PACKAGES = pkg/api pkg/commands pkg/utils pkg/cli pkg/sshcommand
27+
PACKAGES = pkg/api pkg/commands pkg/utils pkg/cli pkg/sshcommand pkg/config
2828
REV = $(shell git rev-parse HEAD || echo "nogit")
2929
TAG = $(shell git describe --tags --always || echo "nogit")
3030
BUILDER = scaleway-cli-builder

pkg/api/config.go

Lines changed: 0 additions & 47 deletions
This file was deleted.

pkg/cli/main.go

Lines changed: 3 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
package cli
66

77
import (
8-
"encoding/json"
98
"fmt"
109
"io/ioutil"
1110
"os"
@@ -15,6 +14,7 @@ import (
1514

1615
"github.com/scaleway/scaleway-cli/pkg/api"
1716
"github.com/scaleway/scaleway-cli/pkg/commands"
17+
"github.com/scaleway/scaleway-cli/pkg/config"
1818
"github.com/scaleway/scaleway-cli/pkg/scwversion"
1919
"github.com/scaleway/scaleway-cli/pkg/utils"
2020
)
@@ -40,7 +40,7 @@ func Start(rawArgs []string, streams *commands.Streams) (int, error) {
4040

4141
flag.CommandLine.Parse(rawArgs)
4242

43-
config, cfgErr := getConfig()
43+
config, cfgErr := config.GetConfig()
4444
if cfgErr != nil && !os.IsNotExist(cfgErr) {
4545
return 1, fmt.Errorf("unable to open .scwrc config file: %v", cfgErr)
4646
}
@@ -130,46 +130,10 @@ func Start(rawArgs []string, streams *commands.Streams) (int, error) {
130130
return 1, fmt.Errorf("scw: unknown subcommand %s\nRun 'scw help' for usage.", name)
131131
}
132132

133-
// getConfig returns the Scaleway CLI config file for the current user
134-
func getConfig() (*api.Config, error) {
135-
scwrcPath, err := utils.GetConfigFilePath()
136-
if err != nil {
137-
return nil, err
138-
}
139-
140-
stat, err := os.Stat(scwrcPath)
141-
// we don't care if it fails, the user just won't see the warning
142-
if err == nil {
143-
mode := stat.Mode()
144-
if mode&0066 != 0 {
145-
return nil, fmt.Errorf("permissions %#o for .scwrc are too open.", mode)
146-
}
147-
}
148-
149-
file, err := ioutil.ReadFile(scwrcPath)
150-
if err != nil {
151-
return nil, err
152-
}
153-
var config api.Config
154-
err = json.Unmarshal(file, &config)
155-
if err != nil {
156-
return nil, err
157-
}
158-
// check if he has an old scwrc version
159-
if config.AccountAPI == "" {
160-
config.AccountAPI = "https://account.scaleway.com"
161-
config.Save()
162-
}
163-
if os.Getenv("scaleway_api_endpoint") == "" {
164-
os.Setenv("scaleway_api_endpoint", config.ComputeAPI)
165-
}
166-
return &config, nil
167-
}
168-
169133
// getScalewayAPI returns a ScalewayAPI using the user config file
170134
func getScalewayAPI() (*api.ScalewayAPI, error) {
171135
// We already get config globally, but whis way we can get explicit error when trying to create a ScalewayAPI object
172-
config, err := getConfig()
136+
config, err := config.GetConfig()
173137
if err != nil {
174138
return nil, err
175139
}

pkg/commands/info.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111

1212
"github.com/scaleway/scaleway-cli/vendor/github.com/kardianos/osext"
1313

14-
"github.com/scaleway/scaleway-cli/pkg/utils"
14+
"github.com/scaleway/scaleway-cli/pkg/config"
1515
)
1616

1717
// InfoArgs are flags for the `RunInfo` function
@@ -26,7 +26,7 @@ func RunInfo(ctx CommandContext, args InfoArgs) error {
2626
fmt.Fprintf(ctx.Stdout, "Organization: %s\n", ctx.API.Organization)
2727
// FIXME: add partially-masked token
2828
fmt.Fprintf(ctx.Stdout, "API Endpoint: %s\n", ctx.Getenv("scaleway_api_endpoint"))
29-
configPath, _ := utils.GetConfigFilePath()
29+
configPath, _ := config.GetConfigFilePath()
3030
fmt.Fprintf(ctx.Stdout, "RC file: %s\n", configPath)
3131
fmt.Fprintf(ctx.Stdout, "User: %s\n", ctx.Getenv("USER"))
3232
fmt.Fprintf(ctx.Stdout, "CPUs: %d\n", runtime.NumCPU())

pkg/commands/login.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
"github.com/scaleway/scaleway-cli/vendor/golang.org/x/crypto/ssh/terminal"
1818

1919
"github.com/scaleway/scaleway-cli/pkg/api"
20-
"github.com/scaleway/scaleway-cli/pkg/utils"
20+
"github.com/scaleway/scaleway-cli/pkg/config"
2121
)
2222

2323
// LoginArgs are arguments passed to `RunLogin`
@@ -30,7 +30,7 @@ type LoginArgs struct {
3030
// selectKey allows to choice a key in ~/.ssh
3131
func selectKey(args *LoginArgs) error {
3232
fmt.Println("Do you want to upload a SSH key ?")
33-
home, err := utils.GetHomeDir()
33+
home, err := config.GetHomeDir()
3434
if err != nil {
3535
return err
3636
}
@@ -88,7 +88,7 @@ func RunLogin(ctx CommandContext, args LoginArgs) error {
8888
promptUser("Token: ", &args.Token, false)
8989
}
9090

91-
cfg := &api.Config{
91+
cfg := &config.Config{
9292
ComputeAPI: "https://api.scaleway.com/",
9393
AccountAPI: "https://account.scaleway.com/",
9494
Organization: strings.Trim(args.Organization, "\n"),

pkg/commands/logout.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"fmt"
99
"os"
1010

11-
"github.com/scaleway/scaleway-cli/pkg/utils"
11+
"github.com/scaleway/scaleway-cli/pkg/config"
1212
)
1313

1414
// LogoutArgs are flags for the `RunLogout` function
@@ -17,7 +17,7 @@ type LogoutArgs struct{}
1717
// RunLogout is the handler for 'scw logout'
1818
func RunLogout(ctx CommandContext, args LogoutArgs) error {
1919
// FIXME: ask if we need to remove the local ssh key on the account
20-
scwrcPath, err := utils.GetConfigFilePath()
20+
scwrcPath, err := config.GetConfigFilePath()
2121
if err != nil {
2222
return fmt.Errorf("unable to get scwrc config file path: %v", err)
2323
}

pkg/commands/run.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"strings"
1313

1414
"github.com/scaleway/scaleway-cli/pkg/api"
15+
"github.com/scaleway/scaleway-cli/pkg/config"
1516
"github.com/scaleway/scaleway-cli/pkg/utils"
1617
"github.com/scaleway/scaleway-cli/vendor/github.com/Sirupsen/logrus"
1718
)
@@ -35,7 +36,7 @@ type RunArgs struct {
3536

3637
// AddSSHKeyToTags adds the ssh key in the tags
3738
func AddSSHKeyToTags(ctx CommandContext, tags *[]string, image string) error {
38-
home, err := utils.GetHomeDir()
39+
home, err := config.GetHomeDir()
3940
if err != nil {
4041
return fmt.Errorf("unable to find your home %v", err)
4142
}

pkg/config/config.go

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
// Copyright (C) 2015 Scaleway. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE.md file.
4+
5+
// ~/.scwrc management
6+
package config
7+
8+
import (
9+
"encoding/json"
10+
"errors"
11+
"fmt"
12+
"io/ioutil"
13+
"os"
14+
"path/filepath"
15+
)
16+
17+
// Config is a Scaleway CLI configuration file
18+
type Config struct {
19+
// ComputeAPI is the endpoint to the Scaleway API
20+
ComputeAPI string `json:"api_endpoint"`
21+
22+
// AccountAPI is the endpoint to the Scaleway Account API
23+
AccountAPI string `json:"account_endpoint"`
24+
25+
// Organization is the identifier of the Scaleway orgnization
26+
Organization string `json:"organization"`
27+
28+
// Token is the authentication token for the Scaleway organization
29+
Token string `json:"token"`
30+
}
31+
32+
// 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)
37+
}
38+
scwrc, err := os.OpenFile(scwrcPath, os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0600)
39+
if err != nil {
40+
return fmt.Errorf("Unable to create scwrc config file: %s", err)
41+
}
42+
defer scwrc.Close()
43+
encoder := json.NewEncoder(scwrc)
44+
err = encoder.Encode(c)
45+
if err != nil {
46+
return fmt.Errorf("Unable to encode scw config file: %s", err)
47+
}
48+
return nil
49+
}
50+
51+
// 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+
}
57+
58+
stat, err := os.Stat(scwrcPath)
59+
// we don't care if it fails, the user just won't see the warning
60+
if err == nil {
61+
mode := stat.Mode()
62+
if mode&0066 != 0 {
63+
return nil, fmt.Errorf("permissions %#o for .scwrc are too open.", mode)
64+
}
65+
}
66+
67+
file, err := ioutil.ReadFile(scwrcPath)
68+
if err != nil {
69+
return nil, err
70+
}
71+
var config Config
72+
err = json.Unmarshal(file, &config)
73+
if err != nil {
74+
return nil, err
75+
}
76+
// check if he has an old scwrc version
77+
if config.AccountAPI == "" {
78+
config.AccountAPI = "https://account.scaleway.com"
79+
config.Save()
80+
}
81+
if os.Getenv("scaleway_api_endpoint") == "" {
82+
os.Setenv("scaleway_api_endpoint", config.ComputeAPI)
83+
}
84+
return &config, nil
85+
}
86+
87+
// GetConfigFilePath returns the path to the Scaleway CLI config file
88+
func GetConfigFilePath() (string, error) {
89+
path, err := GetHomeDir()
90+
if err != nil {
91+
return "", err
92+
}
93+
return filepath.Join(path, ".scwrc"), nil
94+
}
95+
96+
// GetHomeDir returns the path to your home
97+
func GetHomeDir() (string, error) {
98+
homeDir := os.Getenv("HOME") // *nix
99+
if homeDir == "" { // Windows
100+
homeDir = os.Getenv("USERPROFILE")
101+
}
102+
if homeDir == "" {
103+
return "", errors.New("user home directory not found")
104+
}
105+
return homeDir, nil
106+
}

pkg/config/config_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright (C) 2015 Scaleway. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE.md file.
4+
5+
package config
6+
7+
import (
8+
"strings"
9+
"testing"
10+
11+
. "github.com/smartystreets/goconvey/convey"
12+
)
13+
14+
func TestGetConfigFilePath(t *testing.T) {
15+
Convey("Testing GetConfigFilePath()", t, func() {
16+
configPath, err := GetConfigFilePath()
17+
So(err, ShouldBeNil)
18+
So(configPath, ShouldNotEqual, "")
19+
20+
homedir, err := GetHomeDir()
21+
So(err, ShouldBeNil)
22+
So(strings.Contains(configPath, homedir), ShouldBeTrue)
23+
})
24+
}
25+
26+
func TestGetHomeDir(t *testing.T) {
27+
Convey("Testing GetHomeDir()", t, func() {
28+
homedir, err := GetHomeDir()
29+
So(err, ShouldBeNil)
30+
So(homedir, ShouldNotEqual, "")
31+
})
32+
}

pkg/utils/utils.go

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -178,27 +178,6 @@ func RemoveDuplicates(elements []string) []string {
178178
return result
179179
}
180180

181-
// GetHomeDir returns the path to your home
182-
func GetHomeDir() (string, error) {
183-
homeDir := os.Getenv("HOME") // *nix
184-
if homeDir == "" { // Windows
185-
homeDir = os.Getenv("USERPROFILE")
186-
}
187-
if homeDir == "" {
188-
return "", errors.New("user home directory not found")
189-
}
190-
return homeDir, nil
191-
}
192-
193-
// GetConfigFilePath returns the path to the Scaleway CLI config file
194-
func GetConfigFilePath() (string, error) {
195-
path, err := GetHomeDir()
196-
if err != nil {
197-
return "", err
198-
}
199-
return filepath.Join(path, ".scwrc"), nil
200-
}
201-
202181
const termjsBin string = "termjs-cli"
203182

204183
// AttachToSerial tries to connect to server serial using 'term.js-cli' and fallback with a help message

0 commit comments

Comments
 (0)