Skip to content

Commit

Permalink
changes ZeroProjectConfig.Modules structure and yaml file
Browse files Browse the repository at this point in the history
  • Loading branch information
thdaraujo committed Jun 10, 2020
1 parent 3926600 commit 9998eb4
Show file tree
Hide file tree
Showing 8 changed files with 176 additions and 21 deletions.
9 changes: 9 additions & 0 deletions README.md
Expand Up @@ -13,6 +13,15 @@ TBD
$ git clone git@github.com:commitdev/zero.git
$ cd zero && make
```
#### Running the tool locally

To install the CLI into your GOPATH and test it, run:
```
$ make install-go
$ zero --help
```



## Planning and Process

Expand Down
2 changes: 1 addition & 1 deletion cmd/apply.go
Expand Up @@ -26,7 +26,7 @@ var applyCmd = &cobra.Command{

// @TODO where applyConfigPath comes from?
projectContext := context.Apply(applyEnvironments, applyConfigPath)
// @TODO rootdir or applyConfigPath?
// @TODO rootdir?
projectconfig.Apply(projectconfig.RootDir, projectContext, applyEnvironments)
},
}
2 changes: 1 addition & 1 deletion internal/config/projectconfig/apply.go
Expand Up @@ -33,7 +33,7 @@ func makeAll(dir string, projectContext *ZeroProjectConfig, applyEnvironments []
for _, module := range projectContext.Modules {
// TODO what's the root dir for these modules?
// what's the real path to these modules? It's probably not the module name...
modulePath, err := filepath.Abs(path.Join(dir, projectContext.Name, module))
modulePath, err := filepath.Abs(path.Join(dir, projectContext.Name, module.Files.Directory))
if err != nil {
return err
}
Expand Down
25 changes: 19 additions & 6 deletions internal/config/projectconfig/init.go
Expand Up @@ -17,13 +17,26 @@ context:
# module can be in any format the go-getter supports (path, github, url, etc.)
# supports https://github.com/hashicorp/go-getter#url-format
# Example:
# - source: "../development/modules/ci"
# - output: "github-actions"
# - repo: "../development/modules/ci"
# - dir: "github-actions"
modules:
- source: "github.com/commitdev/zero-aws-eks-stack"
- source: "github.com/commitdev/zero-deployable-backend"
- source: "github.com/commitdev/zero-deployable-react-frontend"
aws-eks-stack:
parameters:
repoName: infrastructure
region: us-east-1
accountId: 12345
productionHost: something.com
files:
dir: infrastructure
repo: https://github.com/myorg/infrastructure
some-other-module:
parameters:
repoName: api
files:
dir: api
repo: https://github.com/myorg/api
`

var RootDir = "./"
Expand Down
51 changes: 49 additions & 2 deletions internal/config/projectconfig/project_config.go
Expand Up @@ -9,11 +9,11 @@ import (
)

type ZeroProjectConfig struct {
Name string
Name string `yaml:"name"`
ShouldPushRepositories bool
Infrastructure Infrastructure // TODO simplify and flatten / rename?
Parameters map[string]string
Modules []string
Modules Modules `yaml:"modules"`
}

