prompt.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. Copyright 2020 Docker Compose CLI authors
  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. "fmt"
  16. "io"
  17. "github.com/AlecAivazis/survey/v2"
  18. "github.com/docker/cli/cli/streams"
  19. "github.com/docker/compose/v2/pkg/utils"
  20. )
  21. //go:generate mockgen -destination=./prompt_mock.go -self_package "github.com/docker/compose/v2/pkg/prompt" -package=prompt . UI
  22. // UI - prompt user input
  23. type UI interface {
  24. Confirm(message string, defaultValue bool) (bool, error)
  25. }
  26. func NewPrompt(stdin *streams.In, stdout *streams.Out) UI {
  27. if stdin.IsTerminal() {
  28. return User{stdin: streamsFileReader{stdin}, stdout: streamsFileWriter{stdout}}
  29. }
  30. return Pipe{stdin: stdin, stdout: stdout}
  31. }
  32. // User - in a terminal
  33. type User struct {
  34. stdout streamsFileWriter
  35. stdin streamsFileReader
  36. }
  37. // adapt streams.Out to terminal.FileWriter
  38. type streamsFileWriter struct {
  39. stream *streams.Out
  40. }
  41. func (s streamsFileWriter) Write(p []byte) (n int, err error) {
  42. return s.stream.Write(p)
  43. }
  44. func (s streamsFileWriter) Fd() uintptr {
  45. return s.stream.FD()
  46. }
  47. // adapt streams.In to terminal.FileReader
  48. type streamsFileReader struct {
  49. stream *streams.In
  50. }
  51. func (s streamsFileReader) Read(p []byte) (n int, err error) {
  52. return s.stream.Read(p)
  53. }
  54. func (s streamsFileReader) Fd() uintptr {
  55. return s.stream.FD()
  56. }
  57. // Confirm asks for yes or no input
  58. func (u User) Confirm(message string, defaultValue bool) (bool, error) {
  59. qs := &survey.Confirm{
  60. Message: message,
  61. Default: defaultValue,
  62. }
  63. var b bool
  64. err := survey.AskOne(qs, &b, func(options *survey.AskOptions) error {
  65. options.Stdio.In = u.stdin
  66. options.Stdio.Out = u.stdout
  67. return nil
  68. })
  69. return b, err
  70. }
  71. // Pipe - aggregates prompt methods
  72. type Pipe struct {
  73. stdout io.Writer
  74. stdin io.Reader
  75. }
  76. // Confirm asks for yes or no input
  77. func (u Pipe) Confirm(message string, defaultValue bool) (bool, error) {
  78. fmt.Fprint(u.stdout, message)
  79. var answer string
  80. fmt.Scanln(&answer)
  81. return utils.StringToBool(answer), nil
  82. }