5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-02 20:39:34 +08:00

Merge pull request #48 from wailsapp/move-to-new-prompt-library

Move to new prompt library
This commit is contained in:
Lea Anthony 2019-02-19 19:38:29 +11:00 committed by GitHub
commit a4b1f469e9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 101 additions and 92 deletions

View File

@ -6,8 +6,6 @@ import (
"io/ioutil"
"path/filepath"
"strings"
"gopkg.in/AlecAivazis/survey.v1"
)
type author struct {
@ -120,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"`
@ -177,13 +144,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 +177,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 +243,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 +255,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)
}
}

65
cmd/prompt.go Normal file
View File

@ -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 && 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
}

View File

@ -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 = `

1
go.mod
View File

@ -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
)

3
go.sum
View File

@ -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=