-
Notifications
You must be signed in to change notification settings - Fork 215
/
io.go
242 lines (207 loc) · 5.08 KB
/
io.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package main
import (
"fmt"
"io"
"log"
"os"
"path/filepath"
"runtime"
"runtime/debug"
"strings"
"github.com/lunixbochs/vtclean"
"golang.org/x/crypto/ssh/terminal"
)
// Stdout is used to mock stdout for testing
var Stdout io.Writer
// Stderr is to mock stderr for testing
var Stderr io.Writer
// ErrLogPath is the location of the error log
var ErrLogPath = filepath.Join(AppDir(), "error.log")
var errLogger = newLogger(ErrLogPath)
var exitFn = os.Exit
var debugging = isDebugging()
var debuggingHeaders = isDebuggingHeaders()
func init() {
Stdout = os.Stdout
Stderr = os.Stderr
}
func newLogger(path string) *log.Logger {
err := os.MkdirAll(filepath.Dir(path), 0777)
if err != nil {
panic(err)
}
file, err := os.OpenFile(path, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0644)
if err != nil {
panic(err)
}
return log.New(file, "", log.LstdFlags)
}
// Exit just calls os.Exit, but can be mocked out for testing
func Exit(code int) {
exitFn(code)
}
// Err just calls `fmt.Fprint(Stderr, a...)` but can be mocked out for testing.
func Err(a ...interface{}) {
fmt.Fprint(Stderr, a...)
Log(a...)
}
// Errf just calls `fmt.Fprintf(Stderr, a...)` but can be mocked out for testing.
func Errf(format string, a ...interface{}) {
fmt.Fprintf(Stderr, format, a...)
Logf(format, a...)
}
// Errln just calls `fmt.Fprintln(Stderr, a...)` but can be mocked out for testing.
func Errln(a ...interface{}) {
fmt.Fprintln(Stderr, a...)
Logln(a...)
}
// Print is used to replace `fmt.Print()` but can be mocked out for testing.
func Print(a ...interface{}) {
fmt.Fprint(Stdout, a...)
}
// Printf is used to replace `fmt.Printf()` but can be mocked out for testing.
func Printf(format string, a ...interface{}) {
fmt.Fprintf(Stdout, format, a...)
}
// Println is used to replace `fmt.Println()` but can be mocked out for testing.
func Println(a ...interface{}) {
fmt.Fprintln(Stdout, a...)
}
// Log is used to print debugging information
// It will be added to the logfile in ~/.heroku or printed out if HEROKU_DEBUG is set.
func Log(a ...interface{}) {
errLogger.Print(vtclean.Clean(fmt.Sprint(a...), false))
}
// Logln is used to print debugging information
// It will be added to the logfile in ~/.heroku
func Logln(a ...interface{}) {
Log(fmt.Sprintln(a...))
}
// Logf is used to print debugging information
// It will be added to the logfile in ~/.heroku
func Logf(format string, a ...interface{}) {
Log(fmt.Sprintf(format, a...))
}
// Debugln is used to print debugging information
// It will be added to the logfile in ~/.heroku and stderr if HEROKU_DEBUG is set.
func Debugln(a ...interface{}) {
Logln(a...)
if debugging {
fmt.Fprintln(Stderr, a...)
}
}
// Debugf is used to print debugging information
// It will be added to the logfile in ~/.heroku and stderr if HEROKU_DEBUG is set.
func Debugf(f string, a ...interface{}) {
Logf(f, a...)
if debugging {
fmt.Fprintf(Stderr, f, a...)
}
}
// PrintError is a helper that prints out formatted error messages in red text
func PrintError(e error, newline bool) {
if e == nil {
return
}
if newline {
Errln()
}
Error(e.Error())
if debugging {
debug.PrintStack()
}
}
// Warn shows a message with excalamation points prepended to stderr
func Warn(msg string) {
bang := yellow(" ▸ ")
msg = strings.TrimSpace(msg)
msg = strings.Join(strings.Split(msg, "\n"), "\n"+bang)
Errln(bang + msg)
}
// Error shows a message with excalamation points prepended to stderr
func Error(msg string) {
bang := red(" " + errorArrow() + " ")
msg = strings.TrimSpace(msg)
msg = strings.Join(strings.Split(msg, "\n"), "\n"+bang)
Errln(bang + msg)
}
func errorArrow() string {
if windows() {
return "!"
}
return "▸"
}
// ExitIfError calls PrintError and exits if e is not null
func ExitIfError(e error, newline bool) {
if e != nil {
PrintError(e, newline)
os.Exit(1)
}
}
// LogIfError logs out an error if one arises
func LogIfError(e error) {
if e != nil {
Logln(e.Error())
}
}
func isDebugging() bool {
debug := strings.ToUpper(os.Getenv("HEROKU_DEBUG"))
if debug == "TRUE" || debug == "1" {
return true
}
return false
}
func isDebuggingHeaders() bool {
debug := strings.ToUpper(os.Getenv("HEROKU_DEBUG_HEADERS"))
if debug == "TRUE" || debug == "1" {
return true
}
return false
}
func yellow(s string) string {
if supportsColor() && !windows() {
return "\x1b[33m" + s + "\x1b[39m"
}
return s
}
func red(s string) string {
if supportsColor() && !windows() {
return "\x1b[31m" + s + "\x1b[39m"
}
return s
}
func green(s string) string {
if supportsColor() && !windows() {
return "\x1b[32m" + s + "\x1b[39m"
}
return s
}
func cyan(s string) string {
if supportsColor() && !windows() {
return "\x1b[36m" + s + "\x1b[39m"
}
return s
}
func windows() bool {
return runtime.GOOS == "windows"
}
func istty() bool {
return terminal.IsTerminal(int(os.Stdout.Fd()))
}
func supportsColor() bool {
if !istty() {
return false
}
for _, arg := range os.Args {
if arg == "--no-color" {
return false
}
}
return os.Getenv("COLOR") != "false"
}
func plural(word string, count int) string {
if count == 1 {
return word
}
return word + "s"
}