github.com/bshelton229/agent@v3.5.4+incompatible/clicommand/meta_data_exists.go (about) 1 package clicommand 2 3 import ( 4 "os" 5 "time" 6 7 "github.com/buildkite/agent/agent" 8 "github.com/buildkite/agent/api" 9 "github.com/buildkite/agent/cliconfig" 10 "github.com/buildkite/agent/logger" 11 "github.com/buildkite/agent/retry" 12 "github.com/urfave/cli" 13 ) 14 15 var MetaDataExistsHelpDescription = `Usage: 16 17 buildkite-agent meta-data exists <key> [arguments...] 18 19 Description: 20 21 The command exits with a status of 0 if the key has been set, or it will 22 exit with a status of 100 if the key doesn't exist. 23 24 Example: 25 26 $ buildkite-agent meta-data exists "foo"` 27 28 type MetaDataExistsConfig struct { 29 Key string `cli:"arg:0" label:"meta-data key" validate:"required"` 30 Job string `cli:"job" validate:"required"` 31 AgentAccessToken string `cli:"agent-access-token" validate:"required"` 32 Endpoint string `cli:"endpoint" validate:"required"` 33 NoColor bool `cli:"no-color"` 34 Debug bool `cli:"debug"` 35 DebugHTTP bool `cli:"debug-http"` 36 } 37 38 var MetaDataExistsCommand = cli.Command{ 39 Name: "exists", 40 Usage: "Check to see if the meta data key exists for a build", 41 Description: MetaDataExistsHelpDescription, 42 Flags: []cli.Flag{ 43 cli.StringFlag{ 44 Name: "job", 45 Value: "", 46 Usage: "Which job should the meta-data be checked for", 47 EnvVar: "BUILDKITE_JOB_ID", 48 }, 49 AgentAccessTokenFlag, 50 EndpointFlag, 51 NoColorFlag, 52 DebugFlag, 53 DebugHTTPFlag, 54 }, 55 Action: func(c *cli.Context) { 56 // The configuration will be loaded into this struct 57 cfg := MetaDataExistsConfig{} 58 59 // Load the configuration 60 if err := cliconfig.Load(c, &cfg); err != nil { 61 logger.Fatal("%s", err) 62 } 63 64 // Setup the any global configuration options 65 HandleGlobalFlags(cfg) 66 67 // Create the API client 68 client := agent.APIClient{ 69 Endpoint: cfg.Endpoint, 70 Token: cfg.AgentAccessToken, 71 }.Create() 72 73 // Find the meta data value 74 var err error 75 var exists *api.MetaDataExists 76 var resp *api.Response 77 err = retry.Do(func(s *retry.Stats) error { 78 exists, resp, err = client.MetaData.Exists(cfg.Job, cfg.Key) 79 if resp != nil && (resp.StatusCode == 401 || resp.StatusCode == 404) { 80 s.Break() 81 } 82 if err != nil { 83 logger.Warn("%s (%s)", err, s) 84 } 85 86 return err 87 }, &retry.Config{Maximum: 10, Interval: 5 * time.Second}) 88 if err != nil { 89 logger.Fatal("Failed to see if meta-data exists: %s", err) 90 } 91 92 // If the meta data didn't exist, exit with an error. 93 if !exists.Exists { 94 os.Exit(100) 95 } 96 }, 97 }