-
Notifications
You must be signed in to change notification settings - Fork 221
/
noninteractive.go
64 lines (51 loc) · 1.42 KB
/
noninteractive.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package statuslogger
import (
"fmt"
"strings"
"github.com/superfly/flyctl/iostreams"
)
type noninteractiveLogger struct {
io *iostreams.IOStreams
lines []*noninteractiveLine
logNumbers bool
showStatus bool
}
func (nl *noninteractiveLogger) Line(i int) StatusLine {
return nl.lines[i]
}
// Destroy is a no-op for non-interactive loggers.
func (nl *noninteractiveLogger) Destroy(_ bool) {}
type noninteractiveLine struct {
logger *noninteractiveLogger
lineNum int
status Status
}
func (line *noninteractiveLine) Log(s string) {
buf := ""
if line.logger.showStatus {
buf += line.status.charFor(-1) + " "
}
if line.logger.logNumbers {
buf += formatIndex(line.lineNum, len(line.logger.lines)) + " "
}
buf += s
fmt.Fprintln(line.logger.io.Out, buf)
}
func (line *noninteractiveLine) Logf(format string, args ...interface{}) {
line.Log(fmt.Sprintf(format, args...))
}
func (line *noninteractiveLine) LogStatus(s Status, str string) {
line.status = s
line.Log(str)
}
func (line *noninteractiveLine) LogfStatus(s Status, format string, args ...interface{}) {
line.LogStatus(s, fmt.Sprintf(format, args...))
}
func (line *noninteractiveLine) Failed(e error) {
firstLine, _, _ := strings.Cut(e.Error(), "\n")
line.LogfStatus(StatusFailure, "Failed: %s", firstLine)
}
func (line *noninteractiveLine) setStatus(s Status) {
line.status = s
}
func (nl *noninteractiveLogger) Pause() ResumeFn { return func() {} }