Skip to content

Commit 26965aa

Browse files
authored
Merge pull request #167 from commitdev/add-create-command
Add create command
2 parents 10d851b + 1a1ce93 commit 26965aa

File tree

14 files changed

+163
-249
lines changed

14 files changed

+163
-249
lines changed

cmd/create.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
"path"
6+
"strings"
7+
8+
"github.com/commitdev/zero/internal/config/projectconfig"
9+
"github.com/commitdev/zero/internal/constants"
10+
"github.com/commitdev/zero/internal/generate"
11+
"github.com/commitdev/zero/pkg/util/exit"
12+
"github.com/spf13/cobra"
13+
)
14+
15+
var createConfigPath string
16+
17+
func init() {
18+
createCmd.PersistentFlags().StringVarP(&createConfigPath, "config", "c", constants.ZeroProjectYml, "config path")
19+
20+
rootCmd.AddCommand(createCmd)
21+
}
22+
23+
var createCmd = &cobra.Command{
24+
Use: "create",
25+
Short: fmt.Sprintf("Create projects for modules and configuration specified in %s", constants.ZeroProjectYml),
26+
Run: func(cmd *cobra.Command, args []string) {
27+
Create(projectconfig.RootDir, createConfigPath)
28+
},
29+
}
30+
31+
func Create(dir string, createConfigPath string) {
32+
if strings.Trim(createConfigPath, " ") == "" {
33+
exit.Fatal("config path cannot be empty!")
34+
}
35+
configFilePath := path.Join(dir, createConfigPath)
36+
projectConfig := projectconfig.LoadConfig(configFilePath)
37+
38+
generate.Generate(*projectConfig)
39+
}

internal/config/generator_config.go

Lines changed: 0 additions & 38 deletions
This file was deleted.

internal/config/moduleconfig/module_config.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package moduleconfig
33
import (
44
"io/ioutil"
55

6-
"github.com/k0kubun/pp"
76
yaml "gopkg.in/yaml.v2"
87
)
98

@@ -42,6 +41,5 @@ func LoadModuleConfig(filePath string) (ModuleConfig, error) {
4241
if err != nil {
4342
return config, err
4443
}
45-
pp.Println("Module Config:", config)
4644
return config, nil
4745
}

internal/config/projectconfig/project_config.go

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,15 @@ type Modules map[string]Module
3434

3535
type Module struct {
3636
Parameters Parameters `yaml:"parameters,omitempty"`
37-
Files Files `yaml:"files,omitempty"`
37+
Files Files
3838
}
3939

4040
type Parameters map[string]string
4141

4242
type Files struct {
4343
Directory string `yaml:"dir,omitempty"`
4444
Repository string `yaml:"repo,omitempty"`
45+
Source string
4546
}
4647

