Skip to content

Commit d17aefc

Browse files
committed
Merge branch 'v0.10.0' of https://github.com/spaceuptech/space-cloud into ee-v0.10.0
2 parents a38753d + 9820d61 commit d17aefc

File tree

152 files changed

+30239
-686
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

152 files changed

+30239
-686
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ css
2020
.sass-cache
2121

2222
space-cloud
23+
raft-store
2324
publish.sh
2425

2526
test_config.yaml

config/config.go

+32-19
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,34 @@ import "github.com/spaceuptech/space-cloud/utils"
44

55
// Config holds the entire configuration
66
type Config struct {
7-
Projects map[string]*Project `json:"projects" yaml:"projects"` // The key here is the project id
7+
Projects []*Project `json:"projects" yaml:"projects"` // The key here is the project id
8+
SSL *SSL `json:"ssl" yaml:"ssl"`
9+
Admin *Admin `json:"admin" yaml:"admin"`
10+
Cluster string `json:"cluster" yaml:"cluster"`
11+
NodeID string `json:"nodeId" yaml:"nodeId"`
812
}
913

1014
// Project holds the project level configuration
1115
type Project struct {
12-
ID string `json:"id" yaml:"id"`
1316
Secret string `json:"secret" yaml:"secret"`
17+
ID string `json:"id" yaml:"id"`
18+
Name string `json:"name" yaml:"name"`
1419
Modules *Modules `json:"modules" yaml:"modules"`
15-
SSL *SSL `json:"ssl" yaml:"ssl"`
20+
}
21+
22+
// Admin stores the admin credentials
23+
type Admin struct {
24+
User string `json:"user" yaml:"user"`
25+
Pass string `json:"pass" yaml:"pass"`
26+
Role string `json:"role" yaml:"role"`
27+
Secret string `json:"secret" yaml:"secret"`
1628
}
1729

1830
// SSL holds the certificate and key file locations
1931
type SSL struct {
20-
Crt string `json:"crt" yaml:"crt"`
21-
Key string `json:"key" yaml:"key"`
32+
Enabled bool `json:"enabled" yaml:"enabled"`
33+
Crt string `json:"crt" yaml:"crt"`
34+
Key string `json:"key" yaml:"key"`
2235
}
2336

