-
Notifications
You must be signed in to change notification settings - Fork 53
#134 Add prompts for project names #156
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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" | ||
|
|
@@ -28,7 +30,7 @@ func Init(outDir string) *projectconfig.ZeroProjectConfig { | |
| projectConfig.Name = getProjectNamePrompt().GetParam(projectConfig.Parameters) | ||
|
|
||
| rootDir := path.Join(outDir, projectConfig.Name) | ||
| flog.Infof(":tada: Creating project") | ||
| flog.Infof(":tada: Initializing project") | ||
|
|
||
| err := os.MkdirAll(rootDir, os.ModePerm) | ||
| if os.IsExist(err) { | ||
|
|
@@ -37,29 +39,45 @@ func Init(outDir string) *projectconfig.ZeroProjectConfig { | |
| exit.Fatal("Error creating root: %v ", err) | ||
| } | ||
|
|
||
| prompts := getProjectPrompts(projectConfig.Name) | ||
| 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) | ||
| personalToken := prompts["githubPersonalToken"].GetParam(projectConfig.Parameters) | ||
| if personalToken != "" && personalToken != globalconfig.GetUserCredentials(projectConfig.Name).AccessToken { | ||
| projectConfig.Parameters["githubPersonalToken"] = personalToken | ||
| projectCredential := globalconfig.GetUserCredentials(projectConfig.Name) | ||
| projectCredential.GithubResourceConfig.AccessToken = personalToken | ||
| globalconfig.Save(projectCredential) | ||
| } | ||
| moduleSources := chooseStack(getRegistry()) | ||
| moduleConfigs := loadAllModules(moduleSources) | ||
| for _ = range moduleConfigs { | ||
| // TODO: initialize module structs inside project | ||
| } | ||
|
|
||
| prompts := getProjectPrompts(projectConfig.Name, moduleConfigs) | ||
|
|
||
| initParams := make(map[string]string) | ||
| projectConfig.ShouldPushRepositories = true | ||
| initParams["ShouldPushRepositories"] = prompts["ShouldPushRepositories"].GetParam(initParams) | ||
| if initParams["ShouldPushRepositories"] == "n" { | ||
| projectConfig.ShouldPushRepositories = false | ||
| } | ||
|
|
||
| // Prompting for push-up stream, then conditionally prompting for github | ||
| initParams["GithubRootOrg"] = prompts["GithubRootOrg"].GetParam(initParams) | ||
| initParams["GithubPersonalToken"] = prompts["GithubPersonalToken"].GetParam(initParams) | ||
| if initParams["GithubRootOrg"] != "" && initParams["GithubPersonalToken"] != globalconfig.GetUserCredentials(projectConfig.Name).AccessToken { | ||
| projectCredential := globalconfig.GetUserCredentials(projectConfig.Name) | ||
| projectCredential.GithubResourceConfig.AccessToken = initParams["GithubPersonalToken"] | ||
| globalconfig.Save(projectCredential) | ||
| } | ||
|
|
||
| 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 | ||
| repoName := prompts[moduleName].GetParam(initParams) | ||
| repoURL := fmt.Sprintf("%s/%s", initParams["GithubRootOrg"], repoName) | ||
| //projectConfig.Modules[moduleName].Files.Directory = prompts[moduleName].GetParam(initParams) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll uncomment this after Thiago's PR is merged. |
||
| //projectConfig.Modules[moduleName].Files.Repository = repoURL | ||
| fmt.Println(repoURL) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. stray print command
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, reminder that that code needs to be filled in. |
||
| } | ||
|
|
||
| // TODO: load ~/.zero/config.yml (or credentials) | ||
| // TODO: prompt global credentials | ||
|
|
||
|
|
@@ -70,8 +88,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) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fetching will be done in parallel now |
||
| } | ||
| wg.Wait() | ||
|
|
||
| for _, moduleSource := range moduleSources { | ||
| mod, err := module.FetchModule(moduleSource) | ||
| mod, err := module.ParseModuleConfig(moduleSource) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Split the parsing out from the fetching |
||
| if err != nil { | ||
| exit.Fatal("Unable to load module: %v\n", err) | ||
| } | ||
|
|
@@ -103,36 +128,56 @@ func getProjectNamePrompt() PromptHandler { | |
| Default: "", | ||
| }, | ||
| NoCondition, | ||
| NoValidation, | ||
| } | ||
| } | ||
|
|
||
| func getProjectPrompts(projectName string) map[string]PromptHandler { | ||
| return map[string]PromptHandler{ | ||
| "ShouldPushRepoUpstream": { | ||
| func getProjectPrompts(projectName string, modules map[string]moduleconfig.ModuleConfig) map[string]PromptHandler { | ||
| handlers := map[string]PromptHandler{ | ||
| "ShouldPushRepositories": { | ||
| moduleconfig.Parameter{ | ||
| Field: "ShouldPushRepoUpstream", | ||
| Field: "ShouldPushRepositories", | ||
| Label: "Should the created projects be checked into github automatically? (y/n)", | ||
| Default: "y", | ||
| }, | ||
| NoCondition, | ||
| SpecificValueValidation("y", "n"), | ||
| }, | ||
| "GithubRootOrg": { | ||
| moduleconfig.Parameter{ | ||
| Field: "GithubRootOrg", | ||
| Label: "What's the root of the github org to create repositories in?", | ||
| Default: "github.com/", | ||
| }, | ||
| KeyMatchCondition("ShouldPushRepoUpstream", "y"), | ||
| KeyMatchCondition("ShouldPushRepositories", "y"), | ||
| NoValidation, | ||
| }, | ||
| "githubPersonalToken": { | ||
| "GithubPersonalToken": { | ||
| moduleconfig.Parameter{ | ||
| Field: "githubPersonalToken", | ||
| Field: "GithubPersonalToken", | ||
| Label: "Github Personal Access Token with access to the above organization", | ||
| Default: globalconfig.GetUserCredentials(projectName).AccessToken, | ||
| }, | ||
| KeyMatchCondition("ShouldPushRepoUpstream", "y"), | ||
| KeyMatchCondition("ShouldPushRepositories", "y"), | ||
| NoValidation, | ||
| }, | ||
| } | ||
|
|
||
| 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, | ||
| NoValidation, | ||
| } | ||
| } | ||
|
|
||
| return handlers | ||
| } | ||
|
|
||
| func chooseCloudProvider(projectConfig *projectconfig.ZeroProjectConfig) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ import ( | |
| "os" | ||
| "os/exec" | ||
| "regexp" | ||
| "strings" | ||
|
|
||
| "github.com/commitdev/zero/internal/config/moduleconfig" | ||
| "github.com/commitdev/zero/pkg/util/exit" | ||
|
|
@@ -15,17 +16,34 @@ import ( | |
| type PromptHandler struct { | ||
| moduleconfig.Parameter | ||
| Condition func(map[string]string) bool | ||
| Validate func(string) error | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎉 maybe later on we can implement some type of openapi schema validation, then user can supply that on the module side and we can consume from Parameter struct as well |
||
| } | ||
|
|
||
| func NoCondition(map[string]string) bool { | ||
| return true | ||
| } | ||
|
|
||
| func KeyMatchCondition(key string, value string) func(map[string]string) bool { | ||
| return func(param map[string]string) bool { | ||
| return param[key] == value | ||
| } | ||
| } | ||
|
|
||
| func NoValidation(string) error { | ||
| return nil | ||
| } | ||
|
|
||
| func SpecificValueValidation(values ...string) func(string) error { | ||
| return func(checkValue string) error { | ||
| for _, allowedValue := range values { | ||
| if checkValue == allowedValue { | ||
| return nil | ||
| } | ||
| } | ||
| return fmt.Errorf("Please choose one of %s", strings.Join(values, "/")) | ||
| } | ||
| } | ||
|
|
||
| // TODO: validation / allow prompt retry ...etc | ||
| func (p PromptHandler) GetParam(projectParams map[string]string) string { | ||
| var err error | ||
|
|
@@ -40,7 +58,7 @@ func (p PromptHandler) GetParam(projectParams map[string]string) string { | |
| } else if p.Parameter.Value != "" { | ||
| result = p.Parameter.Value | ||
| } else { | ||
| err, result = promptParameter(p.Parameter) | ||
| err, result = promptParameter(p) | ||
| } | ||
| if err != nil { | ||
| exit.Fatal("Exiting prompt: %v\n", err) | ||
|
|
@@ -51,7 +69,8 @@ func (p PromptHandler) GetParam(projectParams map[string]string) string { | |
| return "" | ||
| } | ||
|
|
||
| func promptParameter(param moduleconfig.Parameter) (error, string) { | ||
| func promptParameter(prompt PromptHandler) (error, string) { | ||
| param := prompt.Parameter | ||
| label := param.Label | ||
| if param.Label == "" { | ||
| label = param.Field | ||
|
|
@@ -72,6 +91,7 @@ func promptParameter(param moduleconfig.Parameter) (error, string) { | |
| Label: label, | ||
| Default: defaultValue, | ||
| AllowEdit: true, | ||
| Validate: prompt.Validate, | ||
| } | ||
| result, err = prompt.Run() | ||
| } | ||
|
|
@@ -119,6 +139,7 @@ func PromptModuleParams(moduleConfig moduleconfig.ModuleConfig, parameters map[s | |
| promptHandler := PromptHandler{ | ||
| promptConfig, | ||
| NoCondition, | ||
| NoValidation, | ||
| } | ||
| result := promptHandler.GetParam(parameters) | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Gonna remove this
Infrastructurestruct entirely in another PR