This repository has been archived by the owner on Oct 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathoutput_terminal.go
256 lines (213 loc) · 5.5 KB
/
output_terminal.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package main
import (
"bytes"
"fmt"
"io"
"sort"
"strings"
"sync"
"github.com/fatih/color"
"github.com/gosuri/uilive"
"golang.org/x/crypto/ssh/terminal"
"github.com/mitchellh/golicense/config"
"github.com/mitchellh/golicense/license"
"github.com/mitchellh/golicense/module"
)
// TermOutput is an Output implementation that outputs to the terminal.
type TermOutput struct {
// Out is the stdout to write to. If this is a TTY, TermOutput will
// automatically use a "live" updating output mode for status updates.
// This can be disabled by setting Plain to true below.
Out io.Writer
// Config is the configuration (if any). This will be used to check
// if a license is allowed or not.
Config *config.Config
// Modules is the full list of modules that will be checked. This is
// optional. If this is given in advance, then the output will be cleanly
// aligned.
Modules []module.Module
// Plain, if true, will use the plain output vs the live updating output.
// TermOutput will always use Plain output if the Out configured above
// is not a TTY.
Plain bool
// Verbose will log all status updates in plain mode. This has no effect
// in non-plain mode currently.
Verbose bool
modules map[string]string
moduleMax int
exitCode int
lineMax int
live *uilive.Writer
once sync.Once
lock sync.Mutex
}
func (o *TermOutput) ExitCode() int {
return o.exitCode
}
// Start implements Output
func (o *TermOutput) Start(m *module.Module) {
o.once.Do(o.init)
if o.Plain {
return
}
o.lock.Lock()
defer o.lock.Unlock()
o.modules[m.Path] = fmt.Sprintf("%s %s starting...", iconNormal, o.paddedModule(m))
o.updateLiveOutput()
}
// Update implements Output
func (o *TermOutput) Update(m *module.Module, t license.StatusType, msg string) {
o.once.Do(o.init)
// In plain & verbose mode, we output every status message, but in normal
// plain mode we ignore all status updates.
if o.Plain && o.Verbose {
fmt.Fprintf(o.Out, fmt.Sprintf(
"%s %s\n", o.paddedModule(m), msg))
}
if o.Plain {
return
}
var colorFunc func(string, ...interface{}) string = fmt.Sprintf
icon := iconNormal
switch t {
case license.StatusWarning:
icon = iconWarning
colorFunc = color.YellowString
case license.StatusError:
icon = iconError
colorFunc = color.RedString
}
if icon != "" {
icon += " "
}
o.lock.Lock()
defer o.lock.Unlock()
o.modules[m.Path] = colorFunc("%s%s %s", icon, o.paddedModule(m), msg)
o.updateLiveOutput()
}
// Finish implements Output
func (o *TermOutput) Finish(m *module.Module, l *license.License, err error) {
o.once.Do(o.init)
var colorFunc func(string, ...interface{}) string = fmt.Sprintf
icon := iconNormal
if o.Config != nil {
state := o.Config.Allowed(l)
switch state {
case config.StateAllowed:
colorFunc = color.GreenString
icon = iconSuccess
case config.StateDenied:
colorFunc = color.RedString
icon = iconError
o.exitCode = 1
case config.StateUnknown:
if len(o.Config.Allow) > 0 || len(o.Config.Deny) > 0 {
colorFunc = color.YellowString
icon = iconWarning
o.exitCode = 1
}
}
}
if icon != "" {
icon += " "
}
if o.Plain {
fmt.Fprintf(o.Out, fmt.Sprintf(
"%s %s\n", o.paddedModule(m), l.String()))
return
}
o.lock.Lock()
defer o.lock.Unlock()
delete(o.modules, m.Path)
o.pauseLive(func() {
o.live.Write([]byte(colorFunc(
"%s%s %s\n", icon, o.paddedModule(m), l.String())))
})
}
// Close implements Output
func (o *TermOutput) Close() error {
o.lock.Lock()
defer o.lock.Unlock()
if o.live != nil {
o.live.Stop()
}
return nil
}
// paddedModule returns the name of the module padded so that they align nicely.
func (o *TermOutput) paddedModule(m *module.Module) string {
o.once.Do(o.init)
if o.moduleMax == 0 {
return m.Path
}
// Pad the path so that it is equivalent to the moduleMax length
return m.Path + strings.Repeat(" ", o.moduleMax-len(m.Path))
}
// pauseLive pauses the live output for the duration of the function.
//
// lock must be held.
func (o *TermOutput) pauseLive(f func()) {
o.live.Write([]byte(strings.Repeat(" ", o.lineMax) + "\n"))
o.live.Flush()
f()
o.live.Flush()
o.live.Stop()
o.newLive()
o.updateLiveOutput()
}
// updateLiveOutput updates the output buffer for live status.
//
// lock must be held when this is called
func (o *TermOutput) updateLiveOutput() {
keys := make([]string, 0, len(o.modules))
for k := range o.modules {
keys = append(keys, k)
}
sort.Strings(keys)
var buf bytes.Buffer
for _, k := range keys {
if v := len(o.modules[k]); v > o.lineMax {
o.lineMax = v
}
buf.WriteString(o.modules[k] + strings.Repeat(" ", o.lineMax-len(o.modules[k])) + "\n")
}
o.live.Write(buf.Bytes())
o.live.Flush()
}
func (o *TermOutput) newLive() {
o.live = uilive.New()
o.live.Out = o.Out
o.live.Start()
}
func (o *TermOutput) init() {
if o.modules == nil {
o.modules = make(map[string]string)
}
// Calculate the maximum module length
for _, m := range o.Modules {
if v := len(m.Path); v > o.moduleMax {
o.moduleMax = v
}
}
// Check if the output is a TTY
if !o.Plain {
o.Plain = true // default to plain mode unless we can verify TTY
if iofd, ok := o.Out.(ioFd); ok {
o.Plain = !terminal.IsTerminal(int(iofd.Fd()))
}
if !o.Plain {
o.newLive()
}
}
}
// ioFd is an interface that is implemented by things that have a file
// descriptor. We use this to check if the io.Writer is a TTY.
type ioFd interface {
Fd() uintptr
}
const (
iconNormal = ""
iconWarning = "⚠️ "
iconError = "🚫"
iconSuccess = "✅"
iconSpace = " "
)