-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
73 lines (60 loc) · 1.33 KB
/
main.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
package main
import (
"fmt"
"log"
"os"
"sync"
"github.com/pborman/getopt"
)
func main() {
help := getopt.BoolLong("help", 'h', "display this help")
filter := getopt.StringLong("filter", 'f', "", "filter")
command := getopt.StringLong("command", 'c', "", "command to run, if empty will print the list of hosts")
project := getopt.StringLong("project", 'p', "", "project")
port := getopt.StringLong("port", 't', "22", "optional port, default: 22")
user := getopt.StringLong("user", 'u', "", "optional user, otherwise will read from local configuration")
getopt.Parse()
if *help {
getopt.Usage()
os.Exit(0)
}
search, err := parseArgs(*project, *command)
if err != nil {
getopt.Usage()
log.Fatal(err)
}
var k Knife
t, err := getTarget(*filter, *project)
if err != nil {
log.Fatal(err)
}
k = Knife{
targets: t,
user: *user,
command: *command,
}
if search {
k.Print()
os.Exit(0)
}
var wg sync.WaitGroup
for _, t := range k.targets {
wg.Add(1)
k.user = getUser(t.hostname, *user)
go runCommand(t.hostname, k.command, k.user, *port, &wg)
}
wg.Wait()
os.Exit(0)
}
func parseArgs(p, c string) (bool, error) {
if len(p) == 0 {
return false, fmt.Errorf("Argument project cannot be empty")
}
if len(c) == 0 {
return true, nil
}
return false, nil
}
// TODO
// use channels
// add more tests