packer-cn/packer/ui.go

56 lines
1.3 KiB
Go
Raw Normal View History

package packer
import (
"fmt"
"io"
2013-05-21 14:40:07 -04:00
"log"
)
// 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 {
Say(format string, a ...interface{})
2013-05-10 20:01:24 -04:00
Error(format string, a ...interface{})
}
2013-05-21 16:20:51 -04:00
// PrefixedUi is a UI that wraps another UI implementation and adds a
// prefix to all the messages going out.
type PrefixedUi struct {
Prefix string
Ui Ui
}
// The ReaderWriterUi is a UI that writes and reads from standard Go
// io.Reader and io.Writer.
type ReaderWriterUi struct {
Reader io.Reader
Writer io.Writer
}
2013-05-21 16:20:51 -04:00
func (u *PrefixedUi) Say(format string, a ...interface{}) {
u.Ui.Say(fmt.Sprintf("%s: %s", u.Prefix, format), a...)
}
func (u *PrefixedUi) Error(format string, a ...interface{}) {
u.Ui.Error(fmt.Sprintf("%s: %s", u.Prefix, format), a...)
}
func (rw *ReaderWriterUi) Say(format string, a ...interface{}) {
2013-05-21 14:40:07 -04:00
output := fmt.Sprintf(format, a...)
log.Printf("ui: %s", output)
_, err := fmt.Fprint(rw.Writer, output+"\n")
2013-05-08 20:09:10 -04:00
if err != nil {
panic(err)
}
}
2013-05-08 18:12:48 -04:00
func (rw *ReaderWriterUi) Error(format string, a ...interface{}) {
2013-05-21 14:40:07 -04:00
output := fmt.Sprintf(format, a...)
log.Printf("ui error: %s", output)
_, err := fmt.Fprint(rw.Writer, output+"\n")
2013-05-08 20:09:10 -04:00
if err != nil {
panic(err)
}
2013-05-08 18:12:48 -04:00
}