github.com/versent/saml2aws@v2.17.0+incompatible/pkg/prompter/survey.go (about) 1 package prompter 2 3 import ( 4 "errors" 5 "fmt" 6 7 "github.com/AlecAivazis/survey" 8 ) 9 10 // CliPrompter used to prompt for cli input 11 type CliPrompter struct { 12 } 13 14 // NewCli builds a new cli prompter 15 func NewCli() *CliPrompter { 16 return &CliPrompter{} 17 } 18 19 // RequestSecurityCode request a security code to be entered by the user 20 func (cli *CliPrompter) RequestSecurityCode(pattern string) string { 21 token := "" 22 prompt := &survey.Input{ 23 Message: fmt.Sprintf("Security Token [%s]", pattern), 24 } 25 survey.AskOne(prompt, &token, survey.Required) 26 return token 27 } 28 29 // ChooseWithDefault given the choice return the option selected with a default 30 func (cli *CliPrompter) ChooseWithDefault(pr string, defaultValue string, options []string) (string, error) { 31 selected := "" 32 prompt := &survey.Select{ 33 Message: pr, 34 Options: options, 35 Default: defaultValue, 36 } 37 survey.AskOne(prompt, &selected, survey.Required) 38 39 // return the selected element index 40 for i, option := range options { 41 if selected == option { 42 return options[i], nil 43 } 44 } 45 return "", errors.New("bad input") 46 } 47 48 // Choose given the choice return the option selected 49 func (cli *CliPrompter) Choose(pr string, options []string) int { 50 selected := "" 51 prompt := &survey.Select{ 52 Message: pr, 53 Options: options, 54 } 55 survey.AskOne(prompt, &selected, survey.Required) 56 57 // return the selected element index 58 for i, option := range options { 59 if selected == option { 60 return i 61 } 62 } 63 return 0 64 } 65 66 // StringRequired prompt for string which is required 67 func (cli *CliPrompter) String(pr string, defaultValue string) string { 68 val := "" 69 prompt := &survey.Input{ 70 Message: pr, 71 Default: defaultValue, 72 } 73 survey.AskOne(prompt, &val, nil) 74 return val 75 } 76 77 // StringRequired prompt for string which is required 78 func (cli *CliPrompter) StringRequired(pr string) string { 79 val := "" 80 prompt := &survey.Input{ 81 Message: pr, 82 } 83 survey.AskOne(prompt, &val, survey.Required) 84 return val 85 } 86 87 // Password prompt for password which is required 88 func (cli *CliPrompter) Password(pr string) string { 89 val := "" 90 prompt := &survey.Password{ 91 Message: pr, 92 } 93 survey.AskOne(prompt, &val, nil) 94 return val 95 }