From 6671f3c7a872278fb242c86325ce0d422a3b9e19 Mon Sep 17 00:00:00 2001 From: Bill Monkman Date: Tue, 9 Jun 2020 14:27:31 -0700 Subject: [PATCH] Added prompts for projects names and switched module loading to happen asynchronously --- internal/context/init.go | 50 ++++++++++++++++++++++++++++------ internal/module/module.go | 17 +++++++++--- internal/module/module_test.go | 2 +- 3 files changed, 55 insertions(+), 14 deletions(-) diff --git a/internal/context/init.go b/internal/context/init.go index 2454f74d4..2ec2492db 100644 --- a/internal/context/init.go +++ b/internal/context/init.go @@ -1,8 +1,10 @@ package context import ( + "fmt" "os" "path" + "sync" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/awserr" @@ -37,7 +39,14 @@ func Init(outDir string) *projectconfig.ZeroProjectConfig { exit.Fatal("Error creating root: %v ", err) } - prompts := getProjectPrompts(projectConfig.Name) + moduleSources := chooseStack(getRegistry()) + moduleConfigs := loadAllModules(moduleSources) + for _ = range moduleConfigs { + // TODO: initialize module structs inside project + } + + prompts := getProjectPrompts(projectConfig.Name, moduleConfigs) + projectConfig.Parameters["ShouldPushRepoUpstream"] = prompts["ShouldPushRepoUpstream"].GetParam(projectConfig.Parameters) // Prompting for push-up stream, then conditionally prompting for github projectConfig.Parameters["GithubRootOrg"] = prompts["GithubRootOrg"].GetParam(projectConfig.Parameters) @@ -48,11 +57,6 @@ func Init(outDir string) *projectconfig.ZeroProjectConfig { projectCredential.GithubResourceConfig.AccessToken = personalToken globalconfig.Save(projectCredential) } - moduleSources := chooseStack(getRegistry()) - moduleConfigs := loadAllModules(moduleSources) - for _ = range moduleConfigs { - // TODO: initialize module structs inside project - } projectParameters := promptAllModules(moduleConfigs) for k, v := range projectParameters { @@ -60,6 +64,12 @@ func Init(outDir string) *projectconfig.ZeroProjectConfig { // TODO: Add parameters to module structs inside project } + for moduleName, _ := range moduleConfigs { + // @TODO : Uncomment when this struct is implemented + //projectConfig.Modules[moduleName].Files.Directory = prompts[moduleName].GetParam(projectConfig.Parameters)) + fmt.Println(prompts[moduleName].GetParam(projectConfig.Parameters)) + } + // TODO: load ~/.zero/config.yml (or credentials) // TODO: prompt global credentials @@ -70,8 +80,15 @@ func Init(outDir string) *projectconfig.ZeroProjectConfig { func loadAllModules(moduleSources []string) map[string]moduleconfig.ModuleConfig { modules := make(map[string]moduleconfig.ModuleConfig) + wg := sync.WaitGroup{} + wg.Add(len(moduleSources)) + for _, moduleSource := range moduleSources { + go module.FetchModule(moduleSource, &wg) + } + wg.Wait() + for _, moduleSource := range moduleSources { - mod, err := module.FetchModule(moduleSource) + mod, err := module.ParseModuleConfig(moduleSource) if err != nil { exit.Fatal("Unable to load module: %v\n", err) } @@ -106,8 +123,8 @@ func getProjectNamePrompt() PromptHandler { } } -func getProjectPrompts(projectName string) map[string]PromptHandler { - return map[string]PromptHandler{ +func getProjectPrompts(projectName string, modules map[string]moduleconfig.ModuleConfig) map[string]PromptHandler { + handlers := map[string]PromptHandler{ "ShouldPushRepoUpstream": { moduleconfig.Parameter{ Field: "ShouldPushRepoUpstream", @@ -133,6 +150,21 @@ func getProjectPrompts(projectName string) map[string]PromptHandler { KeyMatchCondition("ShouldPushRepoUpstream", "y"), }, } + + for moduleName, module := range modules { + label := fmt.Sprintf("What do you want to call the %s project?", moduleName) + + handlers[moduleName] = PromptHandler{ + moduleconfig.Parameter{ + Field: moduleName, + Label: label, + Default: module.OutputDir, + }, + NoCondition, + } + } + + return handlers } func chooseCloudProvider(projectConfig *projectconfig.ZeroProjectConfig) { diff --git a/internal/module/module.go b/internal/module/module.go index 3c4a18605..743f599b8 100644 --- a/internal/module/module.go +++ b/internal/module/module.go @@ -7,11 +7,13 @@ import ( "log" "path" "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" ) @@ -21,17 +23,24 @@ type TemplateModule struct { Config moduleconfig.ModuleConfig } -// FetchModule downloads the remote module source (or loads the local files) and parses the module config yaml -func FetchModule(source string) (moduleconfig.ModuleConfig, error) { - 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() + localPath := GetSourceDir(source) if !isLocal(source) { err := getter.Get(localPath, source) if err != nil { - return config, err + exit.Fatal("Failed to fetch remote module from %s: %v\n", source, err) } } + return +} +// ParseModuleConfig loads the local config file for a module and parses the yaml +func ParseModuleConfig(source string) (moduleconfig.ModuleConfig, error) { + localPath := GetSourceDir(source) + config := moduleconfig.ModuleConfig{} configPath := path.Join(localPath, constants.ZeroModuleYml) config, err := moduleconfig.LoadModuleConfig(configPath) return config, err diff --git a/internal/module/module_test.go b/internal/module/module_test.go index 40ca15c9d..4b0d82a6e 100644 --- a/internal/module/module_test.go +++ b/internal/module/module_test.go @@ -32,7 +32,7 @@ func TestNewTemplateModule(t *testing.T) { var mod moduleconfig.ModuleConfig t.Run("Loading module from source", func(t *testing.T) { - mod, _ = module.FetchModule(testModuleSource) + mod, _ = module.ParseModuleConfig(testModuleSource) assert.Equal(t, "CI templates", mod.Name) })