-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.go
123 lines (114 loc) · 3.03 KB
/
run.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package main
import (
"container/list"
"fmt"
"path/filepath"
"github.com/grubernaut/gdsh/dsh"
"gopkg.in/urfave/cli.v2"
)
// ConfDir is the default configuration directory
var ConfDir = "/usr/local/etc/dsh"
// ConfPath is the filepath to the default machine list file
var ConfPath = filepath.Join(ConfDir, "machines.list")
func run(c *cli.Context) error {
// TODO: Not actually an rule. Need to remove
if c.NArg() < 1 {
fmt.Printf("Error: expected an execution argument\n")
return cli.ShowAppHelp(c)
}
// Create ExecOpts
opts := dsh.ExecOpts{}
// Create an empty machineList, and initialize struct
machineList := list.New()
opts.MachineList = machineList
// Set opts.Verbose output
opts.Verbose = false
if c.Bool("verbose") {
fmt.Printf("Verbose flag on\n")
opts.Verbose = true
}
if c.Bool("quiet") {
fmt.Printf("verbose flag off\n")
}
// Build up exec opts
if c.Bool("all") {
if opts.Verbose {
fmt.Printf("Adding all machines to the list\n")
}
if err := opts.ReadMachineList(ConfPath); err != nil {
_ = cli.Exit(fmt.Sprintf("Error adding all machines: %s", err), 1)
}
}
if c.String("group") != "" {
if opts.Verbose {
fmt.Printf("Adding group %s to the list\n", c.String("group"))
}
}
if c.String("file") != "" {
if opts.Verbose {
fmt.Printf("Adding file %s to the list\n", c.String("file"))
}
if err := opts.ReadMachineList(c.String("file")); err != nil {
_ = cli.Exit(fmt.Sprintf("Error adding machines from %s: %s",
c.String("file"), err), 1)
}
}
if c.Bool("show-machine-names") {
if opts.Verbose {
fmt.Printf("Show machine names on output\n")
}
opts.ShowNames = true
}
if c.Bool("hide-machine-names") {
if opts.Verbose {
fmt.Printf("Don't show machine names on output\n")
}
opts.ShowNames = false
}
if c.String("machine") != "" {
if opts.Verbose {
fmt.Printf("Adding machine %s to list\n", c.String("machine"))
}
}
if c.String("remoteshell") != "" {
if opts.Verbose {
fmt.Printf("Using %s as remote shell\n", c.String("remoteshell"))
}
if c.String("remoteshell") == "ssh" || c.String("remoteshell") == "rsh" {
opts.RemoteShell = c.String("remoteshell")
} else {
return cli.Exit("gdsh only supports 'ssh' and 'rsh' currently", 1)
}
}
if c.String("remoteshellopt") != "" {
if opts.Verbose {
fmt.Printf("Adding [%s] to shell options\n", c.String("remoteshellopt"))
}
opts.RemoteCommandOpts = c.String("remoteshellopt")
}
if c.Bool("wait-shell") {
if opts.Verbose {
fmt.Printf("Wait for shell to finish executing\n")
}
opts.ConcurrentShell = false
}
if c.Bool("concurrent-shell") {
if opts.Verbose {
fmt.Printf("Do not wait for shell to finish\n")
}
opts.ConcurrentShell = true
}
// Set remote command to execute
for _, v := range c.Args().Slice() {
if opts.RemoteCommand == "" && v != "" {
opts.RemoteCommand = v
continue
}
opts.RemoteCommand = fmt.Sprintf("%s %s", opts.RemoteCommand, v)
}
// Execute
if err := opts.Execute(); err != nil {
return cli.Exit(fmt.Sprintf("Error executing: %s", err), 1)
}
return nil
}