-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
73 lines (56 loc) · 1.16 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
package main
import (
"errors"
"fmt"
"log"
"os/exec"
"strings"
githubactions "github.com/sethvargo/go-githubactions"
)
var (
// errGoNotFound where is go??
errGoNotFound = errors.New("could not find go in PATH")
// errRunFailed means we could not run the thing
errRunFailed = errors.New("failed to execute go test -cover -json")
)
type jsonfile []string
func (j *jsonfile) Write(p []byte) (n int, err error) {
var l = strings.Split(string(p), "\n")
for i := range l {
if l[i] == "" {
continue
}
*j = append(*j, l[i])
}
return len(p), nil
}
func (j jsonfile) String() string {
return "[" + strings.Join(j, ",") + "]"
}
func run() (string, error) {
var (
err error
g string
c *exec.Cmd
j jsonfile
)
g, err = exec.LookPath("go")
if err != nil {
return "", errGoNotFound
}
c = exec.Command(g, "test", "-cover", "-json", "./...")
c.Stdout = &j
err = c.Run()
if err != nil {
return "", fmt.Errorf("%w: %s", errRunFailed, err.Error())
}
return j.String(), nil
}
func main() {
var j, err = run()
if err != nil {
log.Println(err.Error())
githubactions.Fatalf(err.Error())
}
githubactions.SetOutput("gotessa_json", j)
}