Commit 03c10a9a authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

packer: renamed PrefixedUi to TargettedUi

parent 02edc757
...@@ -213,11 +213,10 @@ func (b *coreBuild) Run(originalUi Ui, cache Cache) ([]Artifact, error) { ...@@ -213,11 +213,10 @@ func (b *coreBuild) Run(originalUi Ui, cache Cache) ([]Artifact, error) {
hook := &DispatchHook{hooks} hook := &DispatchHook{hooks}
artifacts := make([]Artifact, 0, 1) artifacts := make([]Artifact, 0, 1)
// The builder just has a normal Ui, but prefixed // The builder just has a normal Ui, but targetted
builderUi := &PrefixedUi{ builderUi := &TargettedUi{
fmt.Sprintf("==> %s", b.Name()), Target: b.Name(),
fmt.Sprintf(" %s", b.Name()), Ui: originalUi,
originalUi,
} }
log.Printf("Running builder: %s", b.builderType) log.Printf("Running builder: %s", b.builderType)
...@@ -240,10 +239,9 @@ PostProcessorRunSeqLoop: ...@@ -240,10 +239,9 @@ PostProcessorRunSeqLoop:
for _, ppSeq := range b.postProcessors { for _, ppSeq := range b.postProcessors {
priorArtifact := builderArtifact priorArtifact := builderArtifact
for i, corePP := range ppSeq { for i, corePP := range ppSeq {
ppUi := &PrefixedUi{ ppUi := &TargettedUi{
fmt.Sprintf("==> %s (%s)", b.Name(), corePP.processorType), Target: fmt.Sprintf("%s (%s)", b.Name(), corePP.processorType),
fmt.Sprintf(" %s (%s)", b.Name(), corePP.processorType), Ui: originalUi,
originalUi,
} }
builderUi.Say(fmt.Sprintf("Running post-processor: %s", corePP.processorType)) builderUi.Say(fmt.Sprintf("Running post-processor: %s", corePP.processorType))
......
...@@ -43,12 +43,14 @@ type ColoredUi struct { ...@@ -43,12 +43,14 @@ type ColoredUi struct {
Ui Ui Ui Ui
} }
// PrefixedUi is a UI that wraps another UI implementation and adds a // TargettedUi is a UI that wraps another UI implementation and modifies
// prefix to all the messages going out. // the output to indicate a specific target. Specifically, all Say output
type PrefixedUi struct { // is prefixed with the target name. Message output is not prefixed but
SayPrefix string // is offset by the length of the target so that output is lined up properly
MessagePrefix string // with Say output. Machine-readable output has the proper target set.
Ui Ui type TargettedUi struct {
Target string
Ui Ui
} }
// The BasicUI is a UI that reads and writes from a standard Go reader // The BasicUI is a UI that reads and writes from a standard Go reader
...@@ -116,32 +118,37 @@ func (u *ColoredUi) supportsColors() bool { ...@@ -116,32 +118,37 @@ func (u *ColoredUi) supportsColors() bool {
return cygwin return cygwin
} }
func (u *PrefixedUi) Ask(query string) (string, error) { func (u *TargettedUi) Ask(query string) (string, error) {
return u.Ui.Ask(u.prefixLines(u.SayPrefix, query)) return u.Ui.Ask(u.prefixLines(true, query))
} }
func (u *PrefixedUi) Say(message string) { func (u *TargettedUi) Say(message string) {
u.Ui.Say(u.prefixLines(u.SayPrefix, message)) u.Ui.Say(u.prefixLines(true, message))
} }
func (u *PrefixedUi) Message(message string) { func (u *TargettedUi) Message(message string) {
u.Ui.Message(u.prefixLines(u.MessagePrefix, message)) u.Ui.Message(u.prefixLines(false, message))
} }
func (u *PrefixedUi) Error(message string) { func (u *TargettedUi) Error(message string) {
u.Ui.Error(u.prefixLines(u.SayPrefix, message)) u.Ui.Error(u.prefixLines(true, message))
} }
func (u *PrefixedUi) Machine(t string, args ...string) { func (u *TargettedUi) Machine(t string, args ...string) {
// Just pass it through for now. // Just pass it through for now.
u.Ui.Machine(t, args...) u.Ui.Machine(t, args...)
} }
func (u *PrefixedUi) prefixLines(prefix, message string) string { func (u *TargettedUi) prefixLines(arrow bool, message string) string {
arrowText := "==>"
if !arrow {
arrowText = strings.Repeat(" ", len(arrowText))
}
var result bytes.Buffer var result bytes.Buffer
for _, line := range strings.Split(message, "\n") { for _, line := range strings.Split(message, "\n") {
result.WriteString(fmt.Sprintf("%s: %s\n", prefix, line)) result.WriteString(fmt.Sprintf("%s %s: %s\n", arrowText, u.Target, line))
} }
return strings.TrimRightFunc(result.String(), unicode.IsSpace) return strings.TrimRightFunc(result.String(), unicode.IsSpace)
......
...@@ -36,23 +36,26 @@ func TestColoredUi(t *testing.T) { ...@@ -36,23 +36,26 @@ func TestColoredUi(t *testing.T) {
} }
} }
func TestPrefixedUi(t *testing.T) { func TestTargettedUi(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true) assert := asserts.NewTestingAsserts(t, true)
bufferUi := testUi() bufferUi := testUi()
prefixUi := &PrefixedUi{"mitchell", "bar", bufferUi} TargettedUi := &TargettedUi{
Target: "foo",
Ui: bufferUi,
}
prefixUi.Say("foo") TargettedUi.Say("foo")
assert.Equal(readWriter(bufferUi), "mitchell: foo\n", "should have prefix") assert.Equal(readWriter(bufferUi), "==> foo: foo\n", "should have prefix")
prefixUi.Message("foo") TargettedUi.Message("foo")
assert.Equal(readWriter(bufferUi), "bar: foo\n", "should have prefix") assert.Equal(readWriter(bufferUi), " foo: foo\n", "should have prefix")
prefixUi.Error("bar") TargettedUi.Error("bar")
assert.Equal(readWriter(bufferUi), "mitchell: bar\n", "should have prefix") assert.Equal(readWriter(bufferUi), "==> foo: bar\n", "should have prefix")
prefixUi.Say("foo\nbar") TargettedUi.Say("foo\nbar")
assert.Equal(readWriter(bufferUi), "mitchell: foo\nmitchell: bar\n", "should multiline") assert.Equal(readWriter(bufferUi), "==> foo: foo\n==> foo: bar\n", "should multiline")
} }
func TestColoredUi_ImplUi(t *testing.T) { func TestColoredUi_ImplUi(t *testing.T) {
...@@ -63,11 +66,11 @@ func TestColoredUi_ImplUi(t *testing.T) { ...@@ -63,11 +66,11 @@ func TestColoredUi_ImplUi(t *testing.T) {
} }
} }
func TestPrefixedUi_ImplUi(t *testing.T) { func TestTargettedUi_ImplUi(t *testing.T) {
var raw interface{} var raw interface{}
raw = &PrefixedUi{} raw = &TargettedUi{}
if _, ok := raw.(Ui); !ok { if _, ok := raw.(Ui); !ok {
t.Fatalf("PrefixedUi must implement Ui") t.Fatalf("TargettedUi must implement Ui")
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment