Skip to content

Commit b7859ca

Browse files
committed
added account management, started db and form mgnt
1 parent 18c5e29 commit b7859ca

20 files changed

+1280
-67
lines changed

.demo-backend.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
region: na1
2-
pubKey: "your key here"
2+
pubKey: "your key here"
3+
rootToken: "your root token here"

Makefile

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ test:
77
pkg: build
88
@rm -rf dist/*
99
@echo "building linux binaries"
10-
@GOARCH=amd64 GOOS=linux go build -o dist/binary-for-linux-64-bit
11-
@GOARCH=386 GOOS=linux go build -o dist/binary-for-linux-32-bit
10+
@CGO_ENABLED=0 GOARCH=amd64 GOOS=linux go build -o dist/binary-for-linux-64-bit
11+
@CGO_ENABLED=0 GOARCH=386 GOOS=linux go build -o dist/binary-for-linux-32-bit
1212
@echo "building mac binaries"
13-
@GOARCH=amd64 GOOS=darwin go build -o dist/binary-for-mac-64-bit
14-
@GOARCH=386 GOOS=darwin go build -o dist/binary-for-mac-32-bit
13+
@CGO_ENABLED=0 GOARCH=amd64 GOOS=darwin go build -o dist/binary-for-mac-64-bit
14+
@CGO_ENABLED=0 GOARCH=386 GOOS=darwin go build -o dist/binary-for-mac-32-bit
1515
@echo "building windows binaries"
16-
@GOARCH=amd64 GOOS=windows go build -o dist/binary-for-windows-64-bit.exe
17-
@GOARCH=386 GOOS=windows go build -o dist/binary-for-windows-32-bit.exe
16+
@CGO_ENABLED=0 GOARCH=amd64 GOOS=windows go build -o dist/binary-for-windows-64-bit.exe
17+
@CGO_ENABLED=0 GOARCH=386 GOOS=windows go build -o dist/binary-for-windows-32-bit.exe
1818
@echo "compressing binaries"
1919
@gzip dist/*

cmd/accountCreate.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ You may pick a paid plan later when you're ready.
4141
Run: func(cmd *cobra.Command, args []string) {
4242
if len(args) == 0 {
4343
fmt.Printf("%s %s %s\n", cldanger("Argument missing"), clerror("email"), cldanger("please supply an email."))
44+
return
4445
}
4546

4647
email := args[0]

cmd/accountPortal.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
Copyright © 2020 NAME HERE <EMAIL ADDRESS>
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
package cmd
17+
18+
import (
19+
"fmt"
20+
21+
"github.com/spf13/cobra"
22+
"github.com/staticbackendhq/backend-go"
23+
)
24+
25+
// accountCreateCmd represents the accountCreate command
26+
var accountPortalCmd = &cobra.Command{
27+
Use: "portal",
28+
Short: "Retrieve a URL to manage your subscription and credit card.",
29+
Long: fmt.Sprintf(`
30+
%s
31+
32+
Let you manage your billing account, change plan, update credit card and cancel.
33+
`,
34+
clbold(clsecondary("Access your billing portal")),
35+
),
36+
Run: func(cmd *cobra.Command, args []string) {
37+
if setBackend() == false {
38+
return
39+
}
40+
41+
tok, ok := getRookToken()
42+
if !ok {
43+
return
44+
}
45+
46+
var link string
47+
if err := backend.Get(tok, "/account/portal", &link); err != nil {
48+
fmt.Printf("%s: %v\n", cldanger("An error occured"), err)
49+
return
50+
}
51+
52+
fmt.Printf("%s\n", clsecondary("You may access your billing portal via this URL:"))
53+
fmt.Println(link)
54+
},
55+
}
56+
57+
func init() {
58+
accountCmd.AddCommand(accountPortalCmd)
59+
60+
// Here you will define your flags and configuration settings.
61+
62+
// Cobra supports Persistent Flags which will work for this command
63+
// and all subcommands, e.g.:
64+
// accountCreateCmd.PersistentFlags().String("foo", "", "A help for foo")
65+
66+
// Cobra supports local flags which will only run when this command
67+
// is called directly, e.g.:
68+
// accountCreateCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
69+
}

cmd/config.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
Copyright © 2020 Focus Centric inc. <dominicstpierre@gmail.com>
3+
4+
This program is free software: you can redistribute it and/or modify
5+
it under the terms of the GNU General Public License as published by
6+
the Free Software Foundation, either version 3 of the License, or
7+
(at your option) any later version.
8+
9+
This program is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
GNU General Public License for more details.
13+
14+
You should have received a copy of the GNU General Public License
15+
along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
package cmd
18+
19+
import (
20+
"fmt"
21+
22+
"github.com/spf13/viper"
23+
"github.com/staticbackendhq/backend-go"
24+
)
25+
26+
func getPublicKey() (pubKey string, ok bool) {
27+
pubKey = viper.GetString("pubKey")
28+
if len(pubKey) == 0 {
29+
fmt.Printf("%s\n", cldanger("cannot find pubKey in your .backend config file"))
30+
fmt.Println("\nMake sure to get your StaticBackend public key and save it in a .backend YAML config file.")
31+
fmt.Println("\nFor instance:")
32+
fmt.Printf("\n\t%s: na1", clsecondary("region"))
33+
fmt.Printf("\n\t%s: your-key-here", clsecondary("pubKey"))
34+
fmt.Println("\nYou received your public key when you created your account via email.")
35+
return
36+
}
37+
38+
ok = true
39+
return
40+
}
41+
42+
func getRookToken() (tok string, ok bool) {
43+
tok = viper.GetString("rootToken")
44+
if len(tok) == 0 {
45+
fmt.Printf("%s\n", cldanger("cannot find rootToken in your .backend config file"))
46+
fmt.Println("\nMake sure to get your root token and save it in a .backend YAML config file.")
47+
fmt.Println("\nFor instance:")
48+
fmt.Printf("\n\t%s: na1", clsecondary("region"))
49+
fmt.Printf("\n\t%s: your-key-here", clsecondary("pubKey"))
50+
fmt.Printf("\n\t%s: your-root-token-here", clsecondary("rootToken"))
51+
fmt.Println("\nYou received your root token when you created your account via email.")
52+
return
53+
}
54+
55+
ok = true
56+
return
57+
}
58+
59+
func setBackend() bool {
60+
pk, ok := getPublicKey()
61+
if !ok {
62+
return false
63+
}
64+
65+
backend.PublicKey = pk
66+
67+
region := viper.GetString("region")
68+
if len(region) == 0 {
69+
region = "na1"
70+
}
71+
72+
backend.Region = region
73+
74+
return true
75+
}

cmd/db.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
Copyright © 2020 Focus Centric inc. <dominicstpierre@gmail.com>
3+
4+
This program is free software: you can redistribute it and/or modify
5+
it under the terms of the GNU General Public License as published by
6+
the Free Software Foundation, either version 3 of the License, or
7+
(at your option) any later version.
8+
9+
This program is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
GNU General Public License for more details.
13+
14+
You should have received a copy of the GNU General Public License
15+
along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
package cmd
18+
19+
import (
20+
"fmt"
21+
22+
"github.com/spf13/cobra"
23+
)
24+
25+
// dbCmd represents the db command
26+
var dbCmd = &cobra.Command{
27+
Use: "db",
28+
Short: "Manage your database.",
29+
Long: fmt.Sprintf(`
30+
%s
31+
32+
You may perform CRUD operations if you have an admin level login.
33+
`,
34+
clbold(clsecondary("Manage your database")),
35+
),
36+
Run: func(cmd *cobra.Command, args []string) {
37+
38+
},
39+
}
40+
41+
func init() {
42+
rootCmd.AddCommand(dbCmd)
43+
44+
// Here you will define your flags and configuration settings.
45+
46+
// Cobra supports Persistent Flags which will work for this command
47+
// and all subcommands, e.g.:
48+
// accountCmd.PersistentFlags().String("foo", "", "A help for foo")
49+
50+
// Cobra supports local flags which will only run when this command
51+
// is called directly, e.g.:
52+
// accountCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
53+
}

cmd/dbCreate.go

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
Copyright © 2020 Focus Centric inc. <dominicstpierre@gmail.com>
3+
4+
This program is free software: you can redistribute it and/or modify
5+
it under the terms of the GNU General Public License as published by
6+
the Free Software Foundation, either version 3 of the License, or
7+
(at your option) any later version.
8+
9+
This program is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
GNU General Public License for more details.
13+
14+
You should have received a copy of the GNU General Public License
15+
along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
package cmd
18+
19+
import (
20+
"encoding/json"
21+
"fmt"
22+
23+
"github.com/spf13/cobra"
24+
"github.com/staticbackendhq/backend-go"
25+
)
26+
27+
// dbListCmd list document in a repository
28+
var dbCreateCmd = &cobra.Command{
29+
Use: `create repo-name json-content`,
30+
Short: "Create a document.",
31+
Long: fmt.Sprintf(`
32+
%s
33+
34+
To create a document you name the repository and pass a JSON object in a string.
35+
36+
$> backend db create tasks "{ name: \"task 1\", assign: \"dominic\", done: false }"
37+
`,
38+
clbold(clsecondary("Create document")),
39+
),
40+
Run: func(cmd *cobra.Command, args []string) {
41+
if setBackend() == false {
42+
return
43+
}
44+
45+
tok, ok := getRookToken()
46+
if !ok {
47+
return
48+
}
49+
50+
if len(args) == 0 {
51+
fmt.Printf("%s %s %s\n", cldanger("Argument missing"), clerror("repository"), cldanger("please supply a table name."))
52+
return
53+
} else if len(args) == 1 {
54+
fmt.Printf("%s %s %s\n", cldanger("Argument missing"), clerror("json object"), cldanger("please supply a document json object."))
55+
return
56+
}
57+
58+
repo, raw := args[0], args[1]
59+
60+
var doc map[string]interface{}
61+
62+
if err := json.Unmarshal([]byte(raw), &raw); err != nil {
63+
fmt.Printf("%s: %v\n", cldanger("An error occured"), err)
64+
return
65+
}
66+
67+
var result map[string]interface{}
68+
if err := backend.SudoCreate(tok, repo, doc, &result); err != nil {
69+
fmt.Printf("%s: %v\n", cldanger("An error occured"), err)
70+
return
71+
}
72+
73+
o := "{\n"
74+
for k, v := range result {
75+
o += fmt.Sprintf("\t%s: %v, \n", clsecondary(k), v)
76+
}
77+
78+
o += "}"
79+
80+
fmt.Println(o)
81+
},
82+
}
83+
84+
func init() {
85+
dbCmd.AddCommand(dbCreateCmd)
86+
87+
// Here you will define your flags and configuration settings.
88+
89+
// Cobra supports Persistent Flags which will work for this command
90+
// and all subcommands, e.g.:
91+
// accountCmd.PersistentFlags().String("foo", "", "A help for foo")
92+
}

0 commit comments

Comments
 (0)