Skip to content

Commit 889c9c0

Browse files
committed
Refactored 'login' command (scaleway#80)
1 parent 7bc8e27 commit 889c9c0

File tree

1 file changed

+33
-13
lines changed

1 file changed

+33
-13
lines changed

pkg/commands/login.go

+33-13
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
)
2121

2222
var cmdLogin = &types.Command{
23-
Exec: runLogin,
23+
Exec: cmdExecLogin,
2424
UsageLine: "login [OPTIONS]",
2525
Description: "Log in to Scaleway API",
2626
Help: `Generates a configuration file in '/home/$USER/.scwrc'
@@ -32,6 +32,7 @@ You can get your credentials on https://cloud.scaleway.com/#/credentials
3232
}
3333

3434
func promptUser(prompt string, output *string, echo bool) {
35+
// FIXME: should use stdin/stdout from command context
3536
fmt.Fprintf(os.Stdout, prompt)
3637
os.Stdout.Sync()
3738

@@ -59,50 +60,69 @@ var organization string // -o flag
5960
var token string // -t flag
6061
var loginHelp bool // -h, --help flag
6162

62-
func runLogin(cmd *types.Command, args []string) {
63+
type LoginArgs struct {
64+
Organization string
65+
Token string
66+
}
67+
68+
func cmdExecLogin(cmd *types.Command, rawArgs []string) {
6369
if loginHelp {
6470
cmd.PrintUsage()
6571
}
66-
if len(args) != 0 {
72+
if len(rawArgs) != 0 {
6773
cmd.PrintShortUsage()
6874
}
6975

76+
args := LoginArgs{
77+
Organization: organization,
78+
Token: token,
79+
}
80+
ctx := cmd.GetContext(rawArgs)
81+
err := RunLogin(ctx, args)
82+
if err != nil {
83+
log.Fatalf("Cannot execute 'login': %v", err)
84+
}
85+
}
86+
87+
// RunLogin is the handler for 'scw login'
88+
func RunLogin(ctx types.CommandContext, args LoginArgs) error {
7089
if len(organization) == 0 {
7190
fmt.Println("You can get your credentials on https://cloud.scaleway.com/#/credentials")
72-
promptUser("Organization (access key): ", &organization, true)
91+
promptUser("Organization (access key): ", &args.Organization, true)
7392
}
74-
if len(token) == 0 {
75-
promptUser("Token: ", &token, false)
93+
if args.Token == "" {
94+
promptUser("Token: ", &args.Token, false)
7695
}
7796

7897
cfg := &api.Config{
7998
APIEndPoint: "https://account.scaleway.com/",
80-
Organization: strings.Trim(organization, "\n"),
81-
Token: strings.Trim(token, "\n"),
99+
Organization: strings.Trim(args.Organization, "\n"),
100+
Token: strings.Trim(args.Token, "\n"),
82101
}
83102

84103
api, err := api.NewScalewayAPI(cfg.APIEndPoint, cfg.Organization, cfg.Token)
85104
if err != nil {
86-
log.Fatalf("Unable to create ScalewayAPI: %s", err)
105+
return fmt.Errorf("Unable to create ScalewayAPI: %s", err)
87106
}
88107
err = api.CheckCredentials()
89108
if err != nil {
90-
log.Fatalf("Unable to contact ScalewayAPI: %s", err)
109+
return fmt.Errorf("Unable to contact ScalewayAPI: %s", err)
91110
}
92111

93112
scwrcPath, err := utils.GetConfigFilePath()
94113
if err != nil {
95-
log.Fatalf("Unable to get scwrc config file path: %s", err)
114+
return fmt.Errorf("Unable to get scwrc config file path: %s", err)
96115
}
97116
scwrc, err := os.OpenFile(scwrcPath, os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0600)
98117
if err != nil {
99-
log.Fatalf("Unable to create scwrc config file: %s", err)
118+
return fmt.Errorf("Unable to create scwrc config file: %s", err)
100119
}
101120
defer scwrc.Close()
102121
encoder := json.NewEncoder(scwrc)
103122
cfg.APIEndPoint = "https://api.scaleway.com/"
104123
err = encoder.Encode(cfg)
105124
if err != nil {
106-
log.Fatalf("Unable to encode scw config file: %s", err)
125+
return fmt.Errorf("Unable to encode scw config file: %s", err)
107126
}
127+
return nil
108128
}

0 commit comments

Comments
 (0)