This repository has been archived by the owner on Nov 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
group.go
161 lines (141 loc) · 3.98 KB
/
group.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
package main
// All actions under command group
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/url"
"os"
"strconv"
)
type GroupList struct {
client *Client
format Formatter
}
func (g GroupList) Apply(args []string) {
switch len(args) {
case 0:
g.listGroups("")
case 1:
g.listGroups(args[0])
default:
Check(false, "expected 0 or 1 argument")
}
}
func (g GroupList) listGroups(groupid string) {
path := "/v2/groups"
if groupid != "" {
path += "/" + url.QueryEscape(groupid)
}
request := g.client.GET(path)
response, e := g.client.Do(request)
Check(e == nil, "failed to get response", e)
defer response.Body.Close()
fmt.Println(g.format.Format(response.Body, g.Humanize))
}
func (g GroupList) Humanize(body io.Reader) string {
dec := json.NewDecoder(body)
var root Group
e := dec.Decode(&root)
Check(e == nil, "failed to unmarshal response", e)
return columnizeGroup(&root)
}
func columnizeGroup(group *Group) string {
title := "GROUPID VERSION GROUPS APPS\n"
var b bytes.Buffer
gatherGroup(group, &b)
text := title + b.String()
return Columnize(text)
}
func gatherGroup(g *Group, b *bytes.Buffer) {
b.WriteString(g.GroupID)
b.WriteString(" ")
b.WriteString(g.Version)
b.WriteString(" ")
b.WriteString(strconv.Itoa(len(g.Groups)))
b.WriteString(" ")
b.WriteString(strconv.Itoa(len(g.Apps)))
b.WriteString("\n")
for _, group := range g.Groups {
gatherGroup(group, b)
}
}
type GroupCreate struct {
client *Client
format Formatter
}
func (g GroupCreate) Apply(args []string) {
Check(len(args) == 1, "must supply 1 jsonfile")
f, e := os.Open(args[0])
Check(e == nil, "failed to open jsonfile", e)
defer f.Close()
request := g.client.POST("/v2/groups", f)
response, e := g.client.Do(request)
Check(e == nil, "failed to get response")
defer response.Body.Close()
Check(response.StatusCode != 409, "group already exists")
fmt.Println(g.format.Format(response.Body, g.Humanize))
}
func (g GroupCreate) Humanize(body io.Reader) string {
dec := json.NewDecoder(body)
var update Update
e := dec.Decode(&update)
Check(e == nil, "failed to decode response", e)
title := "DEPLOYID VERSION\n"
text := title + update.DeploymentID + " " + update.Version
return Columnize(text)
}
type GroupDestroy struct {
client *Client
format Formatter
}
func (g GroupDestroy) Apply(args []string) {
Check(len(args) == 1, "must specify groupid")
groupid := url.QueryEscape(args[0])
path := "/v2/groups/" + groupid + "?force=true"
request := g.client.DELETE(path)
response, e := g.client.Do(request)
Check(e == nil, "destroy group failed", e)
defer response.Body.Close()
c := response.StatusCode
Check(c != 404, "unknown group")
Check(c == 200, "destroy group bad status", c, g.format.Format(response.Body, g.Humanize))
fmt.Println(g.format.Format(response.Body, g.Humanize))
}
func (g GroupDestroy) Humanize(body io.Reader) string {
dec := json.NewDecoder(body)
var versionmap map[string]string // ugh
e := dec.Decode(&versionmap)
Check(e == nil, "failed to decode response", e)
v, ok := versionmap["version"]
Check(ok, "version missing")
return "VERSION\n" + v
}
type GroupUpdate struct {
client *Client
format Formatter
}
func (g GroupUpdate) Apply(args []string) {
Check(len(args) == 2, "must specify groupid and jsonfile")
groupid := url.QueryEscape(args[0])
f, e := os.Open(args[1])
Check(e == nil, "failed to open jsonfile", e)
defer f.Close()
request := g.client.PUT("/v2/groups/"+groupid+"?force=true", f)
response, e := g.client.Do(request)
Check(e == nil, "failed to get response", e)
defer response.Body.Close()
sc := response.StatusCode
Check(sc == 200, "bad status code", sc, g.format.Format(response.Body, g.Humanize))
fmt.Println(g.format.Format(response.Body, g.Humanize))
}
func (g GroupUpdate) Humanize(body io.Reader) string {
dec := json.NewDecoder(body)
var update Update
e := dec.Decode(&update)
Check(e == nil, "failed to decode response", e)
title := "DEPLOYID VERSION\n"
text := title + update.DeploymentID + " " + update.Version
return Columnize(text)
}