forked from monzo/orchestra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.go
53 lines (45 loc) · 1.24 KB
/
build.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
package commands
import (
"bytes"
"fmt"
"os/exec"
"runtime"
"strings"
"github.com/urfave/cli/v2"
"github.com/wsxiaoys/terminal"
"github.com/tifo/orchestra/services"
)
var BuildCommand = &cli.Command{
Name: "build",
Usage: "Build service(s)",
Action: BeforeAfterWrapper(BuildAction),
}
func BuildAction(c *cli.Context) error {
worker := func(service *services.Service) func() {
return func() {
buildService(c, service)
}
}
pool := make(workerPool, runtime.NumCPU())
svcs := services.Sort(FilterServices(c))
for _, service := range svcs {
pool.Do(worker(service))
}
pool.Drain()
return nil
}
func buildService(c *cli.Context, service *services.Service) {
spacing := strings.Repeat(" ", services.MaxServiceNameLength+2-len(service.Name))
cmd := exec.Command("nice", "-n", niceness, "go", "build", "-v")
cmd.Dir = service.Path
output := new(bytes.Buffer)
cmd.Stdout = output
cmd.Stderr = output
if cmd.Run() != nil {
outputStr := output.String()
appendError(fmt.Errorf("Failed to build service %s\n%s", service.Name, outputStr))
terminal.Stdout.Colorf("%s%s| @{r} error: @{|}Failed to build: %s\n", service.Name, spacing, outputStr)
} else {
terminal.Stdout.Colorf("%s%s| @{g} (re)built\n", service.Name, spacing)
}
}