command: make the UI a look nicer
This commit is contained in:
parent
9bb26f7695
commit
901c1448b4
|
@ -133,7 +133,9 @@ func (m *Meta) contextOpts() *terraform.ContextOpts {
|
|||
vs[k] = v
|
||||
}
|
||||
opts.Variables = vs
|
||||
opts.UIInput = new(UIInput)
|
||||
opts.UIInput = &UIInput{
|
||||
Colorize: m.Colorize(),
|
||||
}
|
||||
|
||||
return &opts
|
||||
}
|
||||
|
|
|
@ -1,15 +1,19 @@
|
|||
package command
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"os/signal"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/hashicorp/terraform/terraform"
|
||||
"github.com/mitchellh/colorstring"
|
||||
)
|
||||
|
||||
var defaultInputReader io.Reader
|
||||
|
@ -18,6 +22,9 @@ var defaultInputWriter io.Writer
|
|||
// UIInput is an implementation of terraform.UIInput that asks the CLI
|
||||
// for input stdin.
|
||||
type UIInput struct {
|
||||
// Colorize will color the output.
|
||||
Colorize *colorstring.Colorize
|
||||
|
||||
// Reader and Writer for IO. If these aren't set, they will default to
|
||||
// Stdout and Stderr respectively.
|
||||
Reader io.Reader
|
||||
|
@ -25,9 +32,12 @@ type UIInput struct {
|
|||
|
||||
interrupted bool
|
||||
l sync.Mutex
|
||||
once sync.Once
|
||||
}
|
||||
|
||||
func (i *UIInput) Input(opts *terraform.InputOpts) (string, error) {
|
||||
i.once.Do(i.init)
|
||||
|
||||
r := i.Reader
|
||||
w := i.Writer
|
||||
if r == nil {
|
||||
|
@ -58,8 +68,21 @@ func (i *UIInput) Input(opts *terraform.InputOpts) (string, error) {
|
|||
signal.Notify(sigCh, os.Interrupt)
|
||||
defer signal.Stop(sigCh)
|
||||
|
||||
// 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")
|
||||
}
|
||||
buf.WriteString(" [bold]Enter a value:[reset] ")
|
||||
|
||||
// Ask the user for their input
|
||||
if _, err := fmt.Fprint(w, opts.Query); err != nil {
|
||||
if _, err := fmt.Fprint(w, i.Colorize.Color(buf.String())); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
|
@ -89,3 +112,12 @@ func (i *UIInput) Input(opts *terraform.InputOpts) (string, error) {
|
|||
return "", errors.New("interrupted")
|
||||
}
|
||||
}
|
||||
|
||||
func (i *UIInput) init() {
|
||||
if i.Colorize == nil {
|
||||
i.Colorize = &colorstring.Colorize{
|
||||
Colors: colorstring.DefaultColors,
|
||||
Disable: true,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -616,7 +616,7 @@ func (m schemaMap) inputString(
|
|||
schema *Schema) (interface{}, error) {
|
||||
result, err := input.Input(&terraform.InputOpts{
|
||||
Id: k,
|
||||
Query: fmt.Sprintf("%s: ", k),
|
||||
Query: k,
|
||||
})
|
||||
|
||||
return result, err
|
||||
|
|
|
@ -167,8 +167,7 @@ func (c *Context) Input() error {
|
|||
var err error
|
||||
value, err = c.uiInput.Input(&InputOpts{
|
||||
Id: fmt.Sprintf("var.%s", n),
|
||||
Query: fmt.Sprintf(
|
||||
"Variable '%s': ", n),
|
||||
Query: fmt.Sprintf("var.%s", n),
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf(
|
||||
|
|
|
@ -15,4 +15,9 @@ type InputOpts struct {
|
|||
|
||||
// Query is a human-friendly question for inputting this value.
|
||||
Query string
|
||||
|
||||
// Description is a description about what this option is. Be wary
|
||||
// that this will probably be in a terminal so split lines as you see
|
||||
// necessary.
|
||||
Description string
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue