/
root.go
185 lines (155 loc) · 5.23 KB
/
root.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package root
import (
"fmt"
"os"
"path/filepath"
"github.com/BurntSushi/toml"
"github.com/bensallen/hkmgr/internal/config"
"github.com/bensallen/hkmgr/internal/console"
"github.com/bensallen/hkmgr/internal/destroy"
"github.com/bensallen/hkmgr/internal/down"
"github.com/bensallen/hkmgr/internal/ssh"
"github.com/bensallen/hkmgr/internal/status"
"github.com/bensallen/hkmgr/internal/up"
"github.com/integrii/flaggy"
"github.com/kr/pretty"
)
// Version is the CLI version or release number. To be overriden at build time.
var Version = "unknown"
// Run hkmgr
func Run() error {
var upSubcommand *flaggy.Subcommand
var downSubcommand *flaggy.Subcommand
var destroySubcommand *flaggy.Subcommand
var validateSubcommand *flaggy.Subcommand
var sshSubcommand *flaggy.Subcommand
var statusSubcommand *flaggy.Subcommand
var consoleSubcommand *flaggy.Subcommand
//
var cliConfigPaths []string
var debug bool
var dryRun bool
var vmName string
flaggy.SetName("hkmgr")
flaggy.SetDescription("VM manager for hyperkit")
flaggy.DefaultParser.AdditionalHelpPrepend = "http://github.com/bensallen/hkmgr"
flaggy.StringSlice(&cliConfigPaths, "c", "config", "Path to configuration TOML file or directory of TOML files")
flaggy.Bool(&debug, "d", "debug", "Enable debug output")
flaggy.Bool(&dryRun, "n", "dry-run", "Don't execute any commands that affect change, just show what will be run")
upSubcommand = flaggy.NewSubcommand("up")
upSubcommand.Description = "Start VMs"
upSubcommand.ShortName = "start"
upSubcommand.AddPositionalValue(&vmName, "name", 1, false, "Specify a VM, otherwise all VMs will be run")
downSubcommand = flaggy.NewSubcommand("down")
downSubcommand.Description = "Stop VMs"
downSubcommand.ShortName = "stop"
var downSignal string
downSubcommand.String(&downSignal, "s", "signal", "Signal to send to VM")
downSubcommand.AddPositionalValue(&vmName, "name", 1, false, "Specify a VM, otherwise all VMs will be stopped")
destroySubcommand = flaggy.NewSubcommand("destroy")
destroySubcommand.Description = "Destroy VMs"
destroySubcommand.AddPositionalValue(&vmName, "name", 1, false, "Specify a VM, otherwise all VMs will be destroyed!")
validateSubcommand = flaggy.NewSubcommand("validate")
validateSubcommand.Description = "Validate configuration"
validateSubcommand.AddPositionalValue(&vmName, "name", 1, false, "Specify a VM")
statusSubcommand = flaggy.NewSubcommand("status")
statusSubcommand.Description = "Display status of VMs"
statusSubcommand.AddPositionalValue(&vmName, "name", 1, false, "Specify a VM")
sshSubcommand = flaggy.NewSubcommand("ssh")
sshSubcommand.Description = "SSH to VM"
sshSubcommand.AddPositionalValue(&vmName, "name", 1, true, "Specify a VM")
consoleSubcommand = flaggy.NewSubcommand("console")
consoleSubcommand.Description = "Open Console of VM"
consoleSubcommand.AddPositionalValue(&vmName, "name", 1, true, "Specify a VM")
flaggy.AttachSubcommand(upSubcommand, 1)
flaggy.AttachSubcommand(downSubcommand, 1)
//flaggy.AttachSubcommand(destroySubcommand, 1)
//flaggy.AttachSubcommand(validateSubcommand, 1)
flaggy.AttachSubcommand(statusSubcommand, 1)
//flaggy.AttachSubcommand(sshSubcommand, 1)
//flaggy.AttachSubcommand(consoleSubcommand, 1)
flaggy.SetVersion(Version)
flaggy.Parse()
if dryRun {
debug = true
}
// Default to look for configs hkmgr.toml and hkmgr.d/*.toml
if len(cliConfigPaths) == 0 {
cliConfigPaths = []string{"hkmgr.toml", "hkmgr.d"}
}
cfgPaths := []string{}
var firstCfgPath string
for _, cliConfigPath := range cliConfigPaths {
cfgStat, err := os.Stat(cliConfigPath)
if os.IsNotExist(err) {
continue
}
if cfgStat.IsDir() {
globPaths, err := filepath.Glob(cliConfigPath + "/*.toml")
if err != nil {
fmt.Printf("configuration path %s is a directory and failed with %v", cliConfigPath, err)
continue
}
if len(globPaths) == 0 {
continue
}
cfgPaths = append(cfgPaths, globPaths...)
} else {
cfgPaths = append(cfgPaths, cliConfigPath)
}
// Record the first valid path so that it can be used to resolve relative paths
if firstCfgPath == "" {
firstCfgPath = cliConfigPath
}
}
if len(cfgPaths) == 0 {
return fmt.Errorf("no configuration files found")
}
var config config.Config
for _, cfgPath := range cfgPaths {
if _, err := toml.DecodeFile(cfgPath, &config); err != nil {
return err
}
}
absPath, err := filepath.Abs(firstCfgPath)
if err != nil {
return err
}
config.Path = absPath
if err := config.Defaults(); err != nil {
return err
}
config.UpdateRelativePaths()
if debug {
fmt.Printf("Parsed config:\n\n%# v\n", pretty.Formatter(config))
}
switch {
case upSubcommand.Used:
if err := up.Run(&config, vmName, debug, dryRun); err != nil {
return err
}
case downSubcommand.Used:
if err := down.Run(&config, vmName, downSignal); err != nil {
return err
}
case destroySubcommand.Used:
if err := destroy.Run(&config); err != nil {
return err
}
case statusSubcommand.Used:
if err := status.Current(&config, vmName, debug); err != nil {
return err
}
case sshSubcommand.Used:
if err := ssh.Run(&config); err != nil {
return err
}
case consoleSubcommand.Used:
if err := console.Run(&config); err != nil {
return err
}
default:
flaggy.ShowHelpAndExit("")
}
return nil
}