-
-
Notifications
You must be signed in to change notification settings - Fork 32
/
main.go
127 lines (106 loc) · 3.04 KB
/
main.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
package main
import (
"bytes"
"fmt"
"github.com/mumoshu/variant/cmd"
"os"
)
func main() {
// See https://help.github.com/en/articles/virtual-environments-for-github-actions#environment-variables
// for the list of supported envvars that can be used to determine it is running on GitHub Actions (or not)
if os.Getenv("GITHUB_ACTION") != "" {
stdout := os.Stdout
stdoutR, stdoutW, _ := os.Pipe()
os.Stdout = stdoutW
stderr := os.Stderr
stderrR, stderrW, _ := os.Pipe()
os.Stderr = stderrW
stdoutCaptureStop := make(chan struct{})
stderrCaptureStop := make(chan struct{})
var allBuf bytes.Buffer
// Concurrently read stdout to not block writes from cmd.MustRun()
var stdoutBuf bytes.Buffer
go func() {
line := linesScanner(stdoutR, stderr)
for line.Scan() {
str := line.Text()
//stdout.WriteString(fmt.Sprintf("str=%q, cnt=%d\n", str, charCount(str, stderr)))
if charCount(str, stderr) > 0 {
str += "\n"
}
if _, err := stdoutBuf.WriteString(str); err != nil {
panic(err)
}
if _, err := allBuf.WriteString(str); err != nil {
panic(err)
}
stdout.WriteString(str)
}
close(stdoutCaptureStop)
}()
// Concurrently read stderr to not block writes from cmd.MustRun()
var stderrBuf bytes.Buffer
go func() {
line := linesScanner(stderrR, stderr)
for line.Scan() {
str := line.Text()
//stdout.WriteString(fmt.Sprintf("str=%q, cnt=%d\n", str, charCount(str, stderr)))
if charCount(str, stderr) > 0 {
str += "\n"
}
if _, err := stderrBuf.WriteString(str); err != nil {
panic(err)
}
if _, err := allBuf.WriteString(str); err != nil {
panic(err)
}
stderr.WriteString(str)
}
close(stderrCaptureStop)
}()
runOpts, runErr := cmd.RunE()
// Restore stdout/err before closing the pipes. Otherwise writes to os.Stdout/err(pipes) fail because they are closed
os.Stdout = stdout
os.Stderr = stderr
runOpts.Log.Out = stderr
if err := stdoutW.Close(); err != nil {
panic(err)
}
if err := stderrW.Close(); err != nil {
panic(err)
}
<-stdoutCaptureStop
<-stderrCaptureStop
stdoutCapture := stdoutBuf.String()
allCapture := allBuf.String()
errMsg, status := cmd.HandleError(runErr, runOpts)
if errMsg != "" {
allCapture += errMsg + "\n"
}
send := func() error {
name := os.Getenv("VARIANT_NAME")
if name == "" {
name = "variant"
}
c := os.Getenv("VARIANT_RUN")
status := cmd.GetStatus(runErr, runOpts)
return sendGitHubComment(name, c, fmt.Sprintf("%d", status), stdoutCapture, allCapture)
}
if runErr != nil {
if os.Getenv("VARIANT_GITHUB_COMMENT") != "" || os.Getenv("VARIANT_GITHUB_COMMENT_ON_FAILURE") != "" {
if err := send(); err != nil {
cmd.HandleErrorAndExit(err, runOpts)
}
}
cmd.LogAndExit(runOpts, errMsg, status)
} else {
if os.Getenv("VARIANT_GITHUB_COMMENT") != "" || os.Getenv("VARIANT_GITHUB_COMMENT_ON_SUCCESS") != "" {
if err := send(); err != nil {
cmd.HandleErrorAndExit(err, runOpts)
}
}
}
} else {
cmd.MustRun()
}
}