@@ -2,54 +2,77 @@ package apply
22
33import (
44 "fmt"
5+ "path/filepath"
56
67 "log"
78 "os/exec"
89 "path"
910 "strings"
1011
12+ "github.com/commitdev/zero/internal/module"
1113 "github.com/commitdev/zero/internal/util"
12- "github.com/commitdev/zero/pkg/util/flog"
1314
15+ "github.com/commitdev/zero/internal/config/globalconfig"
1416 "github.com/commitdev/zero/internal/config/projectconfig"
1517 "github.com/commitdev/zero/pkg/util/exit"
18+ "github.com/commitdev/zero/pkg/util/flog"
1619 "github.com/manifoldco/promptui"
1720)
1821
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+ func Apply (rootDir string , configPath string , environments []string ) {
23+ if strings .Trim (configPath , " " ) == "" {
24+ exit .Fatal ("config path cannot be empty!" )
25+ }
26+ configFilePath := path .Join (rootDir , configPath )
27+ projectConfig := projectconfig .LoadConfig (configFilePath )
28+
29+ if len (environments ) == 0 {
30+ fmt .Println (`Choose the environments to apply. This will create infrastructure, CI pipelines, etc.
31+ At this point, real things will be generated that may cost money!
32+ Only a single environment may be suitable for an initial test, but for a real system we suggest setting up both staging and production environments.` )
33+ environments = promptEnvironments ()
34+ }
2235
23- flog .Infof (":tada: Bootstrapping project %s. Please use the zero.[hcl, yaml] file to modify the project as needed. %s. " , context .Name )
36+ flog .Infof (":tada: Bootstrapping project %s. Please use the zero-project.yml file to modify the project as needed." , projectConfig .Name )
2437
2538 flog .Infof ("Cloud provider: %s" , "AWS" ) // will this come from the config?
2639
2740 flog .Infof ("Runtime platform: %s" , "Kubernetes" )
2841
2942 flog .Infof ("Infrastructure executor: %s" , "Terraform" )
3043
31- // other details...
44+ applyAll ( rootDir , * projectConfig , environments )
3245
33- return makeAll (dir , context , applyEnvironments )
46+ // TODO Summary
47+ flog .Infof (":check_mark_button: Done - Summary goes here." )
3448}
3549
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 {
38- if len (applyEnvironments ) == 0 {
39- fmt .Println (`Choose the environments to apply. This will create infrastructure, CI pipelines, etc.
40- At this point, real things will be generated that may cost money!
41- Only a single environment may be suitable for an initial test, but for a real system we suggest setting up both staging and production environments.` )
42- applyEnvironments = promptEnvironments ()
43- }
50+ func applyAll (dir string , projectConfig projectconfig.ZeroProjectConfig , applyEnvironments []string ) {
51+ environmentArg := fmt .Sprintf ("ENVIRONMENT=%s" , strings .Join (applyEnvironments , "," ))
4452
45- validateEnvironments (applyEnvironments )
53+ // Go through each of the modules and run `make`
54+ for _ , mod := range projectConfig .Modules {
55+ // Add env vars for the makefile
56+ envList := []string {
57+ environmentArg ,
58+ fmt .Sprintf ("PROJECT_DIR=%s" , path .Join (dir , mod .Files .Directory )),
59+ fmt .Sprintf ("REPOSITORY=%s" , mod .Files .Repository ),
60+ }
4661
47- if applyConfigPath == "" {
48- exit .Fatal ("config path cannot be empty!" )
62+ modulePath := module .GetSourceDir (mod .Files .Source )
63+ // Passed in `dir` will only be used to find the project path, not the module path,
64+ // unless the module path is relative
65+ if module .IsLocal (mod .Files .Source ) && ! filepath .IsAbs (modulePath ) {
66+ modulePath = filepath .Join (dir , modulePath )
67+ }
68+
69+ // Get project credentials for the makefile
70+ credentials := globalconfig .GetProjectCredentials (projectConfig .Name )
71+
72+ envList = util .AppendProjectEnvToCmdEnv (mod .Parameters , envList )
73+ envList = util .AppendProjectEnvToCmdEnv (credentials .AsEnvVars (), envList )
74+ util .ExecuteCommand (exec .Command ("make" ), modulePath , envList )
4975 }
50- configPath := path .Join (dir , applyConfigPath )
51- projectConfig := projectconfig .LoadConfig (configPath )
52- return projectConfig
5376}
5477
5578// promptEnvironments Prompts the user for the environments to apply against and returns a slice of strings representing the environments
@@ -85,18 +108,3 @@ func validateEnvironments(applyEnvironments []string) {
85108 }
86109 }
87110}
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- }
0 commit comments