From ea94c2de1f11eb241b406bb6c07e43443500e0b8 Mon Sep 17 00:00:00 2001 From: Lea Anthony Date: Tue, 5 Feb 2019 18:51:08 +1100 Subject: [PATCH] simplified PromptForInputs --- cmd/project.go | 95 +++++++++++++++++++++++++++++--------------------- 1 file changed, 56 insertions(+), 39 deletions(-) diff --git a/cmd/project.go b/cmd/project.go index c2ed96098..6b59cef5b 100644 --- a/cmd/project.go +++ b/cmd/project.go @@ -179,18 +179,9 @@ func (po *ProjectOptions) PromptForInputs() error { var questions []*survey.Question - if po.Name == "" { - questions = append(questions, InputQuestion("Name", "The name of the project", "My Project", true)) - } else { - fmt.Println("Project Name: " + po.Name) - } + processProjectName(po.Name, &questions) - if po.BinaryName == "" { - var binaryNameComputed = computeBinaryName(po.Name) - questions = append(questions, InputQuestion("BinaryName", "The output binary name", binaryNameComputed, true)) - } else { - fmt.Println("Output binary Name: " + po.BinaryName) - } + processBinaryName(po.BinaryName, po.Name, &questions) err := processOutputDirectory(po.OutputDirectory, &questions) if err != nil { @@ -242,35 +233,10 @@ func (po *ProjectOptions) PromptForInputs() error { // Populate template details templateMetadata := templateDetails[po.Template].Metadata - if templateMetadata["frontenddir"] != nil { - po.FrontEnd = &frontend{} - po.FrontEnd.Dir = templateMetadata["frontenddir"].(string) - } - if templateMetadata["install"] != nil { - if po.FrontEnd == nil { - return fmt.Errorf("install set in template metadata but not frontenddir") - } - po.FrontEnd.Install = templateMetadata["install"].(string) - } - if templateMetadata["build"] != nil { - if po.FrontEnd == nil { - return fmt.Errorf("build set in template metadata but not frontenddir") - } - po.FrontEnd.Build = templateMetadata["build"].(string) - } - if templateMetadata["bridge"] != nil { - if po.FrontEnd == nil { - return fmt.Errorf("bridge set in template metadata but not frontenddir") - } - po.FrontEnd.Bridge = templateMetadata["bridge"].(string) - } - - if templateMetadata["serve"] != nil { - if po.FrontEnd == nil { - return fmt.Errorf("serve set in template metadata but not frontenddir") - } - po.FrontEnd.Serve = templateMetadata["serve"].(string) + err = processTemplateMetadata(templateMetadata, po) + if err != nil { + return err } return nil @@ -333,3 +299,54 @@ func processOutputDirectory(outputDirectory string, questions *[]*survey.Questio } return nil } + +func processProjectName(name string, questions *[]*survey.Question) { + if name == "" { + *questions = append(*questions, InputQuestion("Name", "The name of the project", "My Project", true)) + } else { + fmt.Println("Project Name: " + name) + } +} + +func processBinaryName(binaryName string, name string, questions *[]*survey.Question) { + if binaryName == "" { + var binaryNameComputed = computeBinaryName(name) + *questions = append(*questions, InputQuestion("BinaryName", "The output binary name", binaryNameComputed, true)) + } else { + fmt.Println("Output binary Name: " + binaryName) + } +} + +func processTemplateMetadata(templateMetadata map[string]interface{}, po *ProjectOptions) error { + if templateMetadata["frontenddir"] != nil { + po.FrontEnd = &frontend{} + po.FrontEnd.Dir = templateMetadata["frontenddir"].(string) + } + if templateMetadata["install"] != nil { + if po.FrontEnd == nil { + return fmt.Errorf("install set in template metadata but not frontenddir") + } + po.FrontEnd.Install = templateMetadata["install"].(string) + } + if templateMetadata["build"] != nil { + if po.FrontEnd == nil { + return fmt.Errorf("build set in template metadata but not frontenddir") + } + po.FrontEnd.Build = templateMetadata["build"].(string) + } + + if templateMetadata["bridge"] != nil { + if po.FrontEnd == nil { + return fmt.Errorf("bridge set in template metadata but not frontenddir") + } + po.FrontEnd.Bridge = templateMetadata["bridge"].(string) + } + + if templateMetadata["serve"] != nil { + if po.FrontEnd == nil { + return fmt.Errorf("serve set in template metadata but not frontenddir") + } + po.FrontEnd.Serve = templateMetadata["serve"].(string) + } + return nil +}