88
99 "github.com/spf13/cobra"
1010 "github.com/staticbackendhq/backend-go"
11- "golang.org/x/crypto/ssh/terminal"
1211)
1312
1413// loginCmd represents the login command
@@ -20,37 +19,67 @@ var loginCmd = &cobra.Command{
2019
2120You have to authenticate to manipulate your StaticBackend data.
2221
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.
2423 ` , clbold (clsecondary ("Login to your account" ))),
2524 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 )
2728 return
2829 }
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 )
2964
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
3665 }
3766
38- email = strings .Replace (email , "\n " , "" , - 1 )
67+ backend .PublicKey = pk
68+ backend .Region = region
3969
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 )
4473 return
4574 }
4675
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 )
5079 return
5180 }
5281
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." ) )
5483 },
5584}
5685
@@ -65,5 +94,5 @@ func init() {
6594
6695 // Cobra supports local flags which will only run when this command
6796 // 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 " )
6998}
0 commit comments