-
Notifications
You must be signed in to change notification settings - Fork 300
/
meta_data_exists.go
97 lines (81 loc) · 2.44 KB
/
meta_data_exists.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 clicommand
import (
"os"
"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 MetaDataExistsHelpDescription = `Usage:
buildkite-agent meta-data exists <key> [arguments...]
Description:
The command exits with a status of 0 if the key has been set, or it will
exit with a status of 100 if the key doesn't exist.
Example:
$ buildkite-agent meta-data exists "foo"`
type MetaDataExistsConfig struct {
Key string `cli:"arg:0" label:"meta-data key" validate:"required"`
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 MetaDataExistsCommand = cli.Command{
Name: "exists",
Usage: "Check to see if the meta data key exists for a build",
Description: MetaDataExistsHelpDescription,
Flags: []cli.Flag{
cli.StringFlag{
Name: "job",
Value: "",
Usage: "Which job should the meta-data be checked for",
EnvVar: "BUILDKITE_JOB_ID",
},
AgentAccessTokenFlag,
EndpointFlag,
NoColorFlag,
DebugFlag,
DebugHTTPFlag,
},
Action: func(c *cli.Context) {
// The configuration will be loaded into this struct
cfg := MetaDataExistsConfig{}
// 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 err error
var exists *api.MetaDataExists
var resp *api.Response
err = retry.Do(func(s *retry.Stats) error {
exists, resp, err = client.MetaData.Exists(cfg.Job, cfg.Key)
if resp != nil && (resp.StatusCode == 401 || resp.StatusCode == 404) {
s.Break()
}
if err != nil {
logger.Warn("%s (%s)", err, s)
}
return err
}, &retry.Config{Maximum: 10, Interval: 5 * time.Second})
if err != nil {
logger.Fatal("Failed to see if meta-data exists: %s", err)
}
// If the meta data didn't exist, exit with an error.
if !exists.Exists {
os.Exit(100)
}
},
}