2437
// Modules holds the config of all the modules of that environment
@@ -51,16 +64,16 @@ type TableRule struct {
5164
// Rule is the authorisation object at the query level
5265
type Rule struct {
5366
Rule string `json:"rule" yaml:"rule"`
54-
Eval string `json:"eval" yaml:"eval"`
55-
Type string `json:"type" yaml:"type"`
56-
F1 interface{} `json:"f1" yaml:"f1"`
57-
F2 interface{} `json:"f2" yaml:"f2"`
58-
Clauses []*Rule `json:"clauses" yaml:"clauses"`
59-
DB string `json:"db" yaml:"db"`
60-
Col string `json:"col" yaml:"col"`
61-
Find map[string]interface{} `json:"find" yaml:"find"`
62-
Service string `json:"service" yaml:"service"`
63-
Func string `json:"func" yaml:"func"`
67+
Eval string `json:"eval,omitempty" yaml:"eval,omitempty"`
68+
Type string `json:"type,omitempty" yaml:"type,omitempty"`
69+
F1 interface{} `json:"f1,omitempty" yaml:"f1,omitempty"`
70+
F2 interface{} `json:"f2,omitempty" yaml:"f2,omitempty"`
71+
Clauses []*Rule `json:"clauses,omitempty" yaml:"clauses,omitempty"`
72+
DB string `json:"db,omitempty" yaml:"db,omitempty"`
73+
Col string `json:"col,omitempty" yaml:"col,omitempty"`
74+
Find map[string]interface{} `json:"find,omitempty" yaml:"find,omitempty"`
75+
Service string `json:"service,omitempty" yaml:"service,omitempty"`
76+
Func string `json:"func,omitempty" yaml:"func,omitempty"`
6477
}
6578

6679
// Auth holds the mapping of the sign in method
@@ -93,10 +106,10 @@ type Realtime struct {
93106

94107
// FileStore holds the config for the file store module
95108
type FileStore struct {
96-
Enabled bool `json:"enabled" yaml:"enabled"`
97-
StoreType string `json:"storeType" yaml:"storeType"`
98-
Conn string `json:"conn" yaml:"conn"`
99-
Rules map[string]*FileRule `json:"rules" yaml:"rules"`
109+
Enabled bool `json:"enabled" yaml:"enabled"`
110+
StoreType string `json:"storeType" yaml:"storeType"`
111+
Conn string `json:"conn" yaml:"conn"`
112+
Rules []*FileRule `json:"rules" yaml:"rules"`
100113
}
101114

102115
// FileRule is the authorization object at the file rule level

config/generate.go

+59-11
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,28 @@ import (
1212
)
1313

1414
type input struct {
15-
Conn string
16-
PrimaryDB string
17-
ID string
15+
Conn string
16+
PrimaryDB string
17+
ID string
18+
AdminName string
19+
AdminPass string
20+
AdminRole string
21+
AdminSecret string
22+
HomeDir string
23+
BuildVersion string
24+
}
25+
26+
// GenerateEmptyConfig creates an empty config file
27+
func GenerateEmptyConfig() *Config {
28+
return &Config{
29+
SSL: &SSL{Enabled: false},
30+
Admin: &Admin{User: "admin", Pass: "123", Role: "captain-cloud", Secret: "some-secret"},
31+
Projects: []*Project{},
32+
}
1833
}
1934

2035
// GenerateConfig started the interactive cli to generate config file
21-
func GenerateConfig() error {
36+
func GenerateConfig(configFilePath string) error {
2237
fmt.Println()
2338
fmt.Println("This utility walks you through creating a config.yaml file for your space-cloud project.")
2439
fmt.Println("It only covers the most essential configurations and suggests sensible defaults.")
@@ -28,9 +43,9 @@ func GenerateConfig() error {
2843
i := new(input)
2944

3045
// Ask the project id
31-
dir, _ := os.Getwd()
32-
array := strings.Split(dir, string(os.PathSeparator))
33-
dir = array[len(array)-1]
46+
workingDir, _ := os.Getwd()
47+
array := strings.Split(workingDir, string(os.PathSeparator))
48+
dir := array[len(array)-1]
3449
err := survey.AskOne(&survey.Input{Message: "project name:", Default: formatProjectID(dir)}, &i.ID, survey.Required)
3550
if err != nil {
3651
return err
@@ -56,17 +71,50 @@ func GenerateConfig() error {
5671
return err
5772
}
5873

59-
return writeConfig(i)
74+
// Ask for the admin username
75+
err = survey.AskOne(&survey.Input{Message: "Mission Control (UserName)", Default: "admin"}, &i.AdminName, survey.Required)
76+
if err != nil {
77+
return err
78+
}
79+
80+
// Ask for the admin password
81+
err = survey.AskOne(&survey.Input{Message: "Mission Control (Password)", Default: "123"}, &i.AdminPass, survey.Required)
82+
if err != nil {
83+
return err
84+
}
85+
86+
// Ask for the admin role
87+
err = survey.AskOne(&survey.Input{Message: "Mission Control (Role)", Default: "captain-cloud"}, &i.AdminRole, survey.Required)
88+
if err != nil {
89+
return err
90+
}
91+
92+
// Ask for the admin secret
93+
err = survey.AskOne(&survey.Input{Message: "Mission Control (JWT Secret)", Default: "some-secret"}, &i.AdminSecret, survey.Required)
94+
if err != nil {
95+
return err
96+
}
97+
98+
i.HomeDir = utils.UserHomeDir()
99+
i.BuildVersion = utils.BuildVersion
100+
101+
if configFilePath == "none" {
102+
configFilePath = workingDir + string(os.PathSeparator) + i.ID + ".yaml"
103+
}
104+
105+
return writeConfig(i, configFilePath)
60106
}
61107

62-
func writeConfig(i *input) error {
63-
f, err := os.Create("./" + i.ID + ".yaml")
108+
func writeConfig(i *input, configFilePath string) error {
109+
f, err := os.Create(configFilePath)
64110
if err != nil {
65111
return err
66112
}
67113
defer f.Close()
68114

69-
tmpl, err := template.New("config").Parse(templateString)
115+
tmplString := templateString
116+
117+
tmpl, err := template.New("config").Parse(tmplString)
70118
if err != nil {
71119
return err
72120
}

config/http.go

-42
This file was deleted.

config/load.go

+19-18
Original file line numberDiff line numberDiff line change
@@ -9,38 +9,39 @@ import (
99
"gopkg.in/yaml.v2"
1010
)
1111

12-
func loadEnvironmentVariable(p *Project) {
13-
if strings.HasPrefix(p.Secret, "$") {
14-
tempString := strings.TrimPrefix(p.Secret, "$")
15-
tempEnvVar, present := os.LookupEnv(tempString)
16-
17-
if present {
18-
p.Secret = tempEnvVar
12+
func loadEnvironmentVariable(c *Config) {
13+
for _, p := range c.Projects {
14+
if strings.HasPrefix(p.Secret, "$") {
15+
tempString := strings.TrimPrefix(p.Secret, "$")
16+
tempEnvVar, present := os.LookupEnv(tempString)
17+
18+
if present {
19+
p.Secret = tempEnvVar
20+
}
1921
}
20-
}
21-
22-
for _, value := range p.Modules.Crud {
23-
if strings.HasPrefix(value.Conn, "$") {
24-
tempStringC := strings.TrimPrefix(value.Conn, "$")
25-
tempEnvVarC, presentC := os.LookupEnv(tempStringC)
26-
27-
if presentC {
28-
value.Conn = tempEnvVarC
22+
for _, value := range p.Modules.Crud {
23+
if strings.HasPrefix(value.Conn, "$") {
24+
tempStringC := strings.TrimPrefix(value.Conn, "$")
25+
tempEnvVarC, presentC := os.LookupEnv(tempStringC)
26+
27+
if presentC {
28+
value.Conn = tempEnvVarC
29+
}
2930
}
3031
}
3132
}
3233
}
3334

3435
// LoadConfigFromFile loads the config from the provided file path
35-
func LoadConfigFromFile(path string) (*Project, error) {
36+
func LoadConfigFromFile(path string) (*Config, error) {
3637
// Load the file in memory
3738
dat, err := ioutil.ReadFile(path)
3839
if err != nil {
3940
return nil, err
4041
}
4142

4243
// Marshal the configuration
43-
conf := new(Project)
44+
conf := new(Config)
4445
if strings.HasSuffix(path, "json") {
4546
err = json.Unmarshal(dat, conf)
4647
} else {

config/store.go

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package config
2+
3+
import (
4+
"encoding/json"
5+
"errors"
6+
"io/ioutil"
7+
"strings"
8+
9+
"gopkg.in/yaml.v2"
10+
)
11+
12+
// StoreConfigToFile stores the config file to disk
13+
func StoreConfigToFile(conf *Config, path string) error {
14+
var data []byte
15+
var err error
16+
17+
if strings.HasSuffix(path, ".yaml") {
18+
data, err = yaml.Marshal(conf)
19+
} else if strings.HasSuffix(path, ".json") {
20+
data, err = json.Marshal(conf)
21+
} else {
22+
return errors.New("Invalid config file type")
23+
}
24+
25+
// Check if error occured while marshaling
26+
if err != nil {
27+
return err
28+
}
29+
30+
return ioutil.WriteFile(path, data, 0644)
31+
}

0 commit comments

Comments
 (0)