|
| 1 | +package confirm |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "strings" |
| 8 | + "sync/atomic" |
| 9 | + |
| 10 | + "github.com/manifoldco/promptui" |
| 11 | + "github.com/pkg/errors" |
| 12 | + "github.com/tarantool/sdvg/internal/generator/cli/render" |
| 13 | + "github.com/tarantool/sdvg/internal/generator/cli/utils" |
| 14 | +) |
| 15 | + |
| 16 | +// Confirm asks user a yes/no question. Returns true for “yes”. |
| 17 | +type Confirm func(ctx context.Context, question string) (bool, error) |
| 18 | + |
| 19 | +func BuildConfirmTTY(in io.Reader, out io.Writer) func(ctx context.Context, question string) (bool, error) { |
| 20 | + return func(ctx context.Context, question string) (bool, error) { |
| 21 | + fmt.Fprintln(out) |
| 22 | + |
| 23 | + prompt := promptui.Prompt{ |
| 24 | + Label: question + " [y/N]: ", |
| 25 | + Default: "y", |
| 26 | + Stdin: utils.DummyReadWriteCloser{Reader: in}, |
| 27 | + Stdout: utils.DummyReadWriteCloser{Writer: out}, |
| 28 | + } |
| 29 | + validate := func(s string) error { |
| 30 | + if len(s) == 1 && strings.Contains("YyNn", s) || prompt.Default != "" && len(s) == 0 { |
| 31 | + return nil |
| 32 | + } |
| 33 | + return errors.New("invalid input") |
| 34 | + } |
| 35 | + prompt.Validate = validate |
| 36 | + |
| 37 | + var ( |
| 38 | + input string |
| 39 | + err error |
| 40 | + promptFinished = make(chan struct{}) |
| 41 | + ) |
| 42 | + |
| 43 | + go func() { |
| 44 | + input, err = prompt.Run() // goroutine will block here until user input |
| 45 | + |
| 46 | + promptFinished <- struct{}{} |
| 47 | + }() |
| 48 | + |
| 49 | + select { |
| 50 | + case <-ctx.Done(): |
| 51 | + return false, ctx.Err() |
| 52 | + case <-promptFinished: |
| 53 | + } |
| 54 | + |
| 55 | + if err != nil { |
| 56 | + return false, errors.WithMessage(err, "confirm prompt failed") |
| 57 | + } |
| 58 | + |
| 59 | + return strings.Contains("Yy", input), nil |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +func BuildConfirmNoTTY(in render.Renderer, out io.Writer, isUpdatePaused *atomic.Bool) func(ctx context.Context, question string) (bool, error) { |
| 64 | + return func(ctx context.Context, question string) (bool, error) { |
| 65 | + // here we pause ProgressLogManager to stop sending progress messages |
| 66 | + isUpdatePaused.Store(true) |
| 67 | + defer isUpdatePaused.Store(false) |
| 68 | + |
| 69 | + for { |
| 70 | + fmt.Fprintf(out, "%s [y/N]: ", question) |
| 71 | + |
| 72 | + var ( |
| 73 | + input string |
| 74 | + err error |
| 75 | + inputReadFinished = make(chan struct{}) |
| 76 | + ) |
| 77 | + |
| 78 | + go func() { |
| 79 | + input, err = in.ReadLine() // goroutine will block here until user input |
| 80 | + |
| 81 | + inputReadFinished <- struct{}{} |
| 82 | + }() |
| 83 | + |
| 84 | + select { |
| 85 | + case <-ctx.Done(): |
| 86 | + return false, ctx.Err() |
| 87 | + case <-inputReadFinished: |
| 88 | + } |
| 89 | + |
| 90 | + if err != nil { |
| 91 | + return false, err |
| 92 | + } |
| 93 | + |
| 94 | + if !in.IsTerminal() { |
| 95 | + fmt.Fprintln(out, input) |
| 96 | + } |
| 97 | + |
| 98 | + switch strings.ToLower(strings.TrimSpace(input)) { |
| 99 | + case "y", "yes": |
| 100 | + return true, nil |
| 101 | + case "", "n", "no": |
| 102 | + return false, nil |
| 103 | + default: |
| 104 | + fmt.Fprintln(out, "Please enter y or n") |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | +} |
0 commit comments