Skip to content

Commit 6671f3c

Browse files
committed
Added prompts for projects names and switched module loading to happen asynchronously
1 parent a09ffcf commit 6671f3c

File tree

3 files changed

+55
-14
lines changed

3 files changed

+55
-14
lines changed

internal/context/init.go

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package context
22

33
import (
4+
"fmt"
45
"os"
56
"path"
7+
"sync"
68

79
"github.com/aws/aws-sdk-go/aws"
810
"github.com/aws/aws-sdk-go/aws/awserr"
@@ -37,7 +39,14 @@ func Init(outDir string) *projectconfig.ZeroProjectConfig {
3739
exit.Fatal("Error creating root: %v ", err)
3840
}
3941

40-
prompts := getProjectPrompts(projectConfig.Name)
42+
moduleSources := chooseStack(getRegistry())
43+
moduleConfigs := loadAllModules(moduleSources)
44+
for _ = range moduleConfigs {
45+
// TODO: initialize module structs inside project
46+
}
47+
48+
prompts := getProjectPrompts(projectConfig.Name, moduleConfigs)
49+
4150
projectConfig.Parameters["ShouldPushRepoUpstream"] = prompts["ShouldPushRepoUpstream"].GetParam(projectConfig.Parameters)
4251
// Prompting for push-up stream, then conditionally prompting for github
4352
projectConfig.Parameters["GithubRootOrg"] = prompts["GithubRootOrg"].GetParam(projectConfig.Parameters)
@@ -48,18 +57,19 @@ func Init(outDir string) *projectconfig.ZeroProjectConfig {
4857
projectCredential.GithubResourceConfig.AccessToken = personalToken
4958
globalconfig.Save(projectCredential)
5059
}
51-
moduleSources := chooseStack(getRegistry())
52-
moduleConfigs := loadAllModules(moduleSources)
53-
for _ = range moduleConfigs {
54-
// TODO: initialize module structs inside project
55-
}
5660

5761
projectParameters := promptAllModules(moduleConfigs)
5862
for k, v := range projectParameters {
5963
projectConfig.Parameters[k] = v
6064
// TODO: Add parameters to module structs inside project
6165
}
6266

67+
for moduleName, _ := range moduleConfigs {
68+
// @TODO : Uncomment when this struct is implemented
69+
//projectConfig.Modules[moduleName].Files.Directory = prompts[moduleName].GetParam(projectConfig.Parameters))
70+
fmt.Println(prompts[moduleName].GetParam(projectConfig.Parameters))
71+
}
72+
6373
// TODO: load ~/.zero/config.yml (or credentials)
6474
// TODO: prompt global credentials
6575

@@ -70,8 +80,15 @@ func Init(outDir string) *projectconfig.ZeroProjectConfig {
7080
func loadAllModules(moduleSources []string) map[string]moduleconfig.ModuleConfig {
7181
modules := make(map[string]moduleconfig.ModuleConfig)
7282

83+
wg := sync.WaitGroup{}
84+
wg.Add(len(moduleSources))
85+
for _, moduleSource := range moduleSources {
86+
go module.FetchModule(moduleSource, &wg)
87+
}
88+
wg.Wait()
89+
7390
for _, moduleSource := range moduleSources {
74-
mod, err := module.FetchModule(moduleSource)
91+
mod, err := module.ParseModuleConfig(moduleSource)
7592
if err != nil {
7693
exit.Fatal("Unable to load module: %v\n", err)
7794
}
@@ -106,8 +123,8 @@ func getProjectNamePrompt() PromptHandler {
106123
}
107124
}
108125

109-
func getProjectPrompts(projectName string) map[string]PromptHandler {
110-
return map[string]PromptHandler{
126+
func getProjectPrompts(projectName string, modules map[string]moduleconfig.ModuleConfig) map[string]PromptHandler {
127+
handlers := map[string]PromptHandler{
111128
"ShouldPushRepoUpstream": {
112129
moduleconfig.Parameter{
113130
Field: "ShouldPushRepoUpstream",
@@ -133,6 +150,21 @@ func getProjectPrompts(projectName string) map[string]PromptHandler {
133150
KeyMatchCondition("ShouldPushRepoUpstream", "y"),
134151
},
135152
}
153+
154+
for moduleName, module := range modules {
155+
label := fmt.Sprintf("What do you want to call the %s project?", moduleName)
156+
157+
handlers[moduleName] = PromptHandler{
158+
moduleconfig.Parameter{
159+
Field: moduleName,
160+
Label: label,
161+
Default: module.OutputDir,
162+
},
163+
NoCondition,
164+
}
165+
}
166+
167+
return handlers
136168
}
137169

138170
func chooseCloudProvider(projectConfig *projectconfig.ZeroProjectConfig) {

internal/module/module.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ import (
77
"log"
88
"path"
99
"regexp"
10+
"sync"
1011

1112
"github.com/commitdev/zero/internal/config"
1213
"github.com/commitdev/zero/internal/config/moduleconfig"
1314
"github.com/commitdev/zero/internal/constants"
1415
"github.com/commitdev/zero/internal/util"
16+
"github.com/commitdev/zero/pkg/util/exit"
1517
"github.com/hashicorp/go-getter"
1618
)
1719

@@ -21,17 +23,24 @@ type TemplateModule struct {
2123
Config moduleconfig.ModuleConfig
2224
}
2325

24-
// FetchModule downloads the remote module source (or loads the local files) and parses the module config yaml
25-
func FetchModule(source string) (moduleconfig.ModuleConfig, error) {
26-
config := moduleconfig.ModuleConfig{}
26+
// FetchModule downloads the remote module source if necessary. Meant to be run in a goroutine.
27+
func FetchModule(source string, wg *sync.WaitGroup) {
28+
defer wg.Done()
29+
2730
localPath := GetSourceDir(source)
2831
if !isLocal(source) {
2932
err := getter.Get(localPath, source)
3033
if err != nil {
31-
return config, err
34+
exit.Fatal("Failed to fetch remote module from %s: %v\n", source, err)
3235
}
3336
}
37+
return
38+
}
3439

40+
// ParseModuleConfig loads the local config file for a module and parses the yaml
41+
func ParseModuleConfig(source string) (moduleconfig.ModuleConfig, error) {
42+
localPath := GetSourceDir(source)
43+
config := moduleconfig.ModuleConfig{}
3544
configPath := path.Join(localPath, constants.ZeroModuleYml)
3645
config, err := moduleconfig.LoadModuleConfig(configPath)
3746
return config, err

internal/module/module_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func TestNewTemplateModule(t *testing.T) {
3232
var mod moduleconfig.ModuleConfig
3333

3434
t.Run("Loading module from source", func(t *testing.T) {
35-
mod, _ = module.FetchModule(testModuleSource)
35+
mod, _ = module.ParseModuleConfig(testModuleSource)
3636

3737
assert.Equal(t, "CI templates", mod.Name)
3838
})

0 commit comments

Comments
 (0)