-
Notifications
You must be signed in to change notification settings - Fork 18
/
output.go
104 lines (89 loc) · 2.62 KB
/
output.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
package logs
import (
"github.com/cirruslabs/echelon"
"github.com/cirruslabs/echelon/renderers"
"github.com/cirruslabs/echelon/renderers/config"
"io"
"os"
)
const (
OutputAuto = "auto"
OutputInteractive = "interactive"
OutputNoEmoji = "noemoji"
OutputSimple = "simple"
OutputTravis = "travis"
OutputGA = "github-actions"
OutputTeamCity = "teamcity"
)
func DefaultFormat() string {
return OutputAuto
}
func Formats() []string {
return []string{
OutputAuto,
OutputInteractive,
OutputNoEmoji,
OutputSimple,
OutputTravis,
OutputGA,
OutputTeamCity,
}
}
func GetLogger(format string, verbose bool, logWriter io.Writer, logFile *os.File) (*echelon.Logger, func()) {
if format == OutputAuto && envVariableIsTrue("TRAVIS") {
format = OutputTravis
}
if format == OutputAuto && envVariableIsTrue("GITHUB_ACTIONS") {
format = OutputGA
}
if format == OutputAuto && envVariableIsSet("TEAMCITY_VERSION") {
format = OutputTeamCity
}
if format == OutputAuto && envVariableIsTrue("CI") {
format = OutputSimple
}
if format == OutputAuto {
format = OutputInteractive
}
var defaultSimpleRenderer = renderers.NewSimpleRenderer(logWriter, nil)
var renderer echelon.LogRendered = defaultSimpleRenderer
cancelFunc := func() {}
switch format {
case OutputInteractive:
rendererConfig := config.NewDefaultRenderingConfig()
rendererConfig.DescriptionLinesWhenSkipped = rendererConfig.DescriptionLinesWhenFailed
interactiveRenderer := renderers.NewInteractiveRenderer(logFile, rendererConfig)
go interactiveRenderer.StartDrawing()
cancelFunc = func() {
interactiveRenderer.StopDrawing()
}
renderer = interactiveRenderer
case OutputNoEmoji:
rendererConfig := config.NewDefaultSymbolsOnlyRenderingConfig()
rendererConfig.DescriptionLinesWhenSkipped = rendererConfig.DescriptionLinesWhenFailed
interactiveRenderer := renderers.NewInteractiveRenderer(logFile, rendererConfig)
go interactiveRenderer.StartDrawing()
cancelFunc = func() {
interactiveRenderer.StopDrawing()
}
renderer = interactiveRenderer
case OutputTravis:
renderer = NewTravisCILogsRenderer(defaultSimpleRenderer)
case OutputGA:
renderer = NewGithubActionsLogsRenderer(defaultSimpleRenderer)
case OutputTeamCity:
renderer = NewTeamCityLogsRenderer(defaultSimpleRenderer)
}
logger := echelon.NewLogger(echelon.InfoLevel, renderer)
if verbose {
logger = echelon.NewLogger(echelon.DebugLevel, renderer)
}
return logger, cancelFunc
}
func envVariableIsSet(name string) bool {
_, ok := os.LookupEnv(name)
return ok
}
func envVariableIsTrue(name string) bool {
return os.Getenv(name) == "true"
}