Skip to content

Commit 9998eb4

Browse files
committed
changes ZeroProjectConfig.Modules structure and yaml file
1 parent 3926600 commit 9998eb4

File tree

8 files changed

+176
-21
lines changed

8 files changed

+176
-21
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@ TBD
1313
$ git clone git@github.com:commitdev/zero.git
1414
$ cd zero && make
1515
```
16+
#### Running the tool locally
17+
18+
To install the CLI into your GOPATH and test it, run:
19+
```
20+
$ make install-go
21+
$ zero --help
22+
```
23+
24+
1625

1726
## Planning and Process
1827

cmd/apply.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ var applyCmd = &cobra.Command{
2626

2727
// @TODO where applyConfigPath comes from?
2828
projectContext := context.Apply(applyEnvironments, applyConfigPath)
29-
// @TODO rootdir or applyConfigPath?
29+
// @TODO rootdir?
3030
projectconfig.Apply(projectconfig.RootDir, projectContext, applyEnvironments)
3131
},
3232
}

internal/config/projectconfig/apply.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func makeAll(dir string, projectContext *ZeroProjectConfig, applyEnvironments []
3333
for _, module := range projectContext.Modules {
3434
// TODO what's the root dir for these modules?
3535
// what's the real path to these modules? It's probably not the module name...
36-
modulePath, err := filepath.Abs(path.Join(dir, projectContext.Name, module))
36+
modulePath, err := filepath.Abs(path.Join(dir, projectContext.Name, module.Files.Directory))
3737
if err != nil {
3838
return err
3939
}

internal/config/projectconfig/init.go

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,26 @@ context:
1717
# module can be in any format the go-getter supports (path, github, url, etc.)
1818
# supports https://github.com/hashicorp/go-getter#url-format
1919
# Example:
20-
# - source: "../development/modules/ci"
21-
# - output: "github-actions"
22-
20+
# - repo: "../development/modules/ci"
21+
# - dir: "github-actions"
2322
modules:
24-
- source: "github.com/commitdev/zero-aws-eks-stack"
25-
- source: "github.com/commitdev/zero-deployable-backend"
26-
- source: "github.com/commitdev/zero-deployable-react-frontend"
23+
aws-eks-stack:
24+
parameters:
25+
repoName: infrastructure
26+
region: us-east-1
27+
accountId: 12345
28+
productionHost: something.com
29+
files:
30+
dir: infrastructure
31+
repo: https://github.com/myorg/infrastructure
32+
some-other-module:
33+
parameters:
34+
repoName: api
35+
files:
36+
dir: api
37+
repo: https://github.com/myorg/api
38+
39+
2740
`
2841

2942
var RootDir = "./"

internal/config/projectconfig/project_config.go

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ import (
99
)
1010

1111
type ZeroProjectConfig struct {
12-
Name string
12+
Name string `yaml:"name"`
1313
ShouldPushRepositories bool
1414
Infrastructure Infrastructure // TODO simplify and flatten / rename?
1515
Parameters map[string]string
16-
Modules []string
16+
Modules Modules `yaml:"modules"`
1717
}
1818

