generated from gleich/go_template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatuser.go
112 lines (100 loc) · 2.52 KB
/
statuser.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
package statuser
import (
"fmt"
"os"
"strings"
"unicode/utf8"
"github.com/enescakir/emoji"
"github.com/fatih/color"
)
var (
// If the output should use emojis
Emojis = true
// Error emoji
ErrorEmoji = emoji.PoliceCarLight
// Error text
ErrorText = " ERROR "
// Error box character
ErrorBoxChar = "░"
// Error character if emojis are turned off
ErrorChar = "✗"
// Warning emoji
WarningEmoji = emoji.Warning
// Warning text
WarningText = " WARNING "
// Warning character if emojis are turned off
WarningChar = "◈"
// Success emoji
SuccessEmoji = emoji.CheckMarkButton
// Warning text
SuccessText = " "
// Warning character if emojis are turned off
SuccessChar = "✔"
)
func generateBlock(message, surroundingChar string) string {
messageLen := utf8.RuneCountInString(message)
var topAndBottom string
var extension int
if Emojis {
extension = 4
} else {
extension = 2
}
for i := 0; i < messageLen+extension; i++ {
topAndBottom = topAndBottom + surroundingChar
}
return fmt.Sprintf(
"%v\n%v%v%v\n%v",
topAndBottom,
surroundingChar,
message,
surroundingChar,
topAndBottom,
)
}
func separateBySpaces(items []interface{}) string {
stringItems := []string{}
for _, item := range items {
stringItems = append(stringItems, fmt.Sprint(item))
}
return strings.Join(stringItems, " ")
}
// Output an error to the user
func Error(message string, err error, exitCode int) {
title := emoji.Sprint(ErrorChar, ErrorText, ErrorChar)
if Emojis {
title = emoji.Sprint(ErrorEmoji, ErrorText, ErrorEmoji)
}
color.Red(generateBlock(title, ErrorBoxChar))
color.Red("\n" + message)
color.Red("\nGOLANG ERROR (SHOW DEVELOPER):\n" + err.Error())
os.Exit(exitCode)
}
// Output an error to the user
func ErrorMsg(message string, exitCode int) {
title := emoji.Sprint(ErrorChar, ErrorText, ErrorChar)
if Emojis {
title = emoji.Sprint(ErrorEmoji, ErrorText, ErrorEmoji)
}
color.Red(generateBlock(title, ErrorBoxChar))
color.Red("\n" + message)
os.Exit(exitCode)
}
// Output a warning to the user
func Warning(msgItems ...interface{}) {
message := separateBySpaces(msgItems)
title := emoji.Sprint(WarningChar, WarningText, WarningChar)
if Emojis {
title = emoji.Sprint(WarningEmoji, WarningText, WarningEmoji)
}
color.Yellow(title + "\n" + message)
}
// Output a success to the user
func Success(msgItems ...interface{}) {
message := separateBySpaces(msgItems)
prefix := emoji.Sprint(SuccessChar, SuccessText)
if Emojis {
prefix = emoji.Sprint(SuccessEmoji, SuccessText)
}
color.Green(prefix + message)
}