-
Notifications
You must be signed in to change notification settings - Fork 8
/
stacks.go
71 lines (55 loc) · 1.55 KB
/
stacks.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
package scalingo
import (
"context"
"time"
"gopkg.in/errgo.v1"
httpclient "github.com/Scalingo/go-scalingo/v7/http"
)
type DeprecationDate struct {
time.Time
}
type Stack struct {
ID string `json:"id"`
CreatedAt time.Time `json:"created_at"`
Name string `json:"name"`
Description string `json:"description"`
BaseImage string `json:"base_image"`
Default bool `json:"default"`
DeprecatedAt DeprecationDate `json:"deprecated_at,omitempty"`
}
type StacksService interface {
StacksList(ctx context.Context) ([]Stack, error)
}
var _ StacksService = (*Client)(nil)
func (c *Client) StacksList(ctx context.Context) ([]Stack, error) {
req := &httpclient.APIRequest{
Endpoint: "/features/stacks",
}
resmap := map[string][]Stack{}
err := c.ScalingoAPI().DoRequest(ctx, req, &resmap)
if err != nil {
return nil, errgo.Notef(err, "fail to request Scalingo API")
}
return resmap["stacks"], nil
}
// The regional API returns a date formatted as "2006-01-02"
// Go standard library does not unmarshal that format
func (deprecationDate *DeprecationDate) UnmarshalJSON(b []byte) error {
s := string(b)
if s == "null" {
// When there is no deprecation date for a stack, the json will look like: {"deprecated_at": null}
return nil
}
t, err := time.Parse(`"2006-01-02"`, s)
if err != nil {
return err
}
deprecationDate.Time = t
return nil
}
func (s *Stack) IsDeprecated() bool {
if s.DeprecatedAt.IsZero() {
return false
}
return time.Now().After(s.DeprecatedAt.Time)
}