Skip to content

Commit 5c6786c

Browse files
committed
moving apply to internal/apply
1 parent aef6b57 commit 5c6786c

File tree

6 files changed

+68
-134
lines changed

6 files changed

+68
-134
lines changed

cmd/apply.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package cmd
22

33
import (
4+
"github.com/commitdev/zero/internal/apply"
45
"github.com/commitdev/zero/internal/config/projectconfig"
56
"github.com/commitdev/zero/internal/constants"
6-
"github.com/commitdev/zero/internal/context"
77
"github.com/spf13/cobra"
88
)
99

@@ -21,12 +21,7 @@ var applyCmd = &cobra.Command{
2121
Use: "apply",
2222
Short: "Execute modules to create projects, infrastructure, etc.",
2323
Run: func(cmd *cobra.Command, args []string) {
24-
25-
// @TODO : Pass environments to make commands
26-
27-
// @TODO where applyConfigPath comes from?
28-
projectContext := context.Apply(applyEnvironments, applyConfigPath)
2924
// @TODO rootdir?
30-
projectconfig.Apply(projectconfig.RootDir, projectContext, applyEnvironments)
25+
apply.Apply(projectconfig.RootDir, applyConfigPath, applyEnvironments)
3126
},
3227
}
Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,40 @@
1-
package context
1+
package apply
22

33
import (
44
"fmt"
5+
56
"log"
7+
"os/exec"
68
"path"
9+
"strings"
10+
11+
"github.com/commitdev/zero/internal/util"
12+
"github.com/commitdev/zero/pkg/util/flog"
713

814
"github.com/commitdev/zero/internal/config/projectconfig"
915
"github.com/commitdev/zero/pkg/util/exit"
1016
"github.com/manifoldco/promptui"
1117
)
1218