4748
func LoadConfig(filePath string) *ZeroProjectConfig {
@@ -62,35 +63,13 @@ func (c *ZeroProjectConfig) Print() {
6263
pp.Println(c)
6364
}
6465

65-
// @TODO only an example, needs refactoring
66-
func EKSGoReactSampleModules() Modules {
67-
parameters := Parameters{}
68-
return Modules{
69-
"aws-eks-stack": NewModule(parameters, "zero-aws-eks-stack", "github.com/commitdev/zero-aws-eks-stack"),
70-
"deployable-backend": NewModule(parameters, "zero-deployable-backend", "github.com/commitdev/zero-deployable-backend"),
71-
"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 {
66+
func NewModule(parameters Parameters, directory string, repository string, source string) Module {
8967
return Module{
9068
Parameters: parameters,
9169
Files: Files{
9270
Directory: directory,
9371
Repository: repository,
72+
Source: source,
9473
},
9574
}
9675
}

internal/config/projectconfig/project_config_test.go

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,12 @@ func TestLoadConfig(t *testing.T) {
2020
file.Write([]byte(validConfigContent()))
2121
filePath := file.Name()
2222

23-
modules := projectconfig.InfrastructureSampleModules()
24-
sampleModules := projectconfig.EKSGoReactSampleModules()
25-
26-
for k, v := range sampleModules {
27-
modules[k] = v
28-
}
29-
3023
want := &projectconfig.ZeroProjectConfig{
3124
Name: "abc",
32-
Modules: modules,
25+
Modules: eksGoReactSampleModules(),
3326
}
3427

35-
t.Run("Should load and unmarshall config correctly", func(t *testing.T) {
28+
t.Run("Should load and unmarshal config correctly", func(t *testing.T) {
3629
got := projectconfig.LoadConfig(filePath)
3730
if !cmp.Equal(want, got, cmpopts.EquateEmpty()) {
3831
t.Errorf("projectconfig.ZeroProjectConfig.Unmarshal mismatch (-want +got):\n%s", cmp.Diff(want, got))
@@ -41,33 +34,40 @@ func TestLoadConfig(t *testing.T) {
4134

4235
}
4336

37+
func eksGoReactSampleModules() projectconfig.Modules {
38+
parameters := projectconfig.Parameters{"a": "b"}
39+
return projectconfig.Modules{
40+
"aws-eks-stack": projectconfig.NewModule(parameters, "zero-aws-eks-stack", "github.com/something/repo1", "github.com/commitdev/zero-aws-eks-stack"),
41+
"deployable-backend": projectconfig.NewModule(parameters, "zero-deployable-backend", "github.com/something/repo2", "github.com/commitdev/zero-deployable-backend"),
42+
"deployable-react-frontend": projectconfig.NewModule(parameters, "zero-deployable-react-frontend", "github.com/something/repo3", "github.com/commitdev/zero-deployable-react-frontend"),
43+
}
44+
}
45+
4446
func validConfigContent() string {
4547
return `
4648
name: abc
4749
48-
context:
49-
5050
modules:
51-
infrastructure:
52-
parameters:
53-
repoName: infrastructure
54-
region: us-east-1
55-
accountId: 12345
56-
productionHost: something.com
57-
files:
58-
dir: infrastructure
59-
repo: https://github.com/myorg/infrastructure
6051
aws-eks-stack:
52+
parameters:
53+
a: b
6154
files:
6255
dir: zero-aws-eks-stack
63-
repo: github.com/commitdev/zero-aws-eks-stack
56+
repo: github.com/something/repo1
57+
source: github.com/commitdev/zero-aws-eks-stack
6458
deployable-backend:
59+
parameters:
60+
a: b
6561
files:
6662
dir: zero-deployable-backend
67-
repo: github.com/commitdev/zero-deployable-backend
63+
repo: github.com/something/repo2
64+
source: github.com/commitdev/zero-deployable-backend
6865
deployable-react-frontend:
66+
parameters:
67+
a: b
6968
files:
7069
dir: zero-deployable-react-frontend
71-
repo: github.com/commitdev/zero-deployable-react-frontend
70+
repo: github.com/something/repo3
71+
source: github.com/commitdev/zero-deployable-react-frontend
7272
`
7373
}

internal/context/init.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func Init(outDir string) *projectconfig.ZeroProjectConfig {
4141
}
4242

4343
moduleSources := chooseStack(getRegistry())
44-
moduleConfigs := loadAllModules(moduleSources)
44+
moduleConfigs, mappedSources := loadAllModules(moduleSources)
4545

4646
prompts := getProjectPrompts(projectConfig.Name, moduleConfigs)
4747

@@ -75,23 +75,20 @@ func Init(outDir string) *projectconfig.ZeroProjectConfig {
7575
}
7676

7777
}
78-
79-
projectConfig.Modules[moduleName] = projectconfig.NewModule(projectModuleParams, repoName, repoURL)
78+
pp.Println(mappedSources)
79+
projectConfig.Modules[moduleName] = projectconfig.NewModule(projectModuleParams, repoName, repoURL, mappedSources[moduleName])
8080
}
8181

82-
// TODO : Write the project config file. For now, print.
83-
pp.Println(projectConfig)
84-
pp.Print(projectCredentials)
85-
8682
// TODO: load ~/.zero/config.yml (or credentials)
8783
// TODO: prompt global credentials
8884

8985
return &projectConfig
9086
}
9187

9288
// loadAllModules takes a list of module sources, downloads those modules, and parses their config
93-
func loadAllModules(moduleSources []string) map[string]moduleconfig.ModuleConfig {
89+
func loadAllModules(moduleSources []string) (map[string]moduleconfig.ModuleConfig, map[string]string) {
9490
modules := make(map[string]moduleconfig.ModuleConfig)
91+
mappedSources := make(map[string]string)
9592

9693
wg := sync.WaitGroup{}
9794
wg.Add(len(moduleSources))
@@ -106,8 +103,9 @@ func loadAllModules(moduleSources []string) map[string]moduleconfig.ModuleConfig
106103
exit.Fatal("Unable to load module: %v\n", err)
107104
}
108105
modules[mod.Name] = mod
106+
mappedSources[mod.Name] = moduleSource
109107
}
110-
return modules
108+
return modules, mappedSources
111109
}
112110

113111
// promptAllModules takes a map of all the modules and prompts the user for values for all the parameters

internal/generate/generate_infrastructure.go

Lines changed: 0 additions & 80 deletions
This file was deleted.

0 commit comments

Comments
 (0)