-
Notifications
You must be signed in to change notification settings - Fork 8
/
run.go
60 lines (50 loc) · 1.32 KB
/
run.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
package scalingo
import (
"context"
"encoding/json"
"strings"
"github.com/Scalingo/go-scalingo/v7/http"
errors "github.com/Scalingo/go-utils/errors/v2"
)
type RunsService interface {
Run(ctx context.Context, opts RunOpts) (*RunRes, error)
}
var _ RunsService = (*Client)(nil)
type RunOpts struct {
App string
Command []string
Env map[string]string
Size string
Detached bool
HasUploads bool
}
type RunRes struct {
Container *Container `json:"container"`
AttachURL string `json:"attach_url"`
OperationURL string `json:"-"`
}
func (c *Client) Run(ctx context.Context, opts RunOpts) (*RunRes, error) {
req := &http.APIRequest{
Method: "POST",
Endpoint: "/apps/" + opts.App + "/run",
Params: map[string]interface{}{
"command": strings.Join(opts.Command, " "),
"env": opts.Env,
"size": opts.Size,
"detached": opts.Detached,
"has_uploads": opts.HasUploads,
},
}
res, err := c.ScalingoAPI().Do(ctx, req)
if err != nil {
return nil, errors.Notef(ctx, err, "request endpoint %v", req.Endpoint)
}
defer res.Body.Close()
var runRes RunRes
err = json.NewDecoder(res.Body).Decode(&runRes)
if err != nil {
return nil, errors.Notef(ctx, err, "decode response body")
}
runRes.OperationURL = res.Header.Get("Location")
return &runRes, nil
}