8
8
9
9
"github.com/spf13/cobra"
10
10
"github.com/staticbackendhq/backend-go"
11
- "golang.org/x/crypto/ssh/terminal"
12
11
)
13
12
14
13
// loginCmd represents the login command
@@ -20,37 +19,67 @@ var loginCmd = &cobra.Command{
20
19
21
20
You have to authenticate to manipulate your StaticBackend data.
22
21
23
- We're saving your email/password in the .backend file, make sure to add it to your .gitignore file.
22
+ We're saving your root token in the .backend.yml file, make sure to add it to your .gitignore file.
24
23
` , clbold (clsecondary ("Login to your account" ))),
25
24
Run : func (cmd * cobra.Command , args []string ) {
26
- if ok := setBackend (); ! ok {
25
+ dev , err := cmd .Flags ().GetBool ("dev" )
26
+ if err != nil {
27
+ fmt .Println (err )
27
28
return
28
29
}
30
+ pk := "dev-memory-pk"
31
+ region := "dev"
32
+ rtoken := "safe-to-use-in-dev-root-token"
33
+
34
+ if ! dev {
35
+ var err error
36
+
37
+ reader := bufio .NewReader (os .Stdin )
38
+ fmt .Printf ("%s\n " , clsecondary ("enter your Public Key: " ))
39
+ pk , err = reader .ReadString ('\n' )
40
+ if err != nil {
41
+ fmt .Println ("error: " , err )
42
+ return
43
+ }
44
+
45
+ pk = strings .Replace (pk , "\n " , "" , - 1 )
46
+
47
+ fmt .Printf ("%s\n " , clsecondary ("enter host URL: " ))
48
+ region , err = reader .ReadString ('\n' )
49
+ if err != nil {
50
+ fmt .Println ("error: " , err )
51
+ return
52
+ }
53
+
54
+ region = strings .Replace (region , "\n " , "" , - 1 )
55
+
56
+ fmt .Printf ("%s\n " , clsecondary ("enter your Root Token: " ))
57
+ rtoken , err = reader .ReadString ('\n' )
58
+ if err != nil {
59
+ fmt .Println ("error: " , err )
60
+ return
61
+ }
62
+
63
+ rtoken = strings .Replace (rtoken , "\n " , "" , - 1 )
29
64
30
- reader := bufio .NewReader (os .Stdin )
31
- fmt .Printf ("%s\n " , clsecondary ("enter your email: " ))
32
- email , err := reader .ReadString ('\n' )
33
- if err != nil {
34
- fmt .Println ("error: " , err )
35
- return
36
65
}
37
66
38
- email = strings .Replace (email , "\n " , "" , - 1 )
67
+ backend .PublicKey = pk
68
+ backend .Region = region
39
69
40
- fmt .Printf ("%s\n " , clsecondary ("enter your password: " ))
41
- pw , err := terminal .ReadPassword (0 )
42
- if err != nil {
43
- fmt .Println ("error: " , err )
70
+ // we use the SudoListRepositories as a root token validator
71
+ if _ , err := backend .SudoListRepositories (rtoken ); err != nil {
72
+ fmt .Println ("invalid root token: " , err )
44
73
return
45
74
}
46
75
47
- tok , err := backend . Login ( email , string ( pw ) )
48
- if err != nil {
49
- fmt .Printf ( "%s: %v \n " , cldanger ( "an error occured" ) , err )
76
+ s := fmt . Sprintf ( "pubKey: %s \n region: %s \n rootToken: %s" , pk , region , rtoken )
77
+ if err := os . WriteFile ( ".backend.yml" , [] byte ( s ), 0660 ); err != nil {
78
+ fmt .Println ( "unable to save your credentials: " , err )
50
79
return
51
80
}
52
81
53
- fmt .Println ( "token " , tok )
82
+ fmt .Printf ( "%s \n \n You're ready to use the CLI. " , clsecondary ( "Your .backend.yml file has been setup." ) )
54
83
},
55
84
}
56
85
@@ -65,5 +94,5 @@ func init() {
65
94
66
95
// Cobra supports local flags which will only run when this command
67
96
// is called directly, e.g.:
68
- // loginCmd.Flags().BoolP("toggle ", "t", false, "Help message for toggle ")
97
+ loginCmd .Flags ().Bool ( "dev " , false , "Setup for local development credentials " )
69
98
}
0 commit comments