prompt.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. Copyright 2020 Docker, Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package prompt
  14. import (
  15. "github.com/AlecAivazis/survey/v2"
  16. )
  17. // UI - prompt user input
  18. type UI interface {
  19. Select(message string, options []string) (int, error)
  20. Input(message string, defaultValue string) (string, error)
  21. Confirm(message string, defaultValue bool) (bool, error)
  22. Password(message string) (string, error)
  23. }
  24. // User - aggregates prompt methods
  25. type User struct{}
  26. // Select - displays a list
  27. func (u User) Select(message string, options []string) (int, error) {
  28. qs := &survey.Select{
  29. Message: message,
  30. Options: options,
  31. }
  32. var selected int
  33. err := survey.AskOne(qs, &selected, nil)
  34. return selected, err
  35. }
  36. // Input text with default value
  37. func (u User) Input(message string, defaultValue string) (string, error) {
  38. qs := &survey.Input{
  39. Message: message,
  40. Default: defaultValue,
  41. }
  42. var s string
  43. err := survey.AskOne(qs, &s, nil)
  44. return s, err
  45. }
  46. // Confirm asks for yes or no input
  47. func (u User) Confirm(message string, defaultValue bool) (bool, error) {
  48. qs := &survey.Confirm{
  49. Message: message,
  50. Default: defaultValue,
  51. }
  52. var b bool
  53. err := survey.AskOne(qs, &b, nil)
  54. return b, err
  55. }
  56. // Password implemetns a text input with masked characters
  57. func (u User) Password(message string) (string, error) {
  58. qs := &survey.Password{
  59. Message: message,
  60. }
  61. var p string
  62. err := survey.AskOne(qs, &p, nil)
  63. return p, err
  64. }