-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.go
94 lines (82 loc) · 2.03 KB
/
setup.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package setup
import (
"errors"
"fmt"
"github.com/AlecAivazis/survey/v2"
"github.com/guionardo/gs-dev/app/dev"
"github.com/spf13/cobra"
)
var (
rootCmd *cobra.Command
)
func SetRootCmd(root *cobra.Command) {
rootCmd = root
}
// Parse setup from commands
func Setup(cmd *cobra.Command) {
mainCommands := make([]string, 1, 10)
for _, command := range rootCmd.Commands() {
if _, ok := dev.InteractiveCommands[command.Use]; ok {
mainCommands = append(mainCommands, command.Use)
}
}
var questions = []*survey.Question{
{
Name: "main_command",
Prompt: &survey.Select{
Message: "Choose a command:",
Options: mainCommands,
},
}}
answers := struct {
MainCommand string `survey:"main_command"`
}{}
var err error
if err = survey.Ask(questions, &answers); err == nil {
if len(answers.MainCommand) == 0 {
err = errors.New("command is required")
} else {
function := dev.InteractiveCommands[answers.MainCommand]
err = function(cmd)
}
}
if err != nil {
cmd.PrintErr(err)
return
}
// // the questions to ask
// var qs = []*survey.Question{
// {
// Name: "name",
// Prompt: &survey.Input{Message: "What is your name?"},
// Validate: survey.Required,
// Transform: survey.Title,
// },
// {
// Name: "color",
// Prompt: &survey.Select{
// Message: "Choose a color:",
// Options: []string{"red", "blue", "green"},
// Default: "red",
// },
// },
// {
// Name: "age",
// Prompt: &survey.Input{Message: "How old are you?"},
// },
// }
// // the answers will be written to this struct
// answers := struct {
// Name string // survey will match the question and field names
// FavoriteColor string `survey:"color"` // or you can tag fields to match a specific name
// Age int // if the types don't match, survey will convert it
// }{}
// perform the questions
// err := survey.Ask(qs, &answers)
// if err != nil {
// fmt.Println(err.Error())
// return
// }
fmt.Printf("chose %s.", answers.MainCommand)
fmt.Scanln()
}