forked from buildkite/agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
meta_data_get.go
117 lines (99 loc) · 3.06 KB
/
meta_data_get.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package clicommand
import (
"fmt"
"time"
"github.com/buildkite/agent/agent"
"github.com/buildkite/agent/api"
"github.com/buildkite/agent/cliconfig"
"github.com/buildkite/agent/logger"
"github.com/buildkite/agent/retry"
"github.com/urfave/cli"
)
var MetaDataGetHelpDescription = `Usage:
buildkite-agent meta-data get <key> [arguments...]
Description:
Get data from a builds key/value store.
Example:
$ buildkite-agent meta-data get "foo"`
type MetaDataGetConfig struct {
Key string `cli:"arg:0" label:"meta-data key" validate:"required"`
Default string `cli:"default"`
Job string `cli:"job" validate:"required"`
AgentAccessToken string `cli:"agent-access-token" validate:"required"`
Endpoint string `cli:"endpoint" validate:"required"`
NoColor bool `cli:"no-color"`
Debug bool `cli:"debug"`
DebugHTTP bool `cli:"debug-http"`
}
var MetaDataGetCommand = cli.Command{
Name: "get",
Usage: "Get data from a build",
Description: MetaDataGetHelpDescription,
Flags: []cli.Flag{
cli.StringFlag{
Name: "default",
Value: "",
Usage: "If the meta-data value doesn't exist return this instead",
},
cli.StringFlag{
Name: "job",
Value: "",
Usage: "Which job should the meta-data be retrieved from",
EnvVar: "BUILDKITE_JOB_ID",
},
AgentAccessTokenFlag,
EndpointFlag,
NoColorFlag,
DebugFlag,
DebugHTTPFlag,
},
Action: func(c *cli.Context) {
// The configuration will be loaded into this struct
cfg := MetaDataGetConfig{}
// Load the configuration
if err := cliconfig.Load(c, &cfg); err != nil {
logger.Fatal("%s", err)
}
// Setup the any global configuration options
HandleGlobalFlags(cfg)
// Create the API client
client := agent.APIClient{
Endpoint: cfg.Endpoint,
Token: cfg.AgentAccessToken,
}.Create()
// Find the meta data value
var metaData *api.MetaData
var err error
var resp *api.Response
err = retry.Do(func(s *retry.Stats) error {
metaData, resp, err = client.MetaData.Get(cfg.Job, cfg.Key)
// Don't bother retrying if the response was one of these statuses
if resp != nil && (resp.StatusCode == 401 || resp.StatusCode == 404 || resp.StatusCode == 400) {
s.Break()
return err
}
if err != nil {
logger.Warn("%s (%s)", err, s)
}
return err
}, &retry.Config{Maximum: 10, Interval: 5 * time.Second})
// Deal with the error if we got one
if err != nil {
// Buildkite returns a 404 if the key doesn't exist. If
// we get this status, and we've got a default - return
// that instead and bail early.
//
// We also use `IsSet` instead of `cfg.Default != ""`
// to allow people to use a default of a blank string.
if resp.StatusCode == 404 && c.IsSet("default") {
logger.Warn("No meta-data value exists with key `%s`, returning the supplied default \"%s\"", cfg.Key, cfg.Default)
fmt.Print(cfg.Default)
return
} else {
logger.Fatal("Failed to get meta-data: %s", err)
}
}
// Output the value to STDOUT
fmt.Print(metaData.Value)
},
}