forked from goss-org/goss
/
runner.go
100 lines (85 loc) · 1.73 KB
/
runner.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
package goss
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"runtime"
"sync"
"time"
"github.com/aelsabbahy/goss/outputs"
"github.com/aelsabbahy/goss/resource"
"github.com/aelsabbahy/goss/system"
"github.com/codegangsta/cli"
"github.com/fatih/color"
)
func Run(c *cli.Context, startTime time.Time) {
sys := system.New(c)
// handle stdin
var fh *os.File
var err error
var path string
if !c.GlobalIsSet("gossfile") && hasStdin() {
fh = os.Stdin
} else {
specFile := c.GlobalString("gossfile")
path = filepath.Dir(specFile)
fh, err = os.Open(specFile)
if err != nil {
fmt.Printf("Error: %v\n", err)
os.Exit(1)
}
}
data, err := ioutil.ReadAll(fh)
if err != nil {
fmt.Printf("Error: %v\n", err)
os.Exit(1)
}
configJSON := mergeJSONData(ReadJSONData(data), 0, path)
out := make(chan []resource.TestResult)
in := make(chan resource.Resource)
go func() {
for _, t := range configJSON.Resources() {
in <- t
}
close(in)
}()
if os.Getenv("GOMAXPROCS") == "" {
runtime.GOMAXPROCS(runtime.NumCPU())
}
gomaxprocs := runtime.GOMAXPROCS(-1)
workerCount := gomaxprocs * 5
if workerCount > 50 {
workerCount = 50
}
var wg sync.WaitGroup
for i := 0; i < workerCount; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for f := range in {
out <- f.Validate(sys)
}
}()
}
go func() {
wg.Wait()
close(out)
}()
//var outputer outputs.Outputer
if c.Bool("no-color") {
color.NoColor = true
}
outputer := outputs.GetOutputer(c.String("format"))
exitCode := outputer.Output(out, startTime)
os.Exit(exitCode)
}
func hasStdin() bool {
if fi, err := os.Stdin.Stat(); err == nil {
mode := fi.Mode()
if (mode&os.ModeNamedPipe != 0) || mode.IsRegular() {
return true
}
}
return false
}