type Infrastructure struct {
Expand All @@ -30,6 +30,20 @@ type terraform struct {
RemoteState bool
}

type Modules map[string]Module

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

type Parameters map[string]string

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

func LoadConfig(filePath string) *ZeroProjectConfig {
config := &ZeroProjectConfig{}
data, err := ioutil.ReadFile(filePath)
Expand All @@ -47,3 +61,36 @@ func LoadConfig(filePath string) *ZeroProjectConfig {
func (c *ZeroProjectConfig) Print() {
pp.Println(c)
}

// @TODO only an example, needs refactoring
func EKSGoReactSampleModules() Modules {
parameters := Parameters{}
return Modules{
"zero-aws-eks-stack": NewModule(parameters, "zero-aws-eks-stack", "github.com/commitdev/zero-aws-eks-stack"),
"zero-deployable-backend": NewModule(parameters, "zero-deployable-backend", "github.com/commitdev/zero-deployable-backend"),
"zero-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 {
return Module{
Parameters: parameters,
Files: Files{
Directory: directory,
Repository: repository,
},
}
}
88 changes: 88 additions & 0 deletions internal/config/projectconfig/project_config_test.go
@@ -0,0 +1,88 @@
package projectconfig_test

import (
"io/ioutil"
"log"
"os"
"testing"

"github.com/commitdev/zero/internal/config/projectconfig"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
)

func TestLoadConfig(t *testing.T) {
file, err := ioutil.TempFile(os.TempDir(), "config.yml")
if err != nil {
log.Fatal(err)
}
defer os.Remove(file.Name())
file.Write([]byte(validConfigContent()))

type args struct {
filePath string
}

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

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

expected := &projectconfig.ZeroProjectConfig{
Name: "abc",
Modules: modules,
}

tests := []struct {
name string
args args
want *projectconfig.ZeroProjectConfig
}{
{
"Working config",
args{filePath: file.Name()},
expected,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// @TODO handle nil/empty map unmarshall case?
if got := projectconfig.LoadConfig(tt.args.filePath); !cmp.Equal(got, tt.want, cmpopts.EquateEmpty()) {
t.Errorf(cmp.Diff(got, tt.want))
}
})
}
}

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
zero-aws-eks-stack:
files:
dir: zero-aws-eks-stack
repo: github.com/commitdev/zero-aws-eks-stack
zero-deployable-backend:
files:
dir: zero-deployable-backend
repo: github.com/commitdev/zero-deployable-backend
zero-deployable-react-frontend:
files:
dir: zero-deployable-react-frontend
repo: github.com/commitdev/zero-deployable-react-frontend
`
}
4 changes: 3 additions & 1 deletion internal/context/apply.go
Expand Up @@ -3,6 +3,7 @@ package context
import (
"fmt"
"log"
"path"

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

projectConfig := projectconfig.LoadConfig(applyConfigPath)
configPath := path.Join(projectconfig.RootDir, applyConfigPath)
projectConfig := projectconfig.LoadConfig(configPath)
return projectConfig
}

Expand Down
16 changes: 6 additions & 10 deletions internal/context/init.go
Expand Up @@ -21,7 +21,7 @@ import (
"github.com/manifoldco/promptui"
)

type Registry map[string][]string
type Registry map[string]projectconfig.Modules

// Create cloud provider context
func Init(outDir string) *projectconfig.ZeroProjectConfig {
Expand Down Expand Up @@ -85,7 +85,7 @@ func Init(outDir string) *projectconfig.ZeroProjectConfig {
}

// 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 projectconfig.Modules) map[string]moduleconfig.ModuleConfig {
modules := make(map[string]moduleconfig.ModuleConfig)

wg := sync.WaitGroup{}
Expand Down Expand Up @@ -200,12 +200,8 @@ func chooseCloudProvider(projectConfig *projectconfig.ZeroProjectConfig) {
func getRegistry() Registry {
return Registry{
// TODO: better place to store these options as configuration file or any source
"EKS + Go + React": []string{
"github.com/commitdev/zero-aws-eks-stack",
"github.com/commitdev/zero-deployable-backend",
"github.com/commitdev/zero-deployable-react-frontend",
},
"Custom": []string{},
"EKS + Go + React": projectconfig.EKSGoReactSampleModules(),
"Custom": projectconfig.Modules{},
}
}

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

func chooseStack(registry Registry) []string {
func chooseStack(registry Registry) projectconfig.Modules {
providerPrompt := promptui.Select{
Label: "Pick a stack you'd like to use",
Items: registry.availableLabels(),
Expand Down Expand Up @@ -267,6 +263,6 @@ func defaultProjConfig() projectconfig.ZeroProjectConfig {
AWS: nil,
},
Parameters: map[string]string{},
Modules: []string{},
Modules: projectconfig.Modules{},
}
}

0 comments on commit 9998eb4

Please sign in to comment.