@@ -29,27 +29,27 @@ See below the schema `scw init` follows to ask for default config:
2929+---+ no +----v----+ |no
3030|out+<----+Override?| v
3131+---+ +----+----+ +----+-----+
32- | |Read email +-----------+
33- +------>+ or token | token |
32+ | |Read +-----------+
33+ +------>+ token | token |
3434 yes +----------+ |
35- |email |
36- v v
37- +---+----+ +------+---+
38- | Read | |Get access|
39- |password| | key |
40- +---+----+ +------+---+
41- | |
42- v |
43- +--------+ yes +-+-+ |
44- |Read OTP+<----+2FA| |
45- +---+----+ +---+ |
46- | |no |
47- | v |
48- | +-----+------+ |
49- +----->+Create token| |
50- +-----+------+ |
51- | |
52- v |
35+ |
36+ v
37+ +------+---+
38+ |Get access|
39+ | key |
40+ +------+---+
41+ |
42+ |
43+ |
44+ |
45+ |
46+ |
47+ |
48+ |
49+ |
50+ |
51+ |
52+ |
5353 +-------+----------+ |
5454 |ask default config+<-----+
5555 +------------------+
@@ -60,6 +60,7 @@ func GetCommands() *core.Commands {
6060}
6161
6262type initArgs struct {
63+ AccessKey string
6364 SecretKey string
6465 Region scw.Region
6566 Zone scw.Zone
@@ -148,9 +149,17 @@ Default path for configuration file is based on the following priority order:
148149 // Manually prompt for missing args:
149150
150151 // Credentials
152+ if args .AccessKey == "" {
153+ _ , _ = interactive .Println ()
154+ args .AccessKey , err = promptAccessKey (ctx )
155+ if err != nil {
156+ return err
157+ }
158+ }
159+
151160 if args .SecretKey == "" {
152161 _ , _ = interactive .Println ()
153- args .SecretKey , err = promptCredentials (ctx )
162+ args .SecretKey , err = promptSecret (ctx )
154163 if err != nil {
155164 return err
156165 }
@@ -346,81 +355,64 @@ Default path for configuration file is based on the following priority order:
346355 }
347356}
348357
349- func promptCredentials (ctx context.Context ) (string , error ) {
350- UUIDOrEmail , err := interactive .Readline (& interactive.ReadlineConfig {
358+ func promptSecret (ctx context.Context ) (string , error ) {
359+ secret , err := interactive .Readline (& interactive.ReadlineConfig {
351360 Ctx : ctx ,
352361 PromptFunc : func (value string ) string {
353- secretKey , email := "secret-key" , "email "
362+ secretKey := "secret-key"
354363 switch {
355- case validation .IsEmail (value ):
356- email = terminal .Style (email , color .FgBlue )
357364 case validation .IsUUID (value ):
358365 secretKey = terminal .Style (secretKey , color .FgBlue )
359366 }
360- return terminal .Style (fmt .Sprintf ("Enter a valid %s or an %s : " , secretKey , email ), color .Bold )
367+ return terminal .Style (fmt .Sprintf ("Enter a valid %s: " , secretKey ), color .Bold )
361368 },
362369 ValidateFunc : func (s string ) error {
363- if validation .IsEmail ( s ) || validation . IsSecretKey (s ) {
370+ if validation .IsSecretKey (s ) {
364371 return nil
365372 }
366- return fmt .Errorf ("invalid email or secret-key" )
373+ return fmt .Errorf ("invalid secret-key" )
367374 },
368375 })
369376 if err != nil {
370377 return "" , err
371378 }
372379
373380 switch {
374- case validation .IsEmail ( UUIDOrEmail ):
375- passwordRetriesLeft := 3
376- for passwordRetriesLeft > 0 {
377- email := UUIDOrEmail
378- password , err := interactive . PromptPasswordWithConfig ( & interactive. PromptPasswordConfig {
379- Ctx : ctx ,
380- Prompt : "Enter your " + terminal . Style ( "password" , color . Bold ),
381- })
382- if err != nil {
383- return "" , err
384- }
385- hostname , _ := os . Hostname ()
386- loginReq := & account. LoginRequest {
387- Email : email ,
388- Password : password ,
389- Description : fmt . Sprintf ( "scw-cli %s@%s" , os . Getenv ( "USER" ), hostname ),
381+ case validation .IsUUID ( secret ):
382+ return secret , nil
383+
384+ default :
385+ return "" , fmt . Errorf ( "invalid secret-key: '%v'" , secret )
386+ }
387+ }
388+
389+ func promptAccessKey ( ctx context. Context ) ( string , error ) {
390+ accessKey , err := interactive . Readline ( & interactive. ReadlineConfig {
391+ Ctx : ctx ,
392+ PromptFunc : func ( value string ) string {
393+ accessKey := "access-key"
394+ switch {
395+ case validation . IsUUID ( value ):
396+ accessKey = terminal . Style ( accessKey , color . FgBlue )
390397 }
391- for {
392- loginResp , err := account .Login (ctx , loginReq )
393- if err != nil {
394- return "" , err
395- }
396- if loginResp .WrongPassword {
397- passwordRetriesLeft --
398- if loginReq .TwoFactorToken == "" {
399- interactive .Printf ("Wrong username or password.\n " )
400- } else {
401- interactive .Printf ("Wrong 2FA code.\n " )
402- }
403- break
404- }
405- if ! loginResp .TwoFactorRequired {
406- return loginResp .Token .SecretKey , nil
407- }
408- loginReq .TwoFactorToken , err = interactive .PromptStringWithConfig (& interactive.PromptStringConfig {
409- Ctx : ctx ,
410- Prompt : "Enter your 2FA code" ,
411- })
412- if err != nil {
413- return "" , err
414- }
398+ return terminal .Style (fmt .Sprintf ("Enter a valid %s: " , accessKey ), color .Bold )
399+ },
400+ ValidateFunc : func (s string ) error {
401+ if validation .IsAccessKey (s ) {
402+ return nil
415403 }
416- }
417- return "" , fmt .Errorf ("wrong password entered 3 times in a row, exiting" )
418-
419- case validation .IsUUID (UUIDOrEmail ):
420- return UUIDOrEmail , nil
404+ return fmt .Errorf ("invalid access-key" )
405+ },
406+ })
407+ if err != nil {
408+ return "" , err
409+ }
421410
411+ switch {
412+ case validation .IsAccessKey (accessKey ):
413+ return accessKey , nil
422414 default :
423- return "" , fmt .Errorf ("invalid email or secret -key: '%v'" , UUIDOrEmail )
415+ return "" , fmt .Errorf ("invalid access -key: '%v'" , accessKey )
424416 }
425417}
426418
0 commit comments