Skip to content

Commit

Permalink
Merge pull request #167 from commitdev/add-create-command
Browse files Browse the repository at this point in the history
Add create command
  • Loading branch information
bmonkman committed Jun 18, 2020
2 parents 10d851b + 1a1ce93 commit 26965aa
Show file tree
Hide file tree
Showing 14 changed files with 163 additions and 249 deletions.
39 changes: 39 additions & 0 deletions cmd/create.go
@@ -0,0 +1,39 @@
package cmd

import (
"fmt"
"path"
"strings"

"github.com/commitdev/zero/internal/config/projectconfig"
"github.com/commitdev/zero/internal/constants"
"github.com/commitdev/zero/internal/generate"
"github.com/commitdev/zero/pkg/util/exit"
"github.com/spf13/cobra"
)

var createConfigPath string

func init() {
createCmd.PersistentFlags().StringVarP(&createConfigPath, "config", "c", constants.ZeroProjectYml, "config path")

rootCmd.AddCommand(createCmd)
}

var createCmd = &cobra.Command{
Use: "create",
Short: fmt.Sprintf("Create projects for modules and configuration specified in %s", constants.ZeroProjectYml),
Run: func(cmd *cobra.Command, args []string) {
Create(projectconfig.RootDir, createConfigPath)
},
}

func Create(dir string, createConfigPath string) {
if strings.Trim(createConfigPath, " ") == "" {
exit.Fatal("config path cannot be empty!")
}
configFilePath := path.Join(dir, createConfigPath)
projectConfig := projectconfig.LoadConfig(configFilePath)

generate.Generate(*projectConfig)
}
38 changes: 0 additions & 38 deletions internal/config/generator_config.go

This file was deleted.

2 changes: 0 additions & 2 deletions internal/config/moduleconfig/module_config.go
Expand Up @@ -3,7 +3,6 @@ package moduleconfig
import (
"io/ioutil"

"github.com/k0kubun/pp"
yaml "gopkg.in/yaml.v2"
)

Expand Down Expand Up @@ -42,6 +41,5 @@ func LoadModuleConfig(filePath string) (ModuleConfig, error) {
if err != nil {
return config, err
}
pp.Println("Module Config:", config)
return config, nil
}
29 changes: 4 additions & 25 deletions internal/config/projectconfig/project_config.go
Expand Up @@ -34,14 +34,15 @@ type Modules map[string]Module

type Module struct {
Parameters Parameters `yaml:"parameters,omitempty"`
Files Files `yaml:"files,omitempty"`
Files Files
}

type Parameters map[string]string

type Files struct {
Directory string `yaml:"dir,omitempty"`
Repository string `yaml:"repo,omitempty"`
Source string
}

