Skip to content

Commit

Permalink
Merge pull request #129 from commitdev/zero-init
Browse files Browse the repository at this point in the history
Zero init
  • Loading branch information
davidcheung committed May 29, 2020
2 parents e70e167 + 2dfdb41 commit 672019a
Show file tree
Hide file tree
Showing 20 changed files with 298 additions and 179 deletions.
2 changes: 1 addition & 1 deletion cmd/apply.go
Expand Up @@ -8,7 +8,7 @@ import (
var applyConfigPath string

func init() {
applyCmd.PersistentFlags().StringVarP(&applyConfigPath, "config", "c", configs.CommitYml, "config path")
applyCmd.PersistentFlags().StringVarP(&applyConfigPath, "config", "c", configs.ZeroProjectYml, "config path")

rootCmd.AddCommand(applyCmd)
}
Expand Down
27 changes: 0 additions & 27 deletions cmd/create.go

This file was deleted.

5 changes: 2 additions & 3 deletions cmd/init.go
Expand Up @@ -20,8 +20,7 @@ var initCmd = &cobra.Command{
}

projectName := args[0]
context.Create(projectName, "./")

config.CreateExample(projectName)
projectContext := context.Init(projectName, config.RootDir)
config.Init(config.RootDir, projectName, projectContext)
},
}
8 changes: 4 additions & 4 deletions configs/configs.go
@@ -1,8 +1,8 @@
package configs

const (
TemplatesDir = "tmp/templates"
CommitYml = "zero.yml"
IgnoredPaths = "(?i)zero.module.yml|.git/"
TemplateExtn = ".tmpl"
TemplatesDir = "tmp/templates"
ZeroProjectYml = "zero-project.yml"
IgnoredPaths = "(?i)zero.module.yml|.git/"
TemplateExtn = ".tmpl"
)
1 change: 1 addition & 0 deletions go.mod
Expand Up @@ -3,6 +3,7 @@ module github.com/commitdev/zero
go 1.12

require (
github.com/766b/go-outliner v0.0.0-20180511142203-fc6edecdadd7 // indirect
github.com/aws/aws-sdk-go v1.25.33
github.com/commitdev/commit0 v0.0.0-20200522203431-5176cd09c654
github.com/coreos/go-semver v0.2.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Expand Up @@ -7,6 +7,8 @@ cloud.google.com/go v0.45.1 h1:lRi0CHyU+ytlvylOlFKKq0af6JncuyoRh1J+QJBqQx0=
cloud.google.com/go v0.45.1/go.mod h1:RpBamKRgapWJb87xiFSdk4g1CME7QZg3uwTez+TSTjc=
cloud.google.com/go/bigquery v1.0.1/go.mod h1:i/xbL2UlR5RvWAURpBYZTtm/cXjCha9lbfbpx4poX+o=
cloud.google.com/go/datastore v1.0.0/go.mod h1:LXYbyblFSglQ5pkeyhO+Qmw7ukd3C+pD7TKLgZqpHYE=
github.com/766b/go-outliner v0.0.0-20180511142203-fc6edecdadd7 h1:cJXisB2yAM61AzMutv7X+KM8F3xVLxGH99S8VmaSlps=
github.com/766b/go-outliner v0.0.0-20180511142203-fc6edecdadd7/go.mod h1:1SzhThoS5lcKfE4IFOLQJ04WCmFpaAiPe8H9yqXyYSU=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
Expand Down
2 changes: 1 addition & 1 deletion internal/api/generate_api.go
Expand Up @@ -15,7 +15,7 @@ func generateProject(w http.ResponseWriter, req *http.Request) {
switch req.Method {
case "POST":
decoder := json.NewDecoder(req.Body)
var projectConfig config.Commit0Config
var projectConfig config.ZeroProjectConfig
err := decoder.Decode(&projectConfig)
if err != nil {
panic(err)
Expand Down
9 changes: 5 additions & 4 deletions internal/config/context_config.go
Expand Up @@ -8,10 +8,11 @@ import (
yaml "gopkg.in/yaml.v2"
)

type Commit0Config struct {
type ZeroProjectConfig struct {
Name string
Infrastructure Infrastructure // TODO simplify and flatten / rename?
Context map[string]string
Modules []string
}

type Infrastructure struct {
Expand All @@ -28,8 +29,8 @@ type terraform struct {
RemoteState bool
}

func LoadConfig(filePath string) *Commit0Config {
config := &Commit0Config{}
func LoadConfig(filePath string) *ZeroProjectConfig {
config := &ZeroProjectConfig{}
data, err := ioutil.ReadFile(filePath)
if err != nil {
log.Panicf("failed to read config: %v", err)
Expand All @@ -42,6 +43,6 @@ func LoadConfig(filePath string) *Commit0Config {
return config
}

func (c *Commit0Config) Print() {
func (c *ZeroProjectConfig) Print() {
pp.Println(c)
}
32 changes: 0 additions & 32 deletions internal/config/create_example.go

This file was deleted.

43 changes: 43 additions & 0 deletions internal/config/init.go
@@ -0,0 +1,43 @@
package config

import (
"fmt"
"io/ioutil"
"path"

"github.com/commitdev/zero/configs"
"github.com/commitdev/zero/pkg/util/exit"
)

const exampleConfig = `name: %s
# Context is normally populated automatically but could be used to inject global params
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"
modules:
- source: "github.com/commitdev/zero-aws-eks-stack"
- source: "github.com/commitdev/zero-deployable-backend"
- source: "github.com/commitdev/zero-deployable-react-frontend"
`

var RootDir = "./"

func SetRootDir(dir string) {
RootDir = dir
}

func Init(dir string, projectName string, projectContext *ZeroProjectConfig) {
// TODO: template the zero-project.yml with projectContext
content := []byte(fmt.Sprintf(exampleConfig, projectName))

err := ioutil.WriteFile(path.Join(dir, projectName, configs.ZeroProjectYml), content, 0644)
if err != nil {
exit.Fatal(fmt.Sprintf("Failed to create example %s", configs.ZeroProjectYml))
}
}
31 changes: 31 additions & 0 deletions internal/config/init_test.go
@@ -0,0 +1,31 @@
package config_test

import (
"os"
"path"
"testing"

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

func TestInit(t *testing.T) {
const testDir = "../../test-sandbox"
projectName := "test-project"

config.SetRootDir(testDir)
defer os.RemoveAll(testDir)

testDirPath := path.Join(config.RootDir, projectName)
// create sandbox dir
err := os.MkdirAll(testDirPath, os.ModePerm)
if err != nil {
t.Fatal(err)
}

config.Init(config.RootDir, projectName, nil)

if _, err := os.Stat(path.Join(testDirPath, configs.ZeroProjectYml)); err != nil {
t.Fatal(err)
}
}
8 changes: 5 additions & 3 deletions internal/config/module_config.go
Expand Up @@ -15,13 +15,15 @@ type ModuleConfig struct {
Icon string
Thumbnail string
Template TemplateConfig
Prompts []Prompt
Credentials []string `yaml:"requiredCredentials"`
Prompts []Prompt `yaml:"parameters"`
}

type Prompt struct {
Field string
Field string `yaml:"field,omitempty"`
Label string
Options []string
Options []string `yaml:"options,omitempty"`
Execute string `yaml:"execute,omitempty"`
}

type TemplateConfig struct {
Expand Down
94 changes: 0 additions & 94 deletions internal/context/create.go

This file was deleted.

0 comments on commit 672019a

Please sign in to comment.