-
Notifications
You must be signed in to change notification settings - Fork 2
/
command_units.go
91 lines (76 loc) · 1.87 KB
/
command_units.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
package main
import (
"fmt"
"net/url"
"github.com/1and1/soma/internal/cmpl"
"github.com/1and1/soma/lib/proto"
"github.com/codegangsta/cli"
)
func registerUnits(app cli.App) *cli.App {
app.Commands = append(app.Commands,
[]cli.Command{
{
Name: "units",
Usage: "SUBCOMMANDS for metric units",
Subcommands: []cli.Command{
{
Name: "create",
Usage: "Create a new metric unit",
Action: runtime(cmdUnitCreate),
BashComplete: cmpl.Name,
},
{
Name: "delete",
Usage: "Delete a metric unit",
Action: runtime(cmdUnitDelete),
},
{
Name: "list",
Usage: "List metric units",
Action: runtime(cmdUnitList),
},
{
Name: "show",
Usage: "Show details about a metric unit",
Action: runtime(cmdUnitShow),
},
},
},
}...,
)
return &app
}
func cmdUnitCreate(c *cli.Context) error {
utl.ValidateCliArgumentCount(c, 3)
key := []string{"name"}
opts := utl.ParseVariadicArguments(key, key, key, c.Args().Tail())
req := proto.Request{}
req.Unit = &proto.Unit{}
req.Unit.Unit = c.Args().First()
req.Unit.Name = opts["name"][0]
resp := utl.PostRequestWithBody(Client, req, "/units/")
fmt.Println(resp)
return nil
}
func cmdUnitDelete(c *cli.Context) error {
utl.ValidateCliArgumentCount(c, 1)
esc := url.QueryEscape(c.Args().First())
path := fmt.Sprintf("/units/%s", esc)
resp := utl.DeleteRequest(Client, path)
fmt.Println(resp)
return nil
}
func cmdUnitList(c *cli.Context) error {
resp := utl.GetRequest(Client, "/units/")
fmt.Println(resp)
return nil
}
func cmdUnitShow(c *cli.Context) error {
utl.ValidateCliArgumentCount(c, 1)
esc := url.QueryEscape(c.Args().First())
path := fmt.Sprintf("/units/%s", esc)
resp := utl.GetRequest(Client, path)
fmt.Println(resp)
return nil
}
// vim: ts=4 sw=4 sts=4 noet fenc=utf-8 ffs=unix