func LoadConfig(filePath string) *ZeroProjectConfig {
Expand All @@ -62,35 +63,13 @@ func (c *ZeroProjectConfig) Print() {
pp.Println(c)
}

// @TODO only an example, needs refactoring
func EKSGoReactSampleModules() Modules {
parameters := Parameters{}
return Modules{
"aws-eks-stack": NewModule(parameters, "zero-aws-eks-stack", "github.com/commitdev/zero-aws-eks-stack"),
"deployable-backend": NewModule(parameters, "zero-deployable-backend", "github.com/commitdev/zero-deployable-backend"),
"deployable-react-frontend": NewModule(parameters, "zero-deployable-react-frontend", "github.com/commitdev/zero-deployable-react-frontend"),
}
}

// @TODO only an example, needs refactoring
func InfrastructureSampleModules() Modules {
parameters := Parameters{
"repoName": "infrastructure",
"region": "us-east-1",
"accountId": "12345",
"productionHost": "something.com",
}
return Modules{
"infrastructure": NewModule(parameters, "infrastructure", "https://github.com/myorg/infrastructure"),
}
}

func NewModule(parameters Parameters, directory string, repository string) Module {
func NewModule(parameters Parameters, directory string, repository string, source string) Module {
return Module{
Parameters: parameters,
Files: Files{
Directory: directory,
Repository: repository,
Source: source,
},
}
}
46 changes: 23 additions & 23 deletions internal/config/projectconfig/project_config_test.go
Expand Up @@ -20,19 +20,12 @@ func TestLoadConfig(t *testing.T) {
file.Write([]byte(validConfigContent()))
filePath := file.Name()

modules := projectconfig.InfrastructureSampleModules()
sampleModules := projectconfig.EKSGoReactSampleModules()

for k, v := range sampleModules {
modules[k] = v
}

want := &projectconfig.ZeroProjectConfig{
Name: "abc",
Modules: modules,
Modules: eksGoReactSampleModules(),
}

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

}

func eksGoReactSampleModules() projectconfig.Modules {
parameters := projectconfig.Parameters{"a": "b"}
return projectconfig.Modules{
"aws-eks-stack": projectconfig.NewModule(parameters, "zero-aws-eks-stack", "github.com/something/repo1", "github.com/commitdev/zero-aws-eks-stack"),
"deployable-backend": projectconfig.NewModule(parameters, "zero-deployable-backend", "github.com/something/repo2", "github.com/commitdev/zero-deployable-backend"),
"deployable-react-frontend": projectconfig.NewModule(parameters, "zero-deployable-react-frontend", "github.com/something/repo3", "github.com/commitdev/zero-deployable-react-frontend"),
}
}

func validConfigContent() string {
return `
name: abc
context:
modules:
infrastructure:
parameters:
repoName: infrastructure
region: us-east-1
accountId: 12345
productionHost: something.com
files:
dir: infrastructure
repo: https://github.com/myorg/infrastructure
aws-eks-stack:
parameters:
a: b
files:
dir: zero-aws-eks-stack
repo: github.com/commitdev/zero-aws-eks-stack
repo: github.com/something/repo1
source: github.com/commitdev/zero-aws-eks-stack
deployable-backend:
parameters:
a: b
files:
dir: zero-deployable-backend
repo: github.com/commitdev/zero-deployable-backend
repo: github.com/something/repo2
source: github.com/commitdev/zero-deployable-backend
deployable-react-frontend:
parameters:
a: b
files:
dir: zero-deployable-react-frontend
repo: github.com/commitdev/zero-deployable-react-frontend
repo: github.com/something/repo3
source: github.com/commitdev/zero-deployable-react-frontend
`
}
16 changes: 7 additions & 9 deletions internal/context/init.go
Expand Up @@ -41,7 +41,7 @@ func Init(outDir string) *projectconfig.ZeroProjectConfig {
}

moduleSources := chooseStack(getRegistry())
moduleConfigs := loadAllModules(moduleSources)
moduleConfigs, mappedSources := loadAllModules(moduleSources)

prompts := getProjectPrompts(projectConfig.Name, moduleConfigs)

Expand Down Expand Up @@ -75,23 +75,20 @@ func Init(outDir string) *projectconfig.ZeroProjectConfig {
}

}

projectConfig.Modules[moduleName] = projectconfig.NewModule(projectModuleParams, repoName, repoURL)
pp.Println(mappedSources)
projectConfig.Modules[moduleName] = projectconfig.NewModule(projectModuleParams, repoName, repoURL, mappedSources[moduleName])
}

// TODO : Write the project config file. For now, print.
pp.Println(projectConfig)
pp.Print(projectCredentials)

// TODO: load ~/.zero/config.yml (or credentials)
// TODO: prompt global credentials

return &projectConfig
}

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

wg := sync.WaitGroup{}
wg.Add(len(moduleSources))
Expand All @@ -106,8 +103,9 @@ func loadAllModules(moduleSources []string) map[string]moduleconfig.ModuleConfig
exit.Fatal("Unable to load module: %v\n", err)
}
modules[mod.Name] = mod
mappedSources[mod.Name] = moduleSource
}
return modules
return modules, mappedSources
}

// promptAllModules takes a map of all the modules and prompts the user for values for all the parameters
Expand Down
80 changes: 0 additions & 80 deletions internal/generate/generate_infrastructure.go

This file was deleted.

0 comments on commit 26965aa

Please sign in to comment.