forked from stahnma/vmpool-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdelete.go
49 lines (46 loc) · 953 Bytes
/
delete.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"strings"
)
var cmdDelete = &Command{
Run: runDelete,
UsageLine: "delete [hostname]",
Short: "delete a vm from vmpooler",
Long: `
Delete removes a vm with the given hostname
from vmpooler.
`,
}
func runDelete(cmd *Command, args []string) {
if len(args) < 1 {
cmd.Usage()
}
for _, arg := range args {
// Truncate the domain if required
if matchPattern("delivery.puppetlabs.net")(arg) {
arg = strings.Split(arg, ".")[0]
}
resp, err := Request("DELETE", arg, "{}")
if err != nil {
log.Printf("%v\n", err)
os.Exit(1)
}
defer resp.Body.Close()
contents, err := ioutil.ReadAll(resp.Body)
perror(err)
var output_json map[string]bool
err = json.Unmarshal(contents, &output_json)
perror(err)
if !output_json["ok"] {
log.Printf("Host %s not found.\n", arg)
os.Exit(1)
} else {
fmt.Printf("%s deleted\n", arg)
}
}
}