/
connect.go
66 lines (56 loc) · 1.32 KB
/
connect.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
package cmd
import (
"log"
"os"
"github.com/spf13/cobra"
)
var hostName string
var taskName string
// ConnectCmd - Connect to a host.
var ConnectCmd = &cobra.Command{
Use: "to",
Short: "Connect to a host",
Long: "Connect to a host",
Run: func(cmd *cobra.Command, args []string) {
// Check that a host was specified.
if len(args) == 0 {
log.Fatal(errHostRequired)
}
// Set hostname.
hostName = args[0]
if len(args) == 1 {
// Connect to host.
err := connectToHost(cmd, hostName, "")
handleErr(err)
os.Exit(0)
}
// Find the subcommand.
subCmd, subArgs, err := cmd.Find(args[1:])
handleErr(err)
// If the subcommand is the same as the original command,
// then no new subcommand was found. In that case, exit
// with an error.
if subCmd.Use == cmd.Use {
log.Fatalf("Cannot parse subcommand %v\n", subArgs)
} else {
subCmd.Run(cmd, subArgs)
}
},
}
// TaskCmd - Run a task on a host.
var TaskCmd = &cobra.Command{
Use: "and",
Short: "Run a task on a host",
Long: "Run a task on a host",
Run: func(cmd *cobra.Command, args []string) {
// Check that a task was specified.
if len(args) == 0 {
log.Fatal(errTaskRequired)
}
// Set taskname.
taskName = args[0]
// Connect to host and run a command.
err := connectToHost(cmd, hostName, taskName)
handleErr(err)
},
}