mirror of
https://github.com/square/certigo.git
synced 2024-04-21 12:32:40 +00:00
This splits the logic into a cli, which handles parsing command line args and reading files, and a terminal abstraction for handling user input and ouput. All uses of os.Exit are removed in favor of returning errors. Overall this enables better testing and reuse of code. Previously we had to rely on external unit testing for CLI tests, which are harder to write tests.
40 lines
711 B
Go
40 lines
711 B
Go
package terminal
|
|
|
|
import (
|
|
"bytes"
|
|
"io"
|
|
|
|
"github.com/mattn/go-colorable"
|
|
)
|
|
|
|
// TestTerminal just collects input into buffers
|
|
// That can be used to check output in tests
|
|
type TestTerminal struct {
|
|
OutputBuf bytes.Buffer
|
|
ErrorBuf bytes.Buffer
|
|
Password string
|
|
Width int
|
|
}
|
|
|
|
var _ Terminal = &TestTerminal{}
|
|
|
|
func (t *TestTerminal) Output() io.Writer {
|
|
return colorable.NewNonColorable(&t.OutputBuf)
|
|
}
|
|
|
|
func (t *TestTerminal) Error() io.Writer {
|
|
return &t.ErrorBuf
|
|
}
|
|
|
|
func (t *TestTerminal) SetDefaultPassword(password string) {
|
|
t.Password = password
|
|
}
|
|
|
|
func (t *TestTerminal) ReadPassword(prompt string) string {
|
|
return t.Password
|
|
}
|
|
|
|
func (t TestTerminal) DetermineWidth() int {
|
|
return t.Width
|
|
}
|