-
Notifications
You must be signed in to change notification settings - Fork 2
/
make.go
59 lines (53 loc) · 1.31 KB
/
make.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
// Package target implements gnu make like target processing from yaml files
// and OS environment variables.
package target
import (
"fmt"
"sync"
"github.com/upsight/ron/color"
"github.com/upsight/ron/execute"
)
// MSS is an alias for map[string]string
type MSS map[string]string
// Make runs targets...like make kinda
type Make struct {
Configs *Configs
}
// NewMake creates a Make type with config embedded.
func NewMake(configs *Configs) (*Make, error) {
m := &Make{
Configs: configs,
}
return m, nil
}
// Run executes the given target name.
func (m *Make) Run(names ...string) error {
for _, name := range names {
target, ok := m.Configs.Target(name)
if !ok {
return fmt.Errorf("%s target not found", name)
}
if len(m.Configs.RemoteHosts) > 0 {
wg := &sync.WaitGroup{}
for _, h := range m.Configs.RemoteHosts {
wg.Add(1)
go func(host *execute.SSHConfig) {
defer wg.Done()
status, out, err := target.RunRemote(host)
if status != 0 || err != nil {
msg := fmt.Sprintf("%s] %d %s %v\n", host.Host, status, out, err)
m.Configs.StdErr.Write([]byte(color.Red(msg)))
return
}
}(h)
}
wg.Wait()
} else {
status, out, err := target.Run()
if status != 0 || err != nil {
return fmt.Errorf("%d %s %v", status, out, err)
}
}
}
return nil
}