This repository was archived by the owner on Sep 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
136 lines (108 loc) · 3.23 KB
/
main.go
File metadata and controls
136 lines (108 loc) · 3.23 KB
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
package main
import (
"bufio"
"flag"
"fmt"
"os"
"os/exec"
"strings"
)
var (
disableGoTest = flag.Bool("disable-go-test", false, "Disable go test execution")
disableGometalinter = flag.Bool("disable-gometalinter", false, "Disable gometalinter checks")
goTestFlags = flag.String("go-test-flags", "", "Send custom flags to go test as parameters")
goTestShort = flag.Bool("go-test-short", false, "Enable -short mode for go test")
gometalinterConfig = flag.String("gometalinter-config", "", "Path to configuration file for gometalinter")
gometalinterFlags = flag.String("gometalinter-flags", "", "Send custom flags to gometalinter")
gometalinterPath = flag.String("gometalinter-path", ".", "Path for gometalinter to lint. Set it to ./... for recursion")
ignoreErrors = flag.Bool("ignore-errors", false, "Continue with the next check on errors")
packageName = flag.String("package", "", "Package name to test")
recursive = flag.Bool("recursive", false, "Run tests recursivly")
verbose = flag.Bool("verbose", false, "Enable verbose output")
)
func main() {
flag.Parse()
if *packageName == "" {
fmt.Println("Missing required parameter -package")
flag.Usage()
os.Exit(1)
}
if !*disableGometalinter {
runGometalinter()
}
if !*disableGoTest {
runGoTest()
}
}
// runGoTest is executing go test with the given parameters.
func runGoTest() {
fmt.Println("+++ Running go test")
var args []string
args = append(args, "test")
if *goTestFlags != "" {
args = append(args, *goTestFlags)
}
if *goTestShort {
args = append(args, "-short")
}
pkg := *packageName
if *recursive {
pkg = fmt.Sprintf("%s/...", pkg)
}
args = append(args, pkg)
runCommand("go", args, *ignoreErrors, *verbose)
}
// runGometalinter is running the gometalinter checks.
func runGometalinter() {
fmt.Println("+++ Running gometalinter checks")
var args []string
if *gometalinterFlags != "" {
args = append(args, *gometalinterFlags)
}
if *gometalinterConfig != "" {
args = append(args, fmt.Sprintf("--config=%s", *gometalinterConfig))
}
args = append(args, *gometalinterPath)
runCommand("gometalinter", args, *ignoreErrors, *verbose)
}
// runCommand is running a command and printing the stderr and stdout to stdout.
func runCommand(command string, args []string, ignoreErrors, verbose bool) {
cmd := exec.Command(command, args...)
if verbose {
fmt.Printf("Running command: %s %s\n", command, strings.Join(args, " "))
}
stdout, err := cmd.StdoutPipe()
if err != nil {
fmt.Fprintln(os.Stderr, "Could not open stdout pipe", err)
os.Exit(1)
}
stdoutScanner := bufio.NewScanner(stdout)
go func() {
for stdoutScanner.Scan() {
fmt.Println(stdoutScanner.Text())
}
}()
stderr, err := cmd.StderrPipe()
if err != nil {
fmt.Fprintln(os.Stderr, "Could not open stderr pipe", err)
os.Exit(1)
}
stderrScanner := bufio.NewScanner(stderr)
go func() {
for stderrScanner.Scan() {
fmt.Println(stderrScanner.Text())
}
}()
err = cmd.Start()
if err != nil {
fmt.Fprintln(os.Stderr, "Error running command", err)
os.Exit(1)
}
err = cmd.Wait()
if err != nil {
fmt.Fprintf(os.Stderr, "%s exited with errors: %s\n", command, err.Error())
if !ignoreErrors {
os.Exit(1)
}
}
}