/
log.go
121 lines (109 loc) · 2.89 KB
/
log.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
package lib
import (
"fmt"
"log"
"os"
"sort"
"strconv"
"strings"
"github.com/olekukonko/tablewriter"
"github.com/ttacon/chalk"
)
var (
warn = chalk.Red.NewStyle().WithTextStyle(chalk.Bold).WithBackground(chalk.White).Style
err = chalk.Red.NewStyle().WithTextStyle(chalk.Bold).Style
notice = chalk.Cyan.NewStyle().WithTextStyle(chalk.Dim).Style
head = chalk.Green.NewStyle().WithTextStyle(chalk.Underline).Style
result = chalk.Green.NewStyle().Style
)
// PrHeaderf print underlined header
func PrHeaderf(format string, a ...interface{}) {
if os.Getenv("CI") == "true" {
fmt.Printf(format, a...)
return
}
fmt.Printf(head(format), a...)
}
// PrNoticef print dimmed notice
func PrNoticef(format string, a ...interface{}) {
if os.Getenv("CI") == "true" {
fmt.Printf(format, a...)
return
}
fmt.Printf(notice(format), a...)
}
// PrWarnf print bold warning
func PrWarnf(format string, a ...interface{}) {
if os.Getenv("CI") == "true" {
fmt.Printf(format, a...)
return
}
fmt.Printf(warn(format), a...)
}
// PrFatalf log fatal
func PrFatalf(format string, a ...interface{}) {
if os.Getenv("CI") == "true" {
log.Fatalf(format, a...)
return
}
log.Fatalf(err(format), a...)
}
// PrResultf print green result
func PrResultf(format string, a ...interface{}) {
if os.Getenv("CI") == "true" {
fmt.Printf(format, a...)
return
}
fmt.Printf(result(format), a...)
}
// PrettyList output list prettified for terminal
func PrettyList(str string, list []string) {
if len(list) > 0 {
PrHeaderf("\n%s\n", str)
for _, s := range list {
fmt.Printf(" - %s\n", s)
}
fmt.Println()
}
}
// PrettyHash output list prettified for terminal
func PrettyHash(str string, list []Hash) {
fmt.Println()
data := [][]string{}
for i, s := range list {
data = append(data, []string{strconv.Itoa(i + 1), s.Name, s.Hash, s.Path})
}
PrettyTable(str, []string{"#", "NAME", "HASH", "PATH"}, data)
fmt.Println()
}
// PrintResponse print delete response prettified
func PrintResponse(s string, a []DeleteResult) {
if len(a) > 0 {
fmt.Println()
// sort result by code
sort.Slice(a, func(i, j int) bool {
return a[i].Code < a[j].Code
})
data := [][]string{}
for i, it := range a {
data = append(data, []string{strconv.Itoa(i + 1), it.ID, it.Type, it.Code, it.Message})
}
PrettyTable(s, []string{"#", "ID", "TYPE", "CODE", "MESSAGE"}, data)
fmt.Println()
}
}
// PrettyTable markdown style table output
func PrettyTable(caption string, header []string, data [][]string) {
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader(header)
table.SetAutoWrapText(false)
table.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false})
table.SetCenterSeparator("|")
if os.Getenv("CI") == "true" {
table.SetCaption(true, strings.ToUpper(caption))
} else {
table.SetCaption(true, result(strings.ToUpper(caption)))
}
table.AppendBulk(data) // Add Bulk Data
table.Render()
}