Skip to content

Commit

Permalink
Removed extra prints, fixed test, hooked up generator code
Browse files Browse the repository at this point in the history
  • Loading branch information
bmonkman committed Jun 17, 2020
1 parent 9df662d commit 2321b10
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 151 deletions.
2 changes: 0 additions & 2 deletions cmd/create.go
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/commitdev/zero/internal/constants"
"github.com/commitdev/zero/internal/generate"
"github.com/commitdev/zero/pkg/util/exit"
"github.com/k0kubun/pp"
"github.com/spf13/cobra"
)

Expand All @@ -35,6 +34,5 @@ func Create(dir string, createConfigPath string) {
configFilePath := path.Join(dir, createConfigPath)
projectConfig := projectconfig.LoadConfig(configFilePath)

pp.Println(projectConfig)
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
}
66 changes: 22 additions & 44 deletions internal/config/projectconfig/project_config_test.go
Expand Up @@ -11,28 +11,6 @@ import (
"github.com/google/go-cmp/cmp/cmpopts"
)

// @TODO only an example, needs refactoring
func eksGoReactSampleModules() Modules {
parameters := Parameters{}
return 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"),
}
}

// @TODO only an example, needs refactoring
func sampleModules() Modules {
parameters := Parameters{
"repoName": "infrastructure",
"region": "us-east-1",
"accountId": "12345",
"productionHost": "something.com",
}
return Modules{
"infrastructure": projectconfig.NewModule(parameters, "infrastructure", "https://github.com/myorg/infrastructure", "github.com/commitdev/zero-aws-eks-stack"),
}
}
func TestLoadConfig(t *testing.T) {
file, err := ioutil.TempFile(os.TempDir(), "config.yml")
if err != nil {
Expand All @@ -42,16 +20,9 @@ func TestLoadConfig(t *testing.T) {
file.Write([]byte(validConfigContent()))
filePath := file.Name()

modules := sampleModules()
sampleModules := 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) {
Expand All @@ -63,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
`
}
4 changes: 0 additions & 4 deletions internal/context/init.go
Expand Up @@ -79,10 +79,6 @@ func Init(outDir string) *projectconfig.ZeroProjectConfig {
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

Expand Down
84 changes: 34 additions & 50 deletions internal/generate/generate_modules.go
@@ -1,6 +1,7 @@
package generate

import (
"fmt"
"os"
"path"
"path/filepath"
Expand All @@ -11,67 +12,50 @@ import (

"github.com/commitdev/zero/internal/config/projectconfig"
"github.com/commitdev/zero/internal/constants"
"github.com/commitdev/zero/internal/module"
"github.com/commitdev/zero/internal/util"
"github.com/commitdev/zero/pkg/util/flog"
"github.com/k0kubun/pp"

"github.com/commitdev/zero/pkg/util/fs"
)

// func GenerateModules(cfg *config.GeneratorConfig) {
// var templateModules []*module.TemplateModule

// // TODO: Refactor this since the module struct is changing

// // Initiate all the modules defined in the config
// // for _, moduleConfig := range cfg.Modules {
// //mod, err := module.NewTemplateModule(moduleConfig)

// // if err != nil {
// // exit.Error("module failed to load: %s", err)
// // }
// // templateModules = append(templateModules, mod)
// // }

// // Prompt for module params and execute each of the generator modules
// for _, mod := range templateModules {
// // TODO: read zero-project.yml instead

// err := Generate(mod, cfg)
// if err != nil {
// exit.Error("module %s: %s", mod.Source, err)
// }
// }
// }

// type TemplateParams struct {
// Name string
// Context map[string]string
// Params map[string]string
// }

// Generate accepts a projectconfig struct and renders the templates for all referenced modules
func Generate(projectConfig projectconfig.ZeroProjectConfig) error {
flog.Infof(":clock: Fetching Modules")

// for _, module := range projectConfig.Modules {
// Make sure module sources are on disk
wg := sync.WaitGroup{}
wg.Add(len(projectConfig.Modules))
for _, mod := range projectConfig.Modules {
go module.FetchModule(mod.Files.Source, &wg)
}
wg.Wait()

// moduleDir := path.Join(module.GetSourceDir(module..Source), mod.Config.TemplateConfig.InputDir)
// delimiters := mod.Config.TemplateConfig.Delimiters
// overwrite := mod.Overwrite
// outputDir := mod.Output
// if outputDir == "" {
// outputDir = mod.Config.TemplateConfig.OutputDir
// }
flog.Infof(":pencil: Rendering Modules")
for _, mod := range projectConfig.Modules {
// Load module configuration
moduleConfig, err := module.ParseModuleConfig(mod.Files.Source)
if err != nil {
return fmt.Errorf("unable to load module: %v", err)
}

// templateData := TemplateParams{}
// templateData.Name = generatorCfg.Name
// templateData.Context = generatorCfg.Context
// templateData.Params = mod.Params
moduleDir := path.Join(module.GetSourceDir(mod.Files.Source), moduleConfig.InputDir)
delimiters := moduleConfig.Delimiters
outputDir := mod.Files.Directory

// Data that will be passed in to each template
templateData := struct {
Name string
Params projectconfig.Parameters
}{
projectConfig.Name,
mod.Parameters,
}

// fileTmplts := NewTemplates(moduleDir, outputDir, overwrite)
fileTemplates := NewTemplates(moduleDir, outputDir, false)

// ExecuteTemplates(fileTmplts, templateData, delimiters)
// }
ExecuteTemplates(fileTemplates, templateData, delimiters)
}

return nil
}
Expand Down Expand Up @@ -149,8 +133,8 @@ func ExecuteTemplates(templates []*TemplateConfig, data interface{}, delimiters
if rightDelim == "" {
rightDelim = "}}"
}
flog.Infof("Templating params:")
pp.Println(data)
// flog.Infof("Templating params:")
// pp.Println(data)

for _, tmpltConfig := range templates {
source := tmpltConfig.source
Expand Down
6 changes: 2 additions & 4 deletions internal/generate/generate_test.go
Expand Up @@ -3,8 +3,6 @@ package generate
import (
"os"
"testing"

config "github.com/commitdev/zero/internal/config"
)

func setupTeardown(t *testing.T) func(t *testing.T) {
Expand All @@ -20,7 +18,7 @@ func TestGenerateModules(t *testing.T) {
defer teardown(t)

// TODO organize test utils and write assertions
generatorConfig := config.LoadGeneratorConfig("../../tests/test_data/configs/zero-basic.yml")
// generatorConfig := config.LoadGeneratorConfig("../../tests/test_data/configs/zero-basic.yml")

GenerateModules(generatorConfig)
// GenerateModules(generatorConfig)
}
7 changes: 0 additions & 7 deletions internal/module/module.go
Expand Up @@ -9,20 +9,13 @@ import (
"regexp"
"sync"

"github.com/commitdev/zero/internal/config"
"github.com/commitdev/zero/internal/config/moduleconfig"
"github.com/commitdev/zero/internal/constants"
"github.com/commitdev/zero/internal/util"
"github.com/commitdev/zero/pkg/util/exit"
"github.com/hashicorp/go-getter"
)

// TemplateModule merges a module instance params with the static configs
type TemplateModule struct {
config.ModuleInstance // @TODO Move this
Config moduleconfig.ModuleConfig
}

// FetchModule downloads the remote module source if necessary. Meant to be run in a goroutine.
func FetchModule(source string, wg *sync.WaitGroup) {
defer wg.Done()
Expand Down

0 comments on commit 2321b10

Please sign in to comment.