1919
type Infrastructure struct {
@@ -30,6 +30,20 @@ type terraform struct {
3030
RemoteState bool
3131
}
3232

33+
type Modules map[string]Module
34+
35+
type Module struct {
36+
Parameters Parameters `yaml:"parameters"`
37+
Files Files `yaml:"files"`
38+
}
39+
40+
type Parameters map[string]string
41+
42+
type Files struct {
43+
Directory string `yaml:"dir,omitempty"`
44+
Repository string `yaml:"repo,omitempty"`
45+
}
46+
3347
func LoadConfig(filePath string) *ZeroProjectConfig {
3448
config := &ZeroProjectConfig{}
3549
data, err := ioutil.ReadFile(filePath)
@@ -47,3 +61,36 @@ func LoadConfig(filePath string) *ZeroProjectConfig {
4761
func (c *ZeroProjectConfig) Print() {
4862
pp.Println(c)
4963
}
64+
65+
// @TODO only an example, needs refactoring
66+
func EKSGoReactSampleModules() Modules {
67+
parameters := Parameters{}
68+
return Modules{
69+
"zero-aws-eks-stack": NewModule(parameters, "zero-aws-eks-stack", "github.com/commitdev/zero-aws-eks-stack"),
70+
"zero-deployable-backend": NewModule(parameters, "zero-deployable-backend", "github.com/commitdev/zero-deployable-backend"),
71+
"zero-deployable-react-frontend": NewModule(parameters, "zero-deployable-react-frontend", "github.com/commitdev/zero-deployable-react-frontend"),
72+
}
73+
}
74+
75+
// @TODO only an example, needs refactoring
76+
func InfrastructureSampleModules() Modules {
77+
parameters := Parameters{
78+
"repoName": "infrastructure",
79+
"region": "us-east-1",
80+
"accountId": "12345",
81+
"productionHost": "something.com",
82+
}
83+
return Modules{
84+
"infrastructure": NewModule(parameters, "infrastructure", "https://github.com/myorg/infrastructure"),
85+
}
86+
}
87+
88+
func NewModule(parameters Parameters, directory string, repository string) Module {
89+
return Module{
90+
Parameters: parameters,
91+
Files: Files{
92+
Directory: directory,
93+
Repository: repository,
94+
},
95+
}
96+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package projectconfig_test
2+
3+
import (
4+
"io/ioutil"
5+
"log"
6+
"os"
7+
"testing"
8+
9+
"github.com/commitdev/zero/internal/config/projectconfig"
10+
"github.com/google/go-cmp/cmp"
11+
"github.com/google/go-cmp/cmp/cmpopts"
12+
)
13+
14+
func TestLoadConfig(t *testing.T) {
15+
file, err := ioutil.TempFile(os.TempDir(), "config.yml")
16+
if err != nil {
17+
log.Fatal(err)
18+
}
19+
defer os.Remove(file.Name())
20+
file.Write([]byte(validConfigContent()))
21+
22+
type args struct {
23+
filePath string
24+
}
25+
26+
modules := projectconfig.EKSGoReactSampleModules()
27+
infrastructureModules := projectconfig.InfrastructureSampleModules()
28+
29+
for k, v := range infrastructureModules {
30+
modules[k] = v
31+
}
32+
33+
expected := &projectconfig.ZeroProjectConfig{
34+
Name: "abc",
35+
Modules: modules,
36+
}
37+
38+
tests := []struct {
39+
name string
40+
args args
41+
want *projectconfig.ZeroProjectConfig
42+
}{
43+
{
44+
"Working config",
45+
args{filePath: file.Name()},
46+
expected,
47+
},
48+
}
49+
for _, tt := range tests {
50+
t.Run(tt.name, func(t *testing.T) {
51+
// @TODO handle nil/empty map unmarshall case?
52+
if got := projectconfig.LoadConfig(tt.args.filePath); !cmp.Equal(got, tt.want, cmpopts.EquateEmpty()) {
53+
t.Errorf(cmp.Diff(got, tt.want))
54+
}
55+
})
56+
}
57+
}
58+
59+
func validConfigContent() string {
60+
return `
61+
name: abc
62+
63+
context:
64+
65+
modules:
66+
infrastructure:
67+
parameters:
68+
repoName: infrastructure
69+
region: us-east-1
70+
accountId: 12345
71+
productionHost: something.com
72+
files:
73+
dir: infrastructure
74+
repo: https://github.com/myorg/infrastructure
75+
zero-aws-eks-stack:
76+
files:
77+
dir: zero-aws-eks-stack
78+
repo: github.com/commitdev/zero-aws-eks-stack
79+
zero-deployable-backend:
80+
files:
81+
dir: zero-deployable-backend
82+
repo: github.com/commitdev/zero-deployable-backend
83+
zero-deployable-react-frontend:
84+
files:
85+
dir: zero-deployable-react-frontend
86+
repo: github.com/commitdev/zero-deployable-react-frontend
87+
`
88+
}

internal/context/apply.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package context
33
import (
44
"fmt"
55
"log"
6+
"path"
67

78
"github.com/commitdev/zero/internal/config/projectconfig"
89
"github.com/commitdev/zero/pkg/util/exit"
@@ -24,7 +25,8 @@ Only a single environment may be suitable for an initial test, but for a real sy
2425
exit.Fatal("config path cannot be empty!")
2526
}
2627

27-
projectConfig := projectconfig.LoadConfig(applyConfigPath)
28+
configPath := path.Join(projectconfig.RootDir, applyConfigPath)
29+
projectConfig := projectconfig.LoadConfig(configPath)
2830
return projectConfig
2931
}
3032

internal/context/init.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
"github.com/manifoldco/promptui"
2222
)
2323

24-
type Registry map[string][]string
24+
type Registry map[string]projectconfig.Modules
2525

2626
// Create cloud provider context
2727
func Init(outDir string) *projectconfig.ZeroProjectConfig {
@@ -85,7 +85,7 @@ func Init(outDir string) *projectconfig.ZeroProjectConfig {
8585
}
8686

8787
// loadAllModules takes a list of module sources, downloads those modules, and parses their config
88-
func loadAllModules(moduleSources []string) map[string]moduleconfig.ModuleConfig {
88+
func loadAllModules(moduleSources projectconfig.Modules) map[string]moduleconfig.ModuleConfig {
8989
modules := make(map[string]moduleconfig.ModuleConfig)
9090

9191
wg := sync.WaitGroup{}
@@ -200,12 +200,8 @@ func chooseCloudProvider(projectConfig *projectconfig.ZeroProjectConfig) {
200200
func getRegistry() Registry {
201201
return Registry{
202202
// TODO: better place to store these options as configuration file or any source
203-
"EKS + Go + React": []string{
204-
"github.com/commitdev/zero-aws-eks-stack",
205-
"github.com/commitdev/zero-deployable-backend",
206-
"github.com/commitdev/zero-deployable-react-frontend",
207-
},
208-
"Custom": []string{},
203+
"EKS + Go + React": projectconfig.EKSGoReactSampleModules(),
204+
"Custom": projectconfig.Modules{},
209205
}
210206
}
211207

@@ -219,7 +215,7 @@ func (registry Registry) availableLabels() []string {
219215
return labels
220216
}
221217

222-
func chooseStack(registry Registry) []string {
218+
func chooseStack(registry Registry) projectconfig.Modules {
223219
providerPrompt := promptui.Select{
224220
Label: "Pick a stack you'd like to use",
225221
Items: registry.availableLabels(),
@@ -267,6 +263,6 @@ func defaultProjConfig() projectconfig.ZeroProjectConfig {
267263
AWS: nil,
268264
},
269265
Parameters: map[string]string{},
270-
Modules: []string{},
266+
Modules: projectconfig.Modules{},
271267
}
272268
}

0 commit comments

Comments
 (0)