-
Notifications
You must be signed in to change notification settings - Fork 301
/
meta_data.go
97 lines (76 loc) · 2.26 KB
/
meta_data.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
package api
import (
"context"
"errors"
"fmt"
)
// MetaData represents a Buildkite Agent API MetaData
type MetaData struct {
Key string `json:"key,omitempty"`
Value string `json:"value,omitempty"`
}
// MetaDataExists represents a Buildkite Agent API MetaData Exists check
// response
type MetaDataExists struct {
Exists bool `json:"exists"`
}
// Sets the meta data value
func (c *Client) SetMetaData(ctx context.Context, jobId string, metaData *MetaData) (*Response, error) {
u := fmt.Sprintf("jobs/%s/data/set", railsPathEscape(jobId))
req, err := c.newRequest(ctx, "POST", u, metaData)
if err != nil {
return nil, err
}
return c.doRequest(req, nil)
}
// Gets the meta data value
func (c *Client) GetMetaData(ctx context.Context, scope, id, key string) (*MetaData, *Response, error) {
if scope != "job" && scope != "build" {
return nil, nil, errors.New("scope must either be job or build")
}
u := fmt.Sprintf("%ss/%s/data/get", scope, railsPathEscape(id))
m := &MetaData{Key: key}
req, err := c.newRequest(ctx, "POST", u, m)
if err != nil {
return nil, nil, err
}
resp, err := c.doRequest(req, m)
if err != nil {
return nil, resp, err
}
return m, resp, err
}
// Returns true if the meta data key has been set, false if it hasn't.
func (c *Client) ExistsMetaData(ctx context.Context, scope, id, key string) (*MetaDataExists, *Response, error) {
if scope != "job" && scope != "build" {
return nil, nil, errors.New("scope must either be job or build")
}
u := fmt.Sprintf("%ss/%s/data/exists", scope, railsPathEscape(id))
m := &MetaData{Key: key}
req, err := c.newRequest(ctx, "POST", u, m)
if err != nil {
return nil, nil, err
}
e := new(MetaDataExists)
resp, err := c.doRequest(req, e)
if err != nil {
return nil, resp, err
}
return e, resp, err
}
func (c *Client) MetaDataKeys(ctx context.Context, scope, id string) ([]string, *Response, error) {
if scope != "job" && scope != "build" {
return nil, nil, errors.New("scope must either be job or build")
}
u := fmt.Sprintf("%ss/%s/data/keys", scope, railsPathEscape(id))
req, err := c.newRequest(ctx, "POST", u, nil)
if err != nil {
return nil, nil, err
}
keys := []string{}
resp, err := c.doRequest(req, &keys)
if err != nil {
return nil, resp, err
}
return keys, resp, err
}