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

Removed prompt library requirements

This commit is contained in:
Lea Anthony 2019-02-19 19:25:33 +11:00
parent 03c479c890
commit 94e9447e1c
No known key found for this signature in database
GPG Key ID: 33DAF7BB90A58405
2 changed files with 115 additions and 57 deletions

View File

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

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