From 94e9447e1c099d3c30ca73f9c29f8f8d6179bf8b Mon Sep 17 00:00:00 2001 From: Lea Anthony Date: Tue, 19 Feb 2019 19:25:33 +1100 Subject: [PATCH 1/2] Removed prompt library requirements --- cmd/project.go | 107 +++++++++++++++++++++++-------------------------- cmd/prompt.go | 65 ++++++++++++++++++++++++++++++ 2 files changed, 115 insertions(+), 57 deletions(-) create mode 100644 cmd/prompt.go diff --git a/cmd/project.go b/cmd/project.go index 2734bcc58..9d4c9e843 100644 --- a/cmd/project.go +++ b/cmd/project.go @@ -6,8 +6,6 @@ import ( "io/ioutil" "path/filepath" "strings" - - "gopkg.in/AlecAivazis/survey.v1" ) type author struct { @@ -120,36 +118,36 @@ func (ph *ProjectHelper) NewProjectOptions() *ProjectOptions { return &result } -// SelectQuestion creates a new select type question for Survey -func SelectQuestion(name, message string, options []string, defaultValue string, required bool) *survey.Question { - result := survey.Question{ - Name: name, - Prompt: &survey.Select{ - Message: message, - Options: options, - Default: defaultValue, - }, - } - if required { - result.Validate = survey.Required - } - return &result -} +// // SelectQuestion creates a new select type question for Survey +// func SelectQuestion(name, message string, options []string, defaultValue string, required bool) *survey.Question { +// result := survey.Question{ +// Name: name, +// Prompt: &survey.Select{ +// Message: message, +// Options: options, +// Default: defaultValue, +// }, +// } +// if required { +// result.Validate = survey.Required +// } +// return &result +// } // InputQuestion creates a new input type question for Survey -func InputQuestion(name, message string, defaultValue string, required bool) *survey.Question { - result := survey.Question{ - Name: name, - Prompt: &survey.Input{ - Message: message + ":", - Default: defaultValue, - }, - } - if required { - result.Validate = survey.Required - } - return &result -} +// func InputQuestion(name, message string, defaultValue string, required bool) *survey.Question { +// result := survey.Question{ +// Name: name, +// Prompt: &survey.Input{ +// Message: message + ":", +// Default: defaultValue, +// }, +// } +// if required { +// result.Validate = survey.Required +// } +// return &result +// } // ProjectOptions holds all the options available for a project type ProjectOptions struct { @@ -177,13 +175,11 @@ func (po *ProjectOptions) Defaults() { // PromptForInputs asks the user to input project details func (po *ProjectOptions) PromptForInputs() error { - var questions []*survey.Question + processProjectName(po) - processProjectName(po.Name, &questions) + processBinaryName(po) - processBinaryName(po.BinaryName, po.Name, &questions) - - err := processOutputDirectory(po.OutputDirectory, &questions) + err := processOutputDirectory(po) if err != nil { return err } @@ -212,15 +208,12 @@ func (po *ProjectOptions) PromptForInputs() error { if po.Template != "" { if _, ok := templateDetails[po.Template]; !ok { po.log.Error("Template '%s' invalid.", po.Template) - questions = append(questions, SelectQuestion("Template", "Select template", templates, templates[0], true)) + templateSelected := PromptSelection("Select template", templates) + po.Template = templates[templateSelected] } } else { - questions = append(questions, SelectQuestion("Template", "Select template", templates, templates[0], true)) - } - - err = survey.Ask(questions, po) - if err != nil { - return err + templateSelected := PromptSelection("Select template", templates) + po.Template = templates[templateSelected] } // Setup NPM Project name @@ -281,10 +274,10 @@ func computeBinaryName(projectName string) string { return binaryNameComputed } -func processOutputDirectory(outputDirectory string, questions *[]*survey.Question) error { - - if outputDirectory != "" { - projectPath, err := filepath.Abs(outputDirectory) +func processOutputDirectory(po *ProjectOptions) error { + // po.OutputDirectory + if po.OutputDirectory != "" { + projectPath, err := filepath.Abs(po.OutputDirectory) if err != nil { return err } @@ -293,27 +286,27 @@ func processOutputDirectory(outputDirectory string, questions *[]*survey.Questio return fmt.Errorf("directory '%s' already exists", projectPath) } - fmt.Println("Project Directory: " + outputDirectory) + fmt.Println("Project Directory: " + po.OutputDirectory) } else { - *questions = append(*questions, InputQuestion("OutputDirectory", "Project directory name", "", true)) + po.OutputDirectory = PromptRequired("Project directory name") } return nil } -func processProjectName(name string, questions *[]*survey.Question) { - if name == "" { - *questions = append(*questions, InputQuestion("Name", "The name of the project", "My Project", true)) +func processProjectName(po *ProjectOptions) { + if po.Name == "" { + po.Name = Prompt("The name of the project", "My Project") } else { - fmt.Println("Project Name: " + name) + fmt.Println("Project Name: " + po.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)) +func processBinaryName(po *ProjectOptions) { + if po.BinaryName == "" { + var binaryNameComputed = computeBinaryName(po.Name) + po.BinaryName = Prompt("The output binary name", binaryNameComputed) } else { - fmt.Println("Output binary Name: " + binaryName) + fmt.Println("Output binary Name: " + po.BinaryName) } } diff --git a/cmd/prompt.go b/cmd/prompt.go new file mode 100644 index 000000000..057ccbe60 --- /dev/null +++ b/cmd/prompt.go @@ -0,0 +1,65 @@ +package cmd + +import ( + "fmt" + "strconv" +) + +// Prompt asks the user for a value +func Prompt(question string, defaultValue ...string) string { + var answer string + haveDefault := len(defaultValue) > 0 + + if haveDefault { + question = fmt.Sprintf("%s (%s)", question, defaultValue[0]) + } + fmt.Printf(question + ": ") + fmt.Scanln(&answer) + if haveDefault { + if len(answer) == 0 { + answer = defaultValue[0] + fmt.Println(" -> " + answer) + } + } + return answer +} + +// PromptRequired calls Prompt repeatedly until a value is given +func PromptRequired(question string, defaultValue ...string) string { + for { + result := Prompt(question, defaultValue...) + if result != "" { + return result + } + } +} + +// PromptSelection asks the user to choose an option +func PromptSelection(question string, options []string) int { + + fmt.Println(question + ":") + for index, option := range options { + fmt.Printf(" %d: %s\n", index+1, option) + } + + fmt.Println() + selectedValue := -1 + + for { + choice := Prompt("Please choose an option") + + // index + number, err := strconv.Atoi(choice) + if err == nil { + if number > 0 && number <= len(options) { + selectedValue = number - 1 + break + } else { + continue + } + } + + } + + return selectedValue +} From b18f04b30d0bb6b05b95572246aa155d713a5e9b Mon Sep 17 00:00:00 2001 From: Lea Anthony Date: Tue, 19 Feb 2019 19:37:45 +1100 Subject: [PATCH 2/2] removed survey --- cmd/project.go | 31 ------------------------------- cmd/prompt.go | 2 +- cmd/system.go | 42 +++++++++++++----------------------------- go.mod | 1 - go.sum | 3 +-- 5 files changed, 15 insertions(+), 64 deletions(-) diff --git a/cmd/project.go b/cmd/project.go index 9d4c9e843..8377c7268 100644 --- a/cmd/project.go +++ b/cmd/project.go @@ -118,37 +118,6 @@ func (ph *ProjectHelper) NewProjectOptions() *ProjectOptions { return &result } -// // SelectQuestion creates a new select type question for Survey -// func SelectQuestion(name, message string, options []string, defaultValue string, required bool) *survey.Question { -// result := survey.Question{ -// Name: name, -// Prompt: &survey.Select{ -// Message: message, -// Options: options, -// Default: defaultValue, -// }, -// } -// if required { -// result.Validate = survey.Required -// } -// return &result -// } - -// InputQuestion creates a new input type question for Survey -// func InputQuestion(name, message string, defaultValue string, required bool) *survey.Question { -// result := survey.Question{ -// Name: name, -// Prompt: &survey.Input{ -// Message: message + ":", -// Default: defaultValue, -// }, -// } -// if required { -// result.Validate = survey.Required -// } -// return &result -// } - // ProjectOptions holds all the options available for a project type ProjectOptions struct { Name string `json:"name"` diff --git a/cmd/prompt.go b/cmd/prompt.go index 057ccbe60..beb87b946 100644 --- a/cmd/prompt.go +++ b/cmd/prompt.go @@ -8,7 +8,7 @@ import ( // Prompt asks the user for a value func Prompt(question string, defaultValue ...string) string { var answer string - haveDefault := len(defaultValue) > 0 + haveDefault := len(defaultValue) > 0 && defaultValue[0] != "" if haveDefault { question = fmt.Sprintf("%s (%s)", question, defaultValue[0]) diff --git a/cmd/system.go b/cmd/system.go index 5b1fc5ce3..c9baa48e5 100644 --- a/cmd/system.go +++ b/cmd/system.go @@ -10,7 +10,6 @@ import ( "strconv" "time" - "gopkg.in/AlecAivazis/survey.v1" homedir "github.com/mitchellh/go-homedir" ) @@ -82,9 +81,6 @@ func (s *SystemHelper) BackupConfig() (string, error) { func (s *SystemHelper) setup() error { - // Answers. We all need them. - answers := &SystemConfig{} - // Try to load current values - ignore errors config, err := s.LoadConfig() defaultName := "" @@ -93,31 +89,10 @@ func (s *SystemHelper) setup() error { defaultName = config.Name defaultEmail = config.Email } - // Questions - var simpleQs = []*survey.Question{ - { - Name: "Name", - Prompt: &survey.Input{ - Message: "What is your name:", - Default: defaultName, - }, - Validate: survey.Required, - }, - { - Name: "Email", - Prompt: &survey.Input{ - Message: "What is your email address:", - Default: defaultEmail, - }, - Validate: survey.Required, - }, - } - // ask the questions - err = survey.Ask(simpleQs, answers) - if err != nil { - return err - } + systemConfig := make(map[string]string) + systemConfig["name"] = PromptRequired("What is your name", defaultName) + systemConfig["email"] = PromptRequired("What is your email address", defaultEmail) // Create the directory err = s.fs.MkDirs(s.wailsSystemDir) @@ -125,12 +100,21 @@ func (s *SystemHelper) setup() error { return err } + // Save + configData, err := json.Marshal(&systemConfig) + if err != nil { + return err + } + err = ioutil.WriteFile(s.wailsSystemConfig, configData, 0755) + if err != nil { + return err + } fmt.Println() s.log.White("Wails config saved to: " + s.wailsSystemConfig) s.log.White("Feel free to customise these settings.") fmt.Println() - return answers.Save(s.wailsSystemConfig) + return nil } const introText = ` diff --git a/go.mod b/go.mod index a3372d741..da4057648 100644 --- a/go.mod +++ b/go.mod @@ -16,5 +16,4 @@ require ( github.com/pkg/errors v0.8.1 // indirect github.com/sirupsen/logrus v1.3.0 github.com/wailsapp/webview v0.2.5 - gopkg.in/AlecAivazis/survey.v1 v1.8.2 ) diff --git a/go.sum b/go.sum index 2bf54ed8f..2c6ddc6af 100644 --- a/go.sum +++ b/go.sum @@ -362,6 +362,7 @@ github.com/wailsapp/webview v0.2.5 h1:/VacryPEUeMBb2VHHOjpoIze6ki8tW3qYX04MnI0b7 github.com/wailsapp/webview v0.2.5/go.mod h1:XO9HJbKWokDxUYTWQEBCYg95n/To1v7PxvanDNVf8hY= github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q= github.com/zserge/webview v0.0.0-20190123072648-16c93bcaeaeb/go.mod h1:a1CV8KR4Dd1eP2g+mEijGOp+HKczwdKHWyx0aPHKvo4= +golang.org/x/crypto v0.0.0-20180904163835-0709b304e793 h1:u+LnwYTOOW7Ukr/fppxEb1Nwz0AtPflrblfvUudpo+I= golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20180910181607-0e37d006457b/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20181001203147-e3636079e1a4/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= @@ -449,8 +450,6 @@ golang.org/x/tools v0.0.0-20190131142011-8dbcc66f33bb/go.mod h1:n7NCudcB/nEzxVGm golang.org/x/tools v0.0.0-20190206221403-44bcb96178d3 h1:M9mD7d4inzK0+YbTneZEs9Y+q1B1zLv8YxJDJ6hFgnY= golang.org/x/tools v0.0.0-20190206221403-44bcb96178d3/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= google.golang.org/appengine v1.2.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= -gopkg.in/AlecAivazis/survey.v1 v1.8.2 h1:168gU32e10Xm6NzttCL75XlCQF+nNh0VWuRU80u1GIw= -gopkg.in/AlecAivazis/survey.v1 v1.8.2/go.mod h1:iBNOmqKz/NUbZx3bA+4hAGLRC7fSK7tgtVDT4tB22XA= gopkg.in/airbrake/gobrake.v2 v2.0.9/go.mod h1:/h5ZAUhDkGaJfjzjKLSjv6zCL6O0LLBxU4K+aSYdM/U= gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc/go.mod h1:m7x9LTH6d71AHyAX77c9yqWCCa3UKHcVEj9y7hAtKDk= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=