-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathapps.go
More file actions
125 lines (91 loc) · 2.43 KB
/
apps.go
File metadata and controls
125 lines (91 loc) · 2.43 KB
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
package apps
import (
"encoding/json"
"fmt"
"strconv"
"strings"
"github.com/deis/deis/client-go/controller/api"
"github.com/deis/deis/client-go/controller/client"
)
// List lists apps on a Deis controller.
func List(c *client.Client) ([]api.App, error) {
body, err := c.BasicRequest("GET", "/v1/apps/", nil)
if err != nil {
return []api.App{}, err
}
apps := api.Apps{}
if err = json.Unmarshal([]byte(body), &apps); err != nil {
return []api.App{}, err
}
return apps.Apps, nil
}
// New creates a new app.
func New(c *client.Client, id string) (api.App, error) {
body := []byte{}
var err error
if id != "" {
req := api.AppCreateRequest{ID: id}
body, err = json.Marshal(req)
if err != nil {
return api.App{}, err
}
}
resBody, err := c.BasicRequest("POST", "/v1/apps/", body)
if err != nil {
return api.App{}, err
}
app := api.App{}
if err = json.Unmarshal([]byte(resBody), &app); err != nil {
return api.App{}, err
}
return app, nil
}
// Get app details from a Deis controller.
func Get(c *client.Client, appID string) (api.App, error) {
u := fmt.Sprintf("/v1/apps/%s/", appID)
body, err := c.BasicRequest("GET", u, nil)
if err != nil {
return api.App{}, err
}
app := api.App{}
if err = json.Unmarshal([]byte(body), &app); err != nil {
return api.App{}, err
}
return app, nil
}
// Logs retrieves logs from an app.
func Logs(c *client.Client, appID string, lines int) (string, error) {
u := fmt.Sprintf("/v1/apps/%s/logs", appID)
if lines > 0 {
u += "?log_lines=" + strconv.Itoa(lines)
}
body, err := c.BasicRequest("GET", u, nil)
if err != nil {
return "", err
}
return strings.Trim(body, `"`), nil
}
// Run one time command in an app.
func Run(c *client.Client, appID string, command string) (api.AppRunResponse, error) {
req := api.AppRunRequest{Command: command}
body, err := json.Marshal(req)
if err != nil {
return api.AppRunResponse{}, err
}
u := fmt.Sprintf("/v1/apps/%s/run", appID)
resBody, err := c.BasicRequest("POST", u, body)
if err != nil {
return api.AppRunResponse{}, err
}
out := make([]interface{}, 2)
if err = json.Unmarshal([]byte(resBody), &out); err != nil {
return api.AppRunResponse{}, err
}
return api.AppRunResponse{Output: out[1].(string), ReturnCode: int(out[0].(float64))}, nil
}
// Delete an app.
func Delete(c *client.Client, appID string) error {
u := fmt.Sprintf("/v1/apps/%s/", appID)
_, err := c.BasicRequest("DELETE", u, nil)
return err
}