-
Notifications
You must be signed in to change notification settings - Fork 0
/
shell.go
62 lines (54 loc) · 963 Bytes
/
shell.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
package shell
import (
"os"
"fmt"
"github.com/fatih/color"
)
func arg(a ...interface{}) string {
if len(a) == 1 {
return fmt.Sprintln(a[0])
} else {
return fmt.Sprintf(a[0].(string), a[1:]...)
}
}
func Say(a ...interface{}) {
fmt.Print(arg(a...))
}
// output to stderr
func Say2(a ...interface{}) {
fmt.Fprintf(os.Stderr, arg(a...))
}
func Warn(a ...interface{}) {
Say2(color.YellowString(arg(a...)))
}
// to stderr
func Error(a ...interface{}) {
var msg string
if len(a) == 1 {
switch v := a[0].(type) {
case string:
msg = v
case error:
msg = v.Error()
default:
panic("unkown data type")
}
} else {
msg = arg(a...)
}
if os.Getenv("DEBUG") != "" {
panic(msg)
} else {
Say2(color.RedString(msg))
}
}
// with exit 1
func ErrorExit(a ...interface{}) {
Error(a...)
os.Exit(1)
}
func Debug(a ...interface{}) {
if len(os.Getenv("DEBUG")) > 0 {
Say2(a...)
}
}