Skip to content
This repository was archived by the owner on Sep 2, 2024. It is now read-only.

Commit f03667f

Browse files
committed
added web admin UI, improve the login flow
1 parent 15d8e06 commit f03667f

File tree

6 files changed

+61
-27
lines changed

6 files changed

+61
-27
lines changed

cmd/config.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@ import (
1010
func getPublicKey() (pubKey string, ok bool) {
1111
pubKey = viper.GetString("pubKey")
1212
if len(pubKey) == 0 {
13-
fmt.Printf("%s\n", cldanger("cannot find pubKey in your .backend config file"))
14-
fmt.Println("\nMake sure to get your StaticBackend public key and save it in a .backend YAML config file.")
13+
fmt.Printf("%s\n", cldanger("cannot find pubKey in your .backend.yml config file"))
14+
fmt.Println("\nMake sure to get your StaticBackend public key and save it in a .backend.yml YAML config file.")
1515
fmt.Println("\nFor instance:")
1616
fmt.Printf("\n\t%s: na1", clsecondary("region"))
1717
fmt.Printf("\n\t%s: your-key-here", clsecondary("pubKey"))
1818
fmt.Println("\nYou received your public key when you created your account via email.")
19+
fmt.Printf("\n%s", clbold("use \"backend login --dev\" to work with the development server.\n\n"))
1920
return
2021
}
2122

@@ -50,7 +51,7 @@ func setBackend() bool {
5051

5152
region := viper.GetString("region")
5253
if len(region) == 0 {
53-
region = "na1"
54+
region = "dev"
5455
}
5556

5657
backend.Region = region

cmd/dbCreate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ $> backend db create tasks "{ name: \"task 1\", assign: \"dominic\", done: false
4343

4444
var doc map[string]interface{}
4545

46-
if err := json.Unmarshal([]byte(raw), &raw); err != nil {
46+
if err := json.Unmarshal([]byte(raw), &doc); err != nil {
4747
fmt.Printf("%s: %v\n", cldanger("An error occured"), err)
4848
return
4949
}

cmd/login.go

Lines changed: 48 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
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
2120
You 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\nregion: %s\nrootToken: %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\nYou'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
}

cmd/root.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212

1313
const (
1414
// Version is the current version of the CLI
15-
Version = "v1.4.0"
15+
Version = "v1.4.3"
1616
)
1717

1818
var (
@@ -47,6 +47,10 @@ This CLI gives you the following functionalities:
4747
- A local development server: %s
4848
- Managing your backend resources: %s
4949
- Managing your account: %s
50+
51+
Use "backend server" to start your local dev server.
52+
53+
Use "backend login --dev" to automatically configure for local dev.
5054
`,
5155
clbold(clsecondary("StaticBackend CLI "+Version)),
5256
clbold("backend server"),

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ require (
99
github.com/spf13/cobra v1.1.1
1010
github.com/spf13/viper v1.7.0
1111
github.com/staticbackendhq/backend-go v1.4.0
12-
github.com/staticbackendhq/core v1.4.0
12+
github.com/staticbackendhq/core v1.4.3
1313
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d
1414
)
1515

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -372,8 +372,8 @@ github.com/spf13/viper v1.7.0 h1:xVKxvI7ouOI5I+U9s2eeiUfMaWBVoXA3AWskkrqK0VM=
372372
github.com/spf13/viper v1.7.0/go.mod h1:8WkrPz2fc9jxqZNCJI/76HCieCp4Q8HaLFoCha5qpdg=
373373
github.com/staticbackendhq/backend-go v1.4.0 h1:JnyCrw745c9CfAOH0R2GQRRFOL0x0DGjM0NN7zEDsFc=
374374
github.com/staticbackendhq/backend-go v1.4.0/go.mod h1:cGTVKFtNU2CJUXJ2LcyEeA3MlkSVqNm1YthY8jffl4Q=
375-
github.com/staticbackendhq/core v1.4.0 h1:mrNvQaQh41ZkdD2fc+16N5diIQH7PwaD84C5l03Gzts=
376-
github.com/staticbackendhq/core v1.4.0/go.mod h1:mAfNAZoS7Bf1h99tjcYvh0ruZ5IZdF7YlUY9JmqJ3Tg=
375+
github.com/staticbackendhq/core v1.4.3 h1:ZrvTtgqsxC7StDTPdnxONDv5R1+UMXZE/mec/6UUeSk=
376+
github.com/staticbackendhq/core v1.4.3/go.mod h1:mAfNAZoS7Bf1h99tjcYvh0ruZ5IZdF7YlUY9JmqJ3Tg=
377377
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
378378
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
379379
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=

0 commit comments

Comments
 (0)