open-consul/vendor/github.com/mitchellh/cli/ui_mock.go
Sean Chittenden 333ff22e9a Manage dependencies via Godep
Embrace the future and use Go 1.6's vendor support via Godep.

Go 1.5 users should `export GO15VENDOREXPERIMENT=1`
2016-02-12 16:50:37 -08:00

65 lines
1.1 KiB
Go

package cli
import (
"bytes"
"fmt"
"io"
"sync"
)
// MockUi is a mock UI that is used for tests and is exported publicly for
// use in external tests if needed as well.
type MockUi struct {
InputReader io.Reader
ErrorWriter *bytes.Buffer
OutputWriter *bytes.Buffer
once sync.Once
}
func (u *MockUi) Ask(query string) (string, error) {
u.once.Do(u.init)
var result string
fmt.Fprint(u.OutputWriter, query)
if _, err := fmt.Fscanln(u.InputReader, &result); err != nil {
return "", err
}
return result, nil
}
func (u *MockUi) AskSecret(query string) (string, error) {
return u.Ask(query)
}
func (u *MockUi) Error(message string) {
u.once.Do(u.init)
fmt.Fprint(u.ErrorWriter, message)
fmt.Fprint(u.ErrorWriter, "\n")
}
func (u *MockUi) Info(message string) {
u.Output(message)
}
func (u *MockUi) Output(message string) {
u.once.Do(u.init)
fmt.Fprint(u.OutputWriter, message)
fmt.Fprint(u.OutputWriter, "\n")
}
func (u *MockUi) Warn(message string) {
u.once.Do(u.init)
fmt.Fprint(u.ErrorWriter, message)
fmt.Fprint(u.ErrorWriter, "\n")
}
func (u *MockUi) init() {
u.ErrorWriter = new(bytes.Buffer)
u.OutputWriter = new(bytes.Buffer)
}