-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.go
105 lines (80 loc) · 2.01 KB
/
list.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
package main
import (
"fmt"
"os"
"text/tabwriter"
)
// Help function
func listHelp() {
fmt.Println("Usage: proxmox list [options]")
fmt.Println("List Virtual Machines")
fmt.Println("Options:")
fmt.Println(" -h, --help")
fmt.Println(" -n, --node <node>")
fmt.Println(" -t, --template")
fmt.Println("Examples:")
fmt.Println(" proxmox list")
}
// List operation
func list() {
// get options from parameters
params := os.Args[2:]
if checkParams(params, "help", false) {
listHelp()
os.Exit(0)
}
// if leng of parameters is less than 1, print all VMs
if len(params) < 1 {
listAllNodes()
return
}
// if length of parameters is 1+ get the --node parameter
if checkParams(params, "node", true) {
listNode(params[1])
return
}
// if length of parameters is 1+ get the --template parameter
if checkParams(params, "template", false) {
listTemplates()
return
}
logger.Fatal("Invalid parameters")
}
// List single node virtual machines
func listNode(node string) {
}
// List all virtual machines
func listAllNodes() {
// create tabwriter
tabWriter := new(tabwriter.Writer)
tabWriter.Init(os.Stdout, 5, 8, 1, '\t', 0)
// print header
fmt.Fprintln(tabWriter, "Node\tVMID\tName\tStatus\tTags\t")
fmt.Fprintln(tabWriter, "----\t----\t----\t------\t----\t")
// loop nodes
for _, node := range nodes {
// loop virtual machines
for _, vm := range node.VirtualMachines {
// print virtual machine
fmt.Fprintf(tabWriter, "%s\t%d\t%s\t%s\t%s\t\n", node.Name, vm.VMID, vm.Name, vm.Status, vm.Tags)
}
}
// flush tabwriter
tabWriter.Flush()
}
// List all templates
func listTemplates() {
templates := getTemplates()
// create tabwriter
tabWriter := new(tabwriter.Writer)
tabWriter.Init(os.Stdout, 5, 8, 1, '\t', 0)
// print header
fmt.Fprintln(tabWriter, "Node\tVMID\tName\t")
// loop templates
for _, template := range templates {
// print template
fmt.Fprintf(tabWriter, "%s\t%d\t%s\t\n", template.Node, template.VMID, template.Name)
}
// flush tabwriter
tabWriter.Flush()
}