@@ -20,7 +20,7 @@ import (
20
20
)
21
21
22
22
var cmdLogin = & types.Command {
23
- Exec : runLogin ,
23
+ Exec : cmdExecLogin ,
24
24
UsageLine : "login [OPTIONS]" ,
25
25
Description : "Log in to Scaleway API" ,
26
26
Help : `Generates a configuration file in '/home/$USER/.scwrc'
@@ -32,6 +32,7 @@ You can get your credentials on https://cloud.scaleway.com/#/credentials
32
32
}
33
33
34
34
func promptUser (prompt string , output * string , echo bool ) {
35
+ // FIXME: should use stdin/stdout from command context
35
36
fmt .Fprintf (os .Stdout , prompt )
36
37
os .Stdout .Sync ()
37
38
@@ -59,50 +60,69 @@ var organization string // -o flag
59
60
var token string // -t flag
60
61
var loginHelp bool // -h, --help flag
61
62
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 ) {
63
69
if loginHelp {
64
70
cmd .PrintUsage ()
65
71
}
66
- if len (args ) != 0 {
72
+ if len (rawArgs ) != 0 {
67
73
cmd .PrintShortUsage ()
68
74
}
69
75
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 {
70
89
if len (organization ) == 0 {
71
90
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 )
73
92
}
74
- if len ( token ) == 0 {
75
- promptUser ("Token: " , & token , false )
93
+ if args . Token == "" {
94
+ promptUser ("Token: " , & args . Token , false )
76
95
}
77
96
78
97
cfg := & api.Config {
79
98
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 " ),
82
101
}
83
102
84
103
api , err := api .NewScalewayAPI (cfg .APIEndPoint , cfg .Organization , cfg .Token )
85
104
if err != nil {
86
- log . Fatalf ("Unable to create ScalewayAPI: %s" , err )
105
+ return fmt . Errorf ("Unable to create ScalewayAPI: %s" , err )
87
106
}
88
107
err = api .CheckCredentials ()
89
108
if err != nil {
90
- log . Fatalf ("Unable to contact ScalewayAPI: %s" , err )
109
+ return fmt . Errorf ("Unable to contact ScalewayAPI: %s" , err )
91
110
}
92
111
93
112
scwrcPath , err := utils .GetConfigFilePath ()
94
113
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 )
96
115
}
97
116
scwrc , err := os .OpenFile (scwrcPath , os .O_CREATE | os .O_TRUNC | os .O_RDWR , 0600 )
98
117
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 )
100
119
}
101
120
defer scwrc .Close ()
102
121
encoder := json .NewEncoder (scwrc )
103
122
cfg .APIEndPoint = "https://api.scaleway.com/"
104
123
err = encoder .Encode (cfg )
105
124
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 )
107
126
}
127
+ return nil
108
128
}
0 commit comments