-
Notifications
You must be signed in to change notification settings - Fork 2
/
outputer.go
90 lines (71 loc) · 1.88 KB
/
outputer.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package main
import (
"fmt"
"io"
. "github.com/logrusorgru/aurora"
)
type OutputerMode string
const (
OutputerModeQuiet OutputerMode = "3"
OutputerModeNormal OutputerMode = "6"
OutputerModeVerbose OutputerMode = "9"
)
type IOutputer interface {
OutVerbose(format string, v ...interface{})
OutInfo(format string, v ...interface{})
OutWarn(format string, v ...interface{})
OutError(format string, v ...interface{})
}
// Outputer is a logger that shows the output to the user.
type Outputer struct {
Mode OutputerMode
Output io.Writer
Logger LogWriter
}
// NewOutputer creates a new Outputer.
func NewOutputer(mode OutputerMode, output io.Writer, logger LogWriter) Outputer {
return Outputer{
Mode: mode,
Output: output,
Logger: logger,
}
}
// OutVerbose prints the message to the Output if the Mode is at least OutputerModeVerbose.
func (o *Outputer) OutVerbose(format string, v ...interface{}) {
msg := fmt.Sprintf(format, v...)
o.Logger.Write(msg)
if o.Mode < OutputerModeVerbose {
return
}
fmt.Fprintln(o.Output, msg)
}
// OutInfo prints the message to the Output if the Mode is at least OutputerModeNormal.
func (o *Outputer) OutInfo(format string, v ...interface{}) {
msg := fmt.Sprintf(format, v...)
o.Logger.Write(msg)
if o.Mode < OutputerModeNormal {
return
}
fmt.Fprintln(o.Output, msg)
}
// OutWarn prints warning message to stdout. TODO
func (o *Outputer) OutWarn(format string, v ...interface{}) {
o.Logger.Write(fmt.Sprintf("WARN: %s", fmt.Sprintf(format, v...)))
fmt.Fprintf(
o.Output,
"%s: %s\n",
Brown("WARN"),
fmt.Sprintf(format, v...),
)
}
// OutError prints error message to stdout.
func (o *Outputer) OutError(format string, v ...interface{}) {
o.Logger.Write(fmt.Sprintf("ERROR: %s", fmt.Sprintf(format, v...)))
// TODO: write to stderr
fmt.Fprintf(
o.Output,
"%s: %s\n",
Red("ERROR"),
fmt.Sprintf(format, v...),
)
}