Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add create command #167

Merged
merged 5 commits into from
Jun 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 39 additions & 0 deletions cmd/create.go
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it bad to just add source to the moduleConfig itself?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ModuleConfig represents the structure of the module file, so we don't want it in there.

}

// 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.