forked from Scalingo/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
env.go
81 lines (67 loc) · 1.72 KB
/
env.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
package api
import "github.com/Scalingo/cli/Godeps/_workspace/src/gopkg.in/errgo.v1"
const (
EnvNameMaxLength = 64
EnvValueMaxLength = 1024
)
type Variable struct {
ID string `json:"id"`
Name string `json:"name"`
Value string `json:"value"`
}
type Variables []*Variable
func (vs Variables) Contains(name string) (*Variable, bool) {
for _, v := range vs {
if v.Name == name {
return v, true
}
}
return nil, false
}
type VariablesRes struct {
Variables Variables `json:"variables"`
}
type VariableSetParams struct {
Variable *Variable `json:"variable"`
}
func VariablesList(app string) (Variables, error) {
return variableList(app, true)
}
func VariablesListWithoutAlias(app string) (Variables, error) {
return variableList(app, false)
}
func variableList(app string, aliases bool) (Variables, error) {
var variablesRes VariablesRes
err := subresourceList(app, "variables", map[string]bool{"aliases": aliases}, &variablesRes)
if err != nil {
return nil, errgo.Mask(err, errgo.Any)
}
return variablesRes.Variables, nil
}
func VariableSet(app string, name string, value string) (*Variable, int, error) {
req := &APIRequest{
Method: "POST",
Endpoint: "/apps/" + app + "/variables",
Params: map[string]interface{}{
"variable": map[string]string{
"name": name,
"value": value,
},
},
Expected: Statuses{200, 201},
}
res, err := req.Do()
if err != nil {
return nil, 0, errgo.Mask(err, errgo.Any)
}
defer res.Body.Close()
var params VariableSetParams
err = ParseJSON(res, ¶ms)
if err != nil {
return nil, 0, errgo.Mask(err, errgo.Any)
}
return params.Variable, res.StatusCode, nil
}
func VariableUnset(app string, id string) error {
return subresourceDelete(app, "variables", id)
}