2014-09-29 12:56:01 -05:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2014-09-29 15:12:06 -05:00
|
|
|
"bufio"
|
|
|
|
"bytes"
|
2019-03-07 14:07:13 -06:00
|
|
|
"context"
|
2014-09-29 12:56:01 -05:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
2014-09-29 15:12:06 -05:00
|
|
|
"strings"
|
2014-09-29 12:56:01 -05:00
|
|
|
"sync"
|
2019-04-29 08:15:26 -05:00
|
|
|
"sync/atomic"
|
2016-08-22 14:59:48 -05:00
|
|
|
"unicode"
|
2014-09-29 12:56:01 -05:00
|
|
|
|
2020-06-24 13:47:59 -05:00
|
|
|
"github.com/bgentry/speakeasy"
|
2021-05-17 14:46:19 -05:00
|
|
|
"github.com/hashicorp/terraform/internal/terraform"
|
2020-06-24 13:47:59 -05:00
|
|
|
"github.com/mattn/go-isatty"
|
2014-09-29 15:12:06 -05:00
|
|
|
"github.com/mitchellh/colorstring"
|
2014-09-29 12:56:01 -05:00
|
|
|
)
|
|
|
|
|
2014-09-29 13:24:16 -05:00
|
|
|
var defaultInputReader io.Reader
|
|
|
|
var defaultInputWriter io.Writer
|
2017-01-18 22:50:45 -06:00
|
|
|
var testInputResponse []string
|
2017-03-01 12:59:17 -06:00
|
|
|
var testInputResponseMap map[string]string
|
2014-09-29 13:24:16 -05:00
|
|
|
|
2014-09-29 12:56:01 -05:00
|
|
|
// UIInput is an implementation of terraform.UIInput that asks the CLI
|
|
|
|
// for input stdin.
|
|
|
|
type UIInput struct {
|
2014-09-29 15:12:06 -05:00
|
|
|
// Colorize will color the output.
|
|
|
|
Colorize *colorstring.Colorize
|
|
|
|
|
2014-09-29 12:56:01 -05:00
|
|
|
// Reader and Writer for IO. If these aren't set, they will default to
|
2019-04-29 08:15:26 -05:00
|
|
|
// Stdin and Stdout respectively.
|
2014-09-29 12:56:01 -05:00
|
|
|
Reader io.Reader
|
|
|
|
Writer io.Writer
|
|
|
|
|
2019-04-29 08:15:26 -05:00
|
|
|
listening int32
|
|
|
|
result chan string
|
2020-10-21 13:10:06 -05:00
|
|
|
err chan string
|
2019-04-29 08:15:26 -05:00
|
|
|
|
2014-09-29 12:56:01 -05:00
|
|
|
interrupted bool
|
|
|
|
l sync.Mutex
|
2014-09-29 15:12:06 -05:00
|
|
|
once sync.Once
|
2014-09-29 12:56:01 -05:00
|
|
|
}
|
|
|
|
|
2019-03-07 14:07:13 -06:00
|
|
|
func (i *UIInput) Input(ctx context.Context, opts *terraform.InputOpts) (string, error) {
|
2014-09-29 15:12:06 -05:00
|
|
|
i.once.Do(i.init)
|
|
|
|
|
2014-09-29 12:56:01 -05:00
|
|
|
r := i.Reader
|
|
|
|
w := i.Writer
|
2014-09-29 13:24:16 -05:00
|
|
|
if r == nil {
|
|
|
|
r = defaultInputReader
|
|
|
|
}
|
|
|
|
if w == nil {
|
|
|
|
w = defaultInputWriter
|
|
|
|
}
|
2014-09-29 12:56:01 -05:00
|
|
|
if r == nil {
|
|
|
|
r = os.Stdin
|
|
|
|
}
|
|
|
|
if w == nil {
|
|
|
|
w = os.Stdout
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure we only ask for input once at a time. Terraform
|
|
|
|
// should enforce this, but it doesn't hurt to verify.
|
|
|
|
i.l.Lock()
|
|
|
|
defer i.l.Unlock()
|
|
|
|
|
|
|
|
// If we're interrupted, then don't ask for input
|
|
|
|
if i.interrupted {
|
|
|
|
return "", errors.New("interrupted")
|
|
|
|
}
|
|
|
|
|
2017-03-01 12:59:17 -06:00
|
|
|
// If we have test results, return those. testInputResponse is the
|
|
|
|
// "old" way of doing it and we should remove that.
|
2017-01-18 22:50:45 -06:00
|
|
|
if testInputResponse != nil {
|
|
|
|
v := testInputResponse[0]
|
|
|
|
testInputResponse = testInputResponse[1:]
|
|
|
|
return v, nil
|
|
|
|
}
|
|
|
|
|
2017-03-01 12:59:17 -06:00
|
|
|
// testInputResponseMap is the new way for test responses, based on
|
|
|
|
// the query ID.
|
|
|
|
if testInputResponseMap != nil {
|
|
|
|
v, ok := testInputResponseMap[opts.Id]
|
|
|
|
if !ok {
|
|
|
|
return "", fmt.Errorf("unexpected input request in test: %s", opts.Id)
|
|
|
|
}
|
|
|
|
|
|
|
|
return v, nil
|
|
|
|
}
|
|
|
|
|
2017-01-18 22:50:45 -06:00
|
|
|
log.Printf("[DEBUG] command: asking for input: %q", opts.Query)
|
|
|
|
|
2014-09-29 12:56:01 -05:00
|
|
|
// Listen for interrupts so we can cancel the input ask
|
|
|
|
sigCh := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(sigCh, os.Interrupt)
|
|
|
|
defer signal.Stop(sigCh)
|
|
|
|
|
2014-09-29 15:12:06 -05:00
|
|
|
// Build the output format for asking
|
|
|
|
var buf bytes.Buffer
|
|
|
|
buf.WriteString("[reset]")
|
|
|
|
buf.WriteString(fmt.Sprintf("[bold]%s[reset]\n", opts.Query))
|
|
|
|
if opts.Description != "" {
|
|
|
|
s := bufio.NewScanner(strings.NewReader(opts.Description))
|
|
|
|
for s.Scan() {
|
|
|
|
buf.WriteString(fmt.Sprintf(" %s\n", s.Text()))
|
|
|
|
}
|
|
|
|
buf.WriteString("\n")
|
|
|
|
}
|
2014-09-29 15:51:16 -05:00
|
|
|
if opts.Default != "" {
|
|
|
|
buf.WriteString(" [bold]Default:[reset] ")
|
|
|
|
buf.WriteString(opts.Default)
|
2014-09-29 16:00:35 -05:00
|
|
|
buf.WriteString("\n")
|
2014-09-29 15:51:16 -05:00
|
|
|
}
|
2014-09-29 15:12:06 -05:00
|
|
|
buf.WriteString(" [bold]Enter a value:[reset] ")
|
|
|
|
|
2014-09-29 12:56:01 -05:00
|
|
|
// Ask the user for their input
|
2014-09-29 15:12:06 -05:00
|
|
|
if _, err := fmt.Fprint(w, i.Colorize.Color(buf.String())); err != nil {
|
2014-09-29 12:56:01 -05:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Listen for the input in a goroutine. This will allow us to
|
2019-04-29 08:15:26 -05:00
|
|
|
// interrupt this if we are interrupted (SIGINT).
|
2014-09-29 12:56:01 -05:00
|
|
|
go func() {
|
2019-04-29 08:15:26 -05:00
|
|
|
if !atomic.CompareAndSwapInt32(&i.listening, 0, 1) {
|
|
|
|
return // We are already listening for input.
|
|
|
|
}
|
|
|
|
defer atomic.CompareAndSwapInt32(&i.listening, 1, 0)
|
|
|
|
|
2020-06-24 13:47:59 -05:00
|
|
|
var line string
|
|
|
|
var err error
|
|
|
|
if opts.Secret && isatty.IsTerminal(os.Stdin.Fd()) {
|
|
|
|
line, err = speakeasy.Ask("")
|
|
|
|
} else {
|
|
|
|
buf := bufio.NewReader(r)
|
|
|
|
line, err = buf.ReadString('\n')
|
|
|
|
}
|
2016-08-22 14:59:48 -05:00
|
|
|
if err != nil {
|
2014-09-29 12:56:01 -05:00
|
|
|
log.Printf("[ERR] UIInput scan err: %s", err)
|
2020-10-21 13:10:06 -05:00
|
|
|
i.err <- string(err.Error())
|
|
|
|
} else {
|
|
|
|
i.result <- strings.TrimRightFunc(line, unicode.IsSpace)
|
2014-09-29 12:56:01 -05:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
2020-10-21 13:10:06 -05:00
|
|
|
case err := <-i.err:
|
|
|
|
return "", errors.New(err)
|
|
|
|
|
2019-04-29 08:15:26 -05:00
|
|
|
case line := <-i.result:
|
2014-09-29 15:30:28 -05:00
|
|
|
fmt.Fprint(w, "\n")
|
2014-09-29 15:51:16 -05:00
|
|
|
|
|
|
|
if line == "" {
|
|
|
|
line = opts.Default
|
|
|
|
}
|
|
|
|
|
2014-09-29 12:56:01 -05:00
|
|
|
return line, nil
|
2019-03-07 14:07:13 -06:00
|
|
|
case <-ctx.Done():
|
|
|
|
// Print a newline so that any further output starts properly
|
|
|
|
// on a new line.
|
|
|
|
fmt.Fprintln(w)
|
|
|
|
|
|
|
|
return "", ctx.Err()
|
2014-09-29 12:56:01 -05:00
|
|
|
case <-sigCh:
|
|
|
|
// Print a newline so that any further output starts properly
|
|
|
|
// on a new line.
|
|
|
|
fmt.Fprintln(w)
|
|
|
|
|
|
|
|
// Mark that we were interrupted so future Ask calls fail.
|
|
|
|
i.interrupted = true
|
|
|
|
|
|
|
|
return "", errors.New("interrupted")
|
|
|
|
}
|
|
|
|
}
|
2014-09-29 15:12:06 -05:00
|
|
|
|
|
|
|
func (i *UIInput) init() {
|
2019-04-29 08:15:26 -05:00
|
|
|
i.result = make(chan string)
|
2020-10-21 13:10:06 -05:00
|
|
|
i.err = make(chan string)
|
2019-04-29 08:15:26 -05:00
|
|
|
|
2014-09-29 15:12:06 -05:00
|
|
|
if i.Colorize == nil {
|
|
|
|
i.Colorize = &colorstring.Colorize{
|
|
|
|
Colors: colorstring.DefaultColors,
|
|
|
|
Disable: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|