forked from Telmate/proxmox-api-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
95 lines (78 loc) · 2.72 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package main
import (
"github.com/3coma3/proxmox-api-go/test"
"flag"
"fmt"
"os"
"strconv"
)
func main() {
var (
err error
options test.TOptions = test.TOptions{
Action: "",
VMid: 0,
VMname: "",
APIurl: os.Getenv("PM_API_URL"),
APIuser: os.Getenv("PM_USER"),
APIpass: os.Getenv("PM_PASS"),
APIinsecure: false,
}
fvmid = flag.Int("vmid", options.VMid, "custom vmid (instead of auto)")
fdebug = flag.Bool("debug", false, "debug mode")
finsecure = flag.Bool("insecure", options.APIinsecure, "TLS insecure mode")
)
flag.Parse()
if len(flag.Args()) == 0 {
fmt.Printf("Usage: %s [flags] [action] [vmid|vname] [host]\n", os.Args[0])
fmt.Printf("\nFlags: \n")
flag.PrintDefaults()
os.Exit(1)
}
// used by test.DebugMsg
test.Debug = *fdebug
test.DebugMsg("-insecure is " + strconv.FormatBool(*finsecure))
test.DebugMsg("-debug is " + strconv.FormatBool(*fdebug))
test.DebugMsg("-fvmid is " + strconv.Itoa(*fvmid))
for i, v := range flag.Args() {
test.DebugMsg("flag.Args()[" + strconv.Itoa(i) + "] is " + v)
}
options.Action = flag.Args()[0]
// Try to get VMid from the second (non-flag) positional parameter
// If that fails (argument is not numeric) or -vmid was specified, just
// set the field to whatever *fvmid is and assume the argument is a VMname
// This implies that -vmid wins when both it and the positional parameter
// are present to set the VMid
// The "extra parameter" could be also set here (see below)
if len(flag.Args()) > 1 {
if options.VMid, err = strconv.Atoi(flag.Args()[1]); *fvmid > 0 || err != nil {
options.VMid = *fvmid
options.VMname = flag.Args()[1]
}
} else {
options.VMid = *fvmid
}
// for other parameters than vm identifications I refrain to do more checks
// to avoid spending time in a CLI until we decide to go with this codebase
// copying the arguments array to the options
// a good solution might be something like https://github.com/mitchellh/mapstructure
// or directly https://github.com/urfave/cli
options.Args = flag.Args()
options.APIinsecure = *finsecure
// Other validations could be done here, like having extra positional
// parameters beyond 3 (action, vmid/vname, node) or having an action that
// actually cares about what has been passed
// Ignoring these conditions is simpler and won't break things, though
test.DebugMsg("Running test: " + options.Action)
veredict := "Test " + options.Action + " "
if err = test.Run(&options); err == nil {
test.DebugMsg(veredict + "PASSED")
os.Exit(0)
} else {
if msg := err.Error(); msg != "" {
test.DebugMsg("Test " + options.Action + " returned the error message: \"" + msg + "\"")
}
test.DebugMsg(veredict + "FAILED")
os.Exit(1)
}
}