2013-03-24 17:03:53 -04:00
|
|
|
package packer
|
|
|
|
|
2013-05-21 02:43:37 -04:00
|
|
|
import (
|
2017-01-29 09:10:14 -05:00
|
|
|
"bufio"
|
2013-07-02 15:28:25 -04:00
|
|
|
"bytes"
|
2013-06-15 21:24:38 -04:00
|
|
|
"errors"
|
2013-05-21 02:43:37 -04:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2013-05-21 14:40:07 -04:00
|
|
|
"log"
|
2013-06-14 18:59:54 -04:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
2013-07-31 17:43:34 -04:00
|
|
|
"runtime"
|
2013-07-02 15:28:25 -04:00
|
|
|
"strings"
|
2013-06-14 18:17:03 -04:00
|
|
|
"sync"
|
2014-02-21 20:43:45 -05:00
|
|
|
"syscall"
|
2013-08-11 22:05:07 -04:00
|
|
|
"time"
|
2013-07-09 14:21:42 -04:00
|
|
|
"unicode"
|
2013-05-21 02:43:37 -04:00
|
|
|
)
|
2013-03-24 17:03:53 -04:00
|
|
|
|
2013-06-03 16:35:43 -04:00
|
|
|
type UiColor uint
|
|
|
|
|
|
|
|
const (
|
|
|
|
UiColorRed UiColor = 31
|
|
|
|
UiColorGreen = 32
|
|
|
|
UiColorYellow = 33
|
|
|
|
UiColorBlue = 34
|
|
|
|
UiColorMagenta = 35
|
|
|
|
UiColorCyan = 36
|
|
|
|
)
|
|
|
|
|
2013-03-24 17:03:53 -04:00
|
|
|
// The Ui interface handles all communication for Packer with the outside
|
|
|
|
// world. This sort of control allows us to strictly control how output
|
|
|
|
// is formatted and various levels of output.
|
|
|
|
type Ui interface {
|
2013-06-15 21:24:38 -04:00
|
|
|
Ask(string) (string, error)
|
2013-05-27 18:12:48 -04:00
|
|
|
Say(string)
|
2013-06-03 14:30:38 -04:00
|
|
|
Message(string)
|
2013-05-27 18:12:48 -04:00
|
|
|
Error(string)
|
2013-08-11 21:16:00 -04:00
|
|
|
Machine(string, ...string)
|
2018-09-04 11:57:21 -04:00
|
|
|
ProgressBar() ProgressBar
|
2013-03-24 17:03:53 -04:00
|
|
|
}
|
|
|
|
|
2018-09-05 11:15:54 -04:00
|
|
|
type NoopUi struct{}
|
|
|
|
|
|
|
|
var _ Ui = new(NoopUi)
|
|
|
|
|
|
|
|
func (*NoopUi) Ask(string) (string, error) { return "", errors.New("this is a noop ui") }
|
|
|
|
func (*NoopUi) Say(string) { return }
|
|
|
|
func (*NoopUi) Message(string) { return }
|
|
|
|
func (*NoopUi) Error(string) { return }
|
|
|
|
func (*NoopUi) Machine(string, ...string) { return }
|
|
|
|
func (*NoopUi) ProgressBar() ProgressBar { return new(NoopProgressBar) }
|
|
|
|
|
2013-06-03 16:35:43 -04:00
|
|
|
// ColoredUi is a UI that is colored using terminal colors.
|
|
|
|
type ColoredUi struct {
|
2013-06-12 13:41:58 -04:00
|
|
|
Color UiColor
|
|
|
|
ErrorColor UiColor
|
|
|
|
Ui Ui
|
2013-06-03 16:35:43 -04:00
|
|
|
}
|
|
|
|
|
2018-09-04 11:57:21 -04:00
|
|
|
var _ Ui = new(ColoredUi)
|
|
|
|
|
2017-03-29 15:44:42 -04:00
|
|
|
// TargetedUI is a UI that wraps another UI implementation and modifies
|
2013-08-11 21:31:28 -04:00
|
|
|
// the output to indicate a specific target. Specifically, all Say output
|
|
|
|
// is prefixed with the target name. Message output is not prefixed but
|
|
|
|
// is offset by the length of the target so that output is lined up properly
|
|
|
|
// with Say output. Machine-readable output has the proper target set.
|
2017-03-29 15:44:42 -04:00
|
|
|
type TargetedUI struct {
|
2013-08-11 21:31:28 -04:00
|
|
|
Target string
|
|
|
|
Ui Ui
|
2013-05-21 16:20:51 -04:00
|
|
|
}
|
|
|
|
|
2018-09-04 11:57:21 -04:00
|
|
|
var _ Ui = new(TargetedUI)
|
|
|
|
|
2013-08-11 21:20:27 -04:00
|
|
|
// The BasicUI is a UI that reads and writes from a standard Go reader
|
2013-08-11 21:38:24 -04:00
|
|
|
// and writer. It is safe to be called from multiple goroutines. Machine
|
|
|
|
// readable output is simply logged for this UI.
|
2013-08-11 21:20:27 -04:00
|
|
|
type BasicUi struct {
|
2013-06-15 21:25:34 -04:00
|
|
|
Reader io.Reader
|
|
|
|
Writer io.Writer
|
2014-02-21 21:29:15 -05:00
|
|
|
ErrorWriter io.Writer
|
2013-06-15 21:25:34 -04:00
|
|
|
l sync.Mutex
|
|
|
|
interrupted bool
|
2017-01-29 09:10:14 -05:00
|
|
|
scanner *bufio.Scanner
|
2018-09-05 11:15:54 -04:00
|
|
|
StackableProgressBar
|
2013-06-14 18:17:03 -04:00
|
|
|
}
|
|
|
|
|
2018-09-04 11:57:21 -04:00
|
|
|
var _ Ui = new(BasicUi)
|
|
|
|
|
|
|
|
func (bu *BasicUi) ProgressBar() ProgressBar {
|
2018-09-05 11:15:54 -04:00
|
|
|
return &bu.StackableProgressBar
|
2018-09-04 11:57:21 -04:00
|
|
|
}
|
|
|
|
|
2013-08-11 22:05:07 -04:00
|
|
|
// MachineReadableUi is a UI that only outputs machine-readable output
|
|
|
|
// to the given Writer.
|
|
|
|
type MachineReadableUi struct {
|
|
|
|
Writer io.Writer
|
|
|
|
}
|
|
|
|
|
2018-09-04 11:57:21 -04:00
|
|
|
var _ Ui = new(MachineReadableUi)
|
|
|
|
|
2013-06-15 21:24:38 -04:00
|
|
|
func (u *ColoredUi) Ask(query string) (string, error) {
|
2013-06-14 18:17:03 -04:00
|
|
|
return u.Ui.Ask(u.colorize(query, u.Color, true))
|
2013-03-24 17:03:53 -04:00
|
|
|
}
|
|
|
|
|
2013-06-03 16:35:43 -04:00
|
|
|
func (u *ColoredUi) Say(message string) {
|
2013-06-12 13:41:58 -04:00
|
|
|
u.Ui.Say(u.colorize(message, u.Color, true))
|
2013-06-03 16:35:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (u *ColoredUi) Message(message string) {
|
2013-06-12 13:41:58 -04:00
|
|
|
u.Ui.Message(u.colorize(message, u.Color, false))
|
2013-06-03 16:35:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (u *ColoredUi) Error(message string) {
|
2013-06-12 13:41:58 -04:00
|
|
|
color := u.ErrorColor
|
|
|
|
if color == 0 {
|
|
|
|
color = UiColorRed
|
|
|
|
}
|
|
|
|
|
|
|
|
u.Ui.Error(u.colorize(message, color, true))
|
2013-06-03 16:35:43 -04:00
|
|
|
}
|
|
|
|
|
2013-08-11 21:16:00 -04:00
|
|
|
func (u *ColoredUi) Machine(t string, args ...string) {
|
|
|
|
// Don't colorize machine-readable output
|
|
|
|
u.Ui.Machine(t, args...)
|
|
|
|
}
|
|
|
|
|
2018-09-04 11:57:21 -04:00
|
|
|
func (u *ColoredUi) ProgressBar() ProgressBar {
|
|
|
|
return u.Ui.ProgressBar() //TODO(adrien): color me
|
|
|
|
}
|
|
|
|
|
2013-06-12 13:41:58 -04:00
|
|
|
func (u *ColoredUi) colorize(message string, color UiColor, bold bool) string {
|
2013-07-31 17:06:01 -04:00
|
|
|
if !u.supportsColors() {
|
|
|
|
return message
|
|
|
|
}
|
|
|
|
|
2013-06-03 16:49:59 -04:00
|
|
|
attr := 0
|
|
|
|
if bold {
|
|
|
|
attr = 1
|
|
|
|
}
|
|
|
|
|
2013-11-20 00:31:54 -05:00
|
|
|
return fmt.Sprintf("\033[%d;%dm%s\033[0m", attr, color, message)
|
2013-06-03 16:35:43 -04:00
|
|
|
}
|
|
|
|
|
2013-07-31 17:06:01 -04:00
|
|
|
func (u *ColoredUi) supportsColors() bool {
|
2013-12-16 17:10:28 -05:00
|
|
|
// Never use colors if we have this environmental variable
|
|
|
|
if os.Getenv("PACKER_NO_COLOR") != "" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2013-07-31 17:06:01 -04:00
|
|
|
// For now, on non-Windows machine, just assume it does
|
|
|
|
if runtime.GOOS != "windows" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// On Windows, if we appear to be in Cygwin, then it does
|
|
|
|
cygwin := os.Getenv("CYGWIN") != "" ||
|
|
|
|
os.Getenv("OSTYPE") == "cygwin" ||
|
|
|
|
os.Getenv("TERM") == "cygwin"
|
|
|
|
|
|
|
|
return cygwin
|
|
|
|
}
|
|
|
|
|
2017-03-29 15:44:42 -04:00
|
|
|
func (u *TargetedUI) Ask(query string) (string, error) {
|
2013-08-11 21:31:28 -04:00
|
|
|
return u.Ui.Ask(u.prefixLines(true, query))
|
2013-06-14 18:17:03 -04:00
|
|
|
}
|
|
|
|
|
2017-03-29 15:44:42 -04:00
|
|
|
func (u *TargetedUI) Say(message string) {
|
2013-08-11 21:31:28 -04:00
|
|
|
u.Ui.Say(u.prefixLines(true, message))
|
2013-06-03 14:30:38 -04:00
|
|
|
}
|
|
|
|
|
2017-03-29 15:44:42 -04:00
|
|
|
func (u *TargetedUI) Message(message string) {
|
2013-08-11 21:31:28 -04:00
|
|
|
u.Ui.Message(u.prefixLines(false, message))
|
2013-05-21 16:20:51 -04:00
|
|
|
}
|
|
|
|
|
2017-03-29 15:44:42 -04:00
|
|
|
func (u *TargetedUI) Error(message string) {
|
2013-08-11 21:31:28 -04:00
|
|
|
u.Ui.Error(u.prefixLines(true, message))
|
2013-07-02 15:28:25 -04:00
|
|
|
}
|
|
|
|
|
2017-03-29 15:44:42 -04:00
|
|
|
func (u *TargetedUI) Machine(t string, args ...string) {
|
2013-08-11 22:08:08 -04:00
|
|
|
// Prefix in the target, then pass through
|
|
|
|
u.Ui.Machine(fmt.Sprintf("%s,%s", u.Target, t), args...)
|
2013-08-11 21:16:00 -04:00
|
|
|
}
|
|
|
|
|
2018-09-04 11:57:21 -04:00
|
|
|
func (u *TargetedUI) ProgressBar() ProgressBar {
|
|
|
|
return u.Ui.ProgressBar()
|
|
|
|
}
|
|
|
|
|
2017-03-29 15:44:42 -04:00
|
|
|
func (u *TargetedUI) prefixLines(arrow bool, message string) string {
|
2013-08-11 21:31:28 -04:00
|
|
|
arrowText := "==>"
|
|
|
|
if !arrow {
|
|
|
|
arrowText = strings.Repeat(" ", len(arrowText))
|
|
|
|
}
|
|
|
|
|
2013-07-02 15:28:25 -04:00
|
|
|
var result bytes.Buffer
|
|
|
|
|
|
|
|
for _, line := range strings.Split(message, "\n") {
|
2013-08-11 21:31:28 -04:00
|
|
|
result.WriteString(fmt.Sprintf("%s %s: %s\n", arrowText, u.Target, line))
|
2013-07-02 15:28:25 -04:00
|
|
|
}
|
|
|
|
|
2013-07-09 14:21:42 -04:00
|
|
|
return strings.TrimRightFunc(result.String(), unicode.IsSpace)
|
2013-05-21 16:20:51 -04:00
|
|
|
}
|
|
|
|
|
2013-08-11 21:20:27 -04:00
|
|
|
func (rw *BasicUi) Ask(query string) (string, error) {
|
2013-06-14 18:17:03 -04:00
|
|
|
rw.l.Lock()
|
|
|
|
defer rw.l.Unlock()
|
|
|
|
|
2013-06-15 21:25:34 -04:00
|
|
|
if rw.interrupted {
|
|
|
|
return "", errors.New("interrupted")
|
|
|
|
}
|
|
|
|
|
2017-01-29 09:10:14 -05:00
|
|
|
if rw.scanner == nil {
|
|
|
|
rw.scanner = bufio.NewScanner(rw.Reader)
|
|
|
|
}
|
2013-06-14 18:59:54 -04:00
|
|
|
sigCh := make(chan os.Signal, 1)
|
2017-09-08 14:31:19 -04:00
|
|
|
signal.Notify(sigCh, os.Interrupt, syscall.SIGTERM)
|
2013-06-14 18:59:54 -04:00
|
|
|
defer signal.Stop(sigCh)
|
|
|
|
|
2013-06-14 18:17:03 -04:00
|
|
|
log.Printf("ui: ask: %s", query)
|
|
|
|
if query != "" {
|
|
|
|
if _, err := fmt.Fprint(rw.Writer, query+" "); err != nil {
|
2013-06-15 21:24:38 -04:00
|
|
|
return "", err
|
2013-06-14 18:17:03 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-14 18:59:54 -04:00
|
|
|
result := make(chan string, 1)
|
|
|
|
go func() {
|
|
|
|
var line string
|
2017-01-29 09:10:14 -05:00
|
|
|
if rw.scanner.Scan() {
|
|
|
|
line = rw.scanner.Text()
|
|
|
|
}
|
|
|
|
if err := rw.scanner.Err(); err != nil {
|
2013-06-14 18:59:54 -04:00
|
|
|
log.Printf("ui: scan err: %s", err)
|
2017-01-29 09:10:14 -05:00
|
|
|
return
|
2013-06-14 18:59:54 -04:00
|
|
|
}
|
|
|
|
result <- line
|
|
|
|
}()
|
|
|
|
|
2013-06-15 21:24:38 -04:00
|
|
|
select {
|
|
|
|
case line := <-result:
|
|
|
|
return line, nil
|
|
|
|
case <-sigCh:
|
2013-06-17 14:40:57 -04:00
|
|
|
// Print a newline so that any further output starts properly
|
|
|
|
// on a new line.
|
|
|
|
fmt.Fprintln(rw.Writer)
|
|
|
|
|
|
|
|
// Mark that we were interrupted so future Ask calls fail.
|
2013-06-15 21:25:34 -04:00
|
|
|
rw.interrupted = true
|
2013-06-17 14:40:57 -04:00
|
|
|
|
2013-06-15 21:24:38 -04:00
|
|
|
return "", errors.New("interrupted")
|
2013-06-14 18:59:54 -04:00
|
|
|
}
|
2013-06-14 18:17:03 -04:00
|
|
|
}
|
|
|
|
|
2013-08-11 21:20:27 -04:00
|
|
|
func (rw *BasicUi) Say(message string) {
|
2013-06-14 18:17:03 -04:00
|
|
|
rw.l.Lock()
|
|
|
|
defer rw.l.Unlock()
|
|
|
|
|
2013-05-27 18:12:48 -04:00
|
|
|
log.Printf("ui: %s", message)
|
|
|
|
_, err := fmt.Fprint(rw.Writer, message+"\n")
|
2013-05-08 20:09:10 -04:00
|
|
|
if err != nil {
|
2013-10-23 23:32:20 -04:00
|
|
|
log.Printf("[ERR] Failed to write to UI: %s", err)
|
2013-05-08 20:09:10 -04:00
|
|
|
}
|
2013-03-24 17:03:53 -04:00
|
|
|
}
|
2013-05-08 18:12:48 -04:00
|
|
|
|
2013-08-11 21:20:27 -04:00
|
|
|
func (rw *BasicUi) Message(message string) {
|
2013-06-14 18:17:03 -04:00
|
|
|
rw.l.Lock()
|
|
|
|
defer rw.l.Unlock()
|
|
|
|
|
2013-06-03 14:30:38 -04:00
|
|
|
log.Printf("ui: %s", message)
|
2013-06-24 02:06:59 -04:00
|
|
|
_, err := fmt.Fprint(rw.Writer, message+"\n")
|
2013-06-03 14:30:38 -04:00
|
|
|
if err != nil {
|
2013-10-23 23:32:20 -04:00
|
|
|
log.Printf("[ERR] Failed to write to UI: %s", err)
|
2013-06-03 14:30:38 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-11 21:20:27 -04:00
|
|
|
func (rw *BasicUi) Error(message string) {
|
2013-06-14 18:17:03 -04:00
|
|
|
rw.l.Lock()
|
|
|
|
defer rw.l.Unlock()
|
|
|
|
|
2014-02-21 21:29:15 -05:00
|
|
|
writer := rw.ErrorWriter
|
|
|
|
if writer == nil {
|
|
|
|
writer = rw.Writer
|
|
|
|
}
|
|
|
|
|
2013-05-27 18:12:48 -04:00
|
|
|
log.Printf("ui error: %s", message)
|
2014-02-21 21:29:15 -05:00
|
|
|
_, err := fmt.Fprint(writer, message+"\n")
|
2013-05-08 20:09:10 -04:00
|
|
|
if err != nil {
|
2013-10-23 23:32:20 -04:00
|
|
|
log.Printf("[ERR] Failed to write to UI: %s", err)
|
2013-05-08 20:09:10 -04:00
|
|
|
}
|
2013-05-08 18:12:48 -04:00
|
|
|
}
|
2013-08-11 21:16:00 -04:00
|
|
|
|
2013-08-11 21:20:27 -04:00
|
|
|
func (rw *BasicUi) Machine(t string, args ...string) {
|
2013-08-11 21:38:24 -04:00
|
|
|
log.Printf("machine readable: %s %#v", t, args)
|
2013-08-11 21:16:00 -04:00
|
|
|
}
|
2013-08-11 22:05:07 -04:00
|
|
|
|
|
|
|
func (u *MachineReadableUi) Ask(query string) (string, error) {
|
|
|
|
return "", errors.New("machine-readable UI can't ask")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *MachineReadableUi) Say(message string) {
|
|
|
|
u.Machine("ui", "say", message)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *MachineReadableUi) Message(message string) {
|
|
|
|
u.Machine("ui", "message", message)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *MachineReadableUi) Error(message string) {
|
|
|
|
u.Machine("ui", "error", message)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *MachineReadableUi) Machine(category string, args ...string) {
|
|
|
|
now := time.Now().UTC()
|
|
|
|
|
|
|
|
// Determine if we have a target, and set it
|
|
|
|
target := ""
|
|
|
|
commaIdx := strings.Index(category, ",")
|
|
|
|
if commaIdx > -1 {
|
|
|
|
target = category[0:commaIdx]
|
|
|
|
category = category[commaIdx+1:]
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prepare the args
|
|
|
|
for i, v := range args {
|
|
|
|
args[i] = strings.Replace(v, ",", "%!(PACKER_COMMA)", -1)
|
2013-08-12 02:19:13 -04:00
|
|
|
args[i] = strings.Replace(args[i], "\r", "\\r", -1)
|
2013-08-12 02:18:14 -04:00
|
|
|
args[i] = strings.Replace(args[i], "\n", "\\n", -1)
|
2013-08-11 22:05:07 -04:00
|
|
|
}
|
|
|
|
argsString := strings.Join(args, ",")
|
|
|
|
|
2013-08-12 02:14:42 -04:00
|
|
|
_, err := fmt.Fprintf(u.Writer, "%d,%s,%s,%s\n", now.Unix(), target, category, argsString)
|
2013-08-11 22:05:07 -04:00
|
|
|
if err != nil {
|
2014-09-04 00:31:34 -04:00
|
|
|
if err == syscall.EPIPE || strings.Contains(err.Error(), "broken pipe") {
|
2014-02-21 20:43:45 -05:00
|
|
|
// Ignore epipe errors because that just means that the file
|
|
|
|
// is probably closed or going to /dev/null or something.
|
|
|
|
} else {
|
|
|
|
panic(err)
|
|
|
|
}
|
2013-08-11 22:05:07 -04:00
|
|
|
}
|
|
|
|
}
|
2018-09-04 11:57:21 -04:00
|
|
|
|
|
|
|
func (u *MachineReadableUi) ProgressBar() ProgressBar {
|
2018-09-05 08:02:09 -04:00
|
|
|
return new(NoopProgressBar)
|
2018-09-04 11:57:21 -04:00
|
|
|
}
|