-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli_commands.go
80 lines (73 loc) · 2.09 KB
/
cli_commands.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
package main
import (
"fmt"
"os"
"github.com/zshamrock/vmx/command"
"github.com/zshamrock/vmx/config"
"gopkg.in/urfave/cli.v1"
)
const profileArgName = "profile"
// GlobalFlags used
var GlobalFlags = []cli.Flag{
cli.StringFlag{
Name: fmt.Sprintf("%s, p", profileArgName),
Usage: "profile to use to read hosts and commands from",
},
}
func getProfile(c *cli.Context) string {
profile := c.GlobalString(profileArgName)
if profile == "" {
profile = os.Getenv("VMX_DEFAULT_PROFILE")
}
return profile
}
// Commands available
var Commands = []cli.Command{
{
Name: "run",
Usage: "Run custom command",
Description: `Example of usage is below:
run logs => run logs command defined in the ~/.vmx/commands`,
Action: command.CmdRun,
Flags: []cli.Flag{
cli.BoolFlag{
Name: fmt.Sprintf("%s, f", command.FollowArgName),
Usage: "flag indicates that the provided command will not exit, but will follow the output instead",
},
},
SkipFlagParsing: true,
BashComplete: func(c *cli.Context) {
var names []string
if c.NArg() == 0 || (c.NArg() == 1 && command.ContainsFollow(c)) {
names = config.GetHostNames()
} else {
names = config.GetCommandNames()
}
for _, name := range names {
fmt.Println(name)
}
},
},
{
Name: "list",
Usage: "List available custom commands",
Description: `Example of usage is below:
list => list available custom commands defined in the ~/.vmx/commands or in the corresponding profile if specified`,
Action: command.CmdList,
Flags: []cli.Flag{},
},
{
Name: "hosts",
Usage: "List available hosts",
Description: `Example of usage is below:
hosts => list available hosts defined in the ~/.vmx/hosts or in the corresponding profile if specified`,
Action: command.CmdHosts,
Flags: []cli.Flag{},
},
}
// CommandNotFound is used by cli to display an error message when unknown command is asked
func CommandNotFound(c *cli.Context, command string) {
fmt.Fprintf(os.Stderr, "%s: '%s' is not a %s command. See '%s --help'.\n", c.App.Name, command, c.App.Name, c.App.Name)
cli.ShowAppHelp(c)
os.Exit(2)
}