13-
// Apply will load the context/configuration to be used by the apply command
14-
func Apply(applyEnvironments []string, applyConfigPath string) *projectconfig.ZeroProjectConfig {
19+
// Apply will bootstrap the runtime environment for the project
20+
func Apply(dir string, applyConfigPath string, applyEnvironments []string) []string {
21+
context := loadContext(dir, applyConfigPath, applyEnvironments)
22+
23+
flog.Infof(":tada: Bootstrapping project %s. Please use the zero.[hcl, yaml] file to modify the project as needed. %s.", context.Name)
24+
25+
flog.Infof("Cloud provider: %s", "AWS") // will this come from the config?
26+
27+
flog.Infof("Runtime platform: %s", "Kubernetes")
28+
29+
flog.Infof("Infrastructure executor: %s", "Terraform")
30+
31+
// other details...
32+
33+
return makeAll(dir, context, applyEnvironments)
34+
}
35+
36+
// loadContext will load the context/configuration to be used by the apply command
37+
func loadContext(dir string, applyConfigPath string, applyEnvironments []string) *projectconfig.ZeroProjectConfig {
1538
if len(applyEnvironments) == 0 {
1639
fmt.Println(`Choose the environments to apply. This will create infrastructure, CI pipelines, etc.
1740
At this point, real things will be generated that may cost money!
@@ -24,8 +47,7 @@ Only a single environment may be suitable for an initial test, but for a real sy
2447
if applyConfigPath == "" {
2548
exit.Fatal("config path cannot be empty!")
2649
}
27-
28-
configPath := path.Join(projectconfig.RootDir, applyConfigPath)
50+
configPath := path.Join(dir, applyConfigPath)
2951
projectConfig := projectconfig.LoadConfig(configPath)
3052
return projectConfig
3153
}
@@ -63,3 +85,18 @@ func validateEnvironments(applyEnvironments []string) {
6385
}
6486
}
6587
}
88+
89+
func makeAll(dir string, projectContext *projectconfig.ZeroProjectConfig, applyEnvironments []string) []string {
90+
environmentArg := fmt.Sprintf("ENVIRONMENT=%s", strings.Join(applyEnvironments, ","))
91+
envList := []string{environmentArg}
92+
outputs := []string{}
93+
94+
for _, mod := range projectContext.Modules {
95+
modulePath := path.Join(dir, mod.Files.Directory)
96+
envList = util.AppendProjectEnvToCmdEnv(mod.Parameters, envList)
97+
98+
output := util.ExecuteCommandOutput(exec.Command("make"), modulePath, envList)
99+
outputs = append(outputs, output)
100+
}
101+
return outputs
102+
}
Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,17 @@
1-
package projectconfig_test
1+
package apply_test
22

33
import (
44
"testing"
55

6-
"github.com/commitdev/zero/internal/config/projectconfig"
6+
"github.com/commitdev/zero/internal/apply"
7+
"github.com/commitdev/zero/internal/constants"
78
"github.com/stretchr/testify/assert"
89
)
910

1011
func TestApply(t *testing.T) {
1112
// @TODO is there a way to do this without relative paths?
12-
// execCMD will use the current folder as target...
13-
dir := "../../../tests/test_data/"
14-
projectName := "sample_project"
15-
projectContext := &projectconfig.ZeroProjectConfig{
16-
Name: projectName,
17-
Modules: projectconfig.EKSGoReactSampleModules(),
18-
}
13+
dir := "../../tests/test_data/sample_project/"
14+
applyConfigPath := constants.ZeroProjectYml
1915
applyEnvironments := []string{"staging", "production"}
2016

2117
want := []string{
@@ -25,7 +21,7 @@ func TestApply(t *testing.T) {
2521
}
2622

2723
t.Run("Should run apply and execute make on each folder module", func(t *testing.T) {
28-
got := projectconfig.Apply(dir, projectContext, applyEnvironments)
29-
assert.Equal(t, want, got)
24+
got := apply.Apply(dir, applyConfigPath, applyEnvironments)
25+
assert.ElementsMatch(t, want, got)
3026
})
3127
}

internal/config/projectconfig/apply.go

Lines changed: 0 additions & 41 deletions
This file was deleted.

internal/module/module.go

Lines changed: 0 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ import (
55
"encoding/base64"
66
"io"
77
"log"
8-
"os"
9-
"os/exec"
108
"path"
119
"regexp"
1210
"sync"
@@ -17,7 +15,6 @@ import (
1715
"github.com/commitdev/zero/internal/util"
1816
"github.com/commitdev/zero/pkg/util/exit"
1917
"github.com/hashicorp/go-getter"
20-
"github.com/manifoldco/promptui"
2118
)
2219

2320
// TemplateModule merges a module instance params with the static configs
@@ -49,73 +46,6 @@ func ParseModuleConfig(source string) (moduleconfig.ModuleConfig, error) {
4946
return config, err
5047
}
5148

52-
// aws cli prints output with linebreak in them
53-
func sanitizePromptResult(str string) string {
54-
re := regexp.MustCompile("\\n")
55-
return re.ReplaceAllString(str, "")
56-
}
57-
58-
// TODO : Use this function signature instead
59-
// PromptParams renders series of prompt UI based on the config
60-
func PromptParams(moduleConfig moduleconfig.ModuleConfig, parameters map[string]string) (map[string]string, error) {
61-
return map[string]string{}, nil
62-
}
63-
64-
// PromptParams renders series of prompt UI based on the config
65-
func (m *TemplateModule) PromptParams(projectContext map[string]string) error {
66-
for _, promptConfig := range m.Config.Prompts {
67-
68-
label := promptConfig.Label
69-
if promptConfig.Label == "" {
70-
label = promptConfig.Field
71-
}
72-
73-
// deduplicate fields already prompted and received
74-
if _, isAlreadySet := projectContext[promptConfig.Field]; isAlreadySet {
75-
continue
76-
}
77-
78-
var err error
79-
var result string
80-
if len(promptConfig.Options) > 0 {
81-
prompt := promptui.Select{
82-
Label: label,
83-
Items: promptConfig.Options,
84-
}
85-
_, result, err = prompt.Run()
86-
87-
} else if promptConfig.Execute != "" {
88-
// TODO: this could perhaps be set as a default for part of regular prompt
89-
cmd := exec.Command("bash", "-c", promptConfig.Execute)
90-
cmd.Env = util.AppendProjectEnvToCmdEnv(projectContext, os.Environ())
91-
out, err := cmd.Output()
92-
93-
if err != nil {
94-
log.Fatalf("Failed to execute %v\n", err)
95-
panic(err)
96-
}
97-
result = string(out)
98-
} else {
99-
prompt := promptui.Prompt{
100-
Label: label,
101-
}
102-
result, err = prompt.Run()
103-
}
104-
if err != nil {
105-
return err
106-
}
107-
108-
result = sanitizePromptResult(result)
109-
if m.Params == nil {
110-
m.Params = make(map[string]string)
111-
}
112-
m.Params[promptConfig.Field] = result
113-
projectContext[promptConfig.Field] = result
114-
}
115-
116-
return nil
117-
}
118-
11949
// GetSourcePath gets a unique local source directory name. For local modules, it use the local directory
12050
func GetSourceDir(source string) string {
12151
if !isLocal(source) {
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
name: sample_project
2+
3+
context:
4+
5+
modules:
6+
aws-eks-stack:
7+
files:
8+
dir: zero-aws-eks-stack
9+
repo: github.com/commitdev/zero-aws-eks-stack
10+
deployable-backend:
11+
files:
12+
dir: zero-deployable-backend
13+
repo: github.com/commitdev/zero-deployable-backend
14+
deployable-react-frontend:
15+
files:
16+
dir: zero-deployable-react-frontend
17+
repo: github.com/commitdev/zero-deployable-react-frontend

0 commit comments

Comments
 (0)