forked from monzo/orchestra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.go
65 lines (58 loc) · 1.64 KB
/
install.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
package commands
import (
"bytes"
"fmt"
"os/exec"
"runtime"
"strings"
"github.com/urfave/cli/v2"
"github.com/wsxiaoys/terminal"
"github.com/tifo/orchestra/services"
)
var InstallCommand = &cli.Command{
Name: "install",
Usage: "Installs all the services",
Action: BeforeAfterWrapper(InstallAction),
}
// InstallAction installs all the services (or the specified ones)
func InstallAction(c *cli.Context) error {
worker := func(service *services.Service) func() {
return func() {
spacing := strings.Repeat(" ", services.MaxServiceNameLength+2-len(service.Name))
rebuilt, err := installService(service)
if err != nil {
appendError(err)
terminal.Stdout.Colorf("%s%s| @{r} error: @{|}%v\n", service.Name, spacing, err)
} else if rebuilt {
terminal.Stdout.Colorf("%s%s| @{g} installed\n", service.Name, spacing)
} else {
terminal.Stdout.Colorf("%s%s| @{g} already up to date\n", service.Name, spacing)
}
}
}
pool := make(workerPool, runtime.NumCPU())
for _, service := range FilterServices(c) {
pool.Do(worker(service))
}
pool.Drain()
return nil
}
// installService runs go install in the service directory
func installService(service *services.Service) (bool, error) {
cmd := exec.Command("nice", "-n", niceness, "go", "install", "-v")
cmd.Dir = service.Path
output := bytes.NewBuffer([]byte{})
cmd.Stdout = output
cmd.Stderr = output
err := cmd.Start()
if err != nil {
return false, err
}
_ = cmd.Wait()
if !cmd.ProcessState.Success() {
return false, fmt.Errorf("Failed to install service %s\n%s", service.Name, output.String())
} else if output.Len() > 0 {
return true, nil
}
return false, nil
}