Skip to content

Commit

Permalink
Added prompts for projects names and switched module loading to happe…
Browse files Browse the repository at this point in the history
…n asynchronously
  • Loading branch information
bmonkman committed Jun 9, 2020
1 parent a09ffcf commit 6671f3c
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 14 deletions.
50 changes: 41 additions & 9 deletions 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"
Expand Down Expand Up @@ -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)
Expand All @@ -48,18 +57,19 @@ 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 {
projectConfig.Parameters[k] = v
// 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

Expand All @@ -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)
}
Expand Down Expand Up @@ -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",
Expand All @@ -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) {
Expand Down
17 changes: 13 additions & 4 deletions internal/module/module.go
Expand Up @@ -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"
)

Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion internal/module/module_test.go
Expand Up @@ -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)
})
Expand Down

0 comments on commit 6671f3c

Please sign in to comment.