graphql-engine/cli/migrate/printer.go

59 lines
1.1 KiB
Go
Raw Normal View History

2018-06-24 16:40:48 +03:00
package migrate
import (
"fmt"
"io"
)
const (
LEVEL_0 = iota
LEVEL_1
LEVEL_2
LEVEL_3
)
// PrefixWriter can write text at various indentation levels.
type PrefixWriter interface {
// Write writes text with the specified indentation level.
Write(level int, format string, a ...interface{})
// WriteLine writes an entire line with no indentation level.
WriteLine(a ...interface{})
// Flush forces indendation to be reset.
Flush()
}
// prefixWriter implements PrefixWriter
type prefixWriter struct {
out io.Writer
}
var _ PrefixWriter = &prefixWriter{}
// NewPrefixWriter creates a new PrefixWriter.
func NewPrefixWriter(out io.Writer) PrefixWriter {
return &prefixWriter{out: out}
}
func (pw *prefixWriter) Write(level int, format string, a ...interface{}) {
levelSpace := " "
prefix := ""
for i := 0; i < level; i++ {
prefix += levelSpace
}
fmt.Fprintf(pw.out, prefix+format, a...)
}
func (pw *prefixWriter) WriteLine(a ...interface{}) {
fmt.Fprintln(pw.out, a...)
}
func (pw *prefixWriter) Flush() {
if f, ok := pw.out.(flusher); ok {
f.Flush()
}
}
type flusher interface {
Flush()
}