github.com/stevenmatthewt/agent@v3.5.4+incompatible/clicommand/meta_data_get.go (about) 1 package clicommand 2 3 import ( 4 "fmt" 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 MetaDataGetHelpDescription = `Usage: 16 17 buildkite-agent meta-data get <key> [arguments...] 18 19 Description: 20 21 Get data from a builds key/value store. 22 23 Example: 24 25 $ buildkite-agent meta-data get "foo"` 26 27 type MetaDataGetConfig struct { 28 Key string `cli:"arg:0" label:"meta-data key" validate:"required"` 29 Default string `cli:"default"` 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 MetaDataGetCommand = cli.Command{ 39 Name: "get", 40 Usage: "Get data from a build", 41 Description: MetaDataGetHelpDescription, 42 Flags: []cli.Flag{ 43 cli.StringFlag{ 44 Name: "default", 45 Value: "", 46 Usage: "If the meta-data value doesn't exist return this instead", 47 }, 48 cli.StringFlag{ 49 Name: "job", 50 Value: "", 51 Usage: "Which job should the meta-data be retrieved from", 52 EnvVar: "BUILDKITE_JOB_ID", 53 }, 54 AgentAccessTokenFlag, 55 EndpointFlag, 56 NoColorFlag, 57 DebugFlag, 58 DebugHTTPFlag, 59 }, 60 Action: func(c *cli.Context) { 61 // The configuration will be loaded into this struct 62 cfg := MetaDataGetConfig{} 63 64 // Load the configuration 65 if err := cliconfig.Load(c, &cfg); err != nil { 66 logger.Fatal("%s", err) 67 } 68 69 // Setup the any global configuration options 70 HandleGlobalFlags(cfg) 71 72 // Create the API client 73 client := agent.APIClient{ 74 Endpoint: cfg.Endpoint, 75 Token: cfg.AgentAccessToken, 76 }.Create() 77 78 // Find the meta data value 79 var metaData *api.MetaData 80 var err error 81 var resp *api.Response 82 err = retry.Do(func(s *retry.Stats) error { 83 metaData, resp, err = client.MetaData.Get(cfg.Job, cfg.Key) 84 // Don't bother retrying if the response was one of these statuses 85 if resp != nil && (resp.StatusCode == 401 || resp.StatusCode == 404 || resp.StatusCode == 400) { 86 s.Break() 87 return err 88 } 89 if err != nil { 90 logger.Warn("%s (%s)", err, s) 91 } 92 93 return err 94 }, &retry.Config{Maximum: 10, Interval: 5 * time.Second}) 95 96 // Deal with the error if we got one 97 if err != nil { 98 // Buildkite returns a 404 if the key doesn't exist. If 99 // we get this status, and we've got a default - return 100 // that instead and bail early. 101 // 102 // We also use `IsSet` instead of `cfg.Default != ""` 103 // to allow people to use a default of a blank string. 104 if resp.StatusCode == 404 && c.IsSet("default") { 105 logger.Warn("No meta-data value exists with key `%s`, returning the supplied default \"%s\"", cfg.Key, cfg.Default) 106 107 fmt.Print(cfg.Default) 108 return 109 } else { 110 logger.Fatal("Failed to get meta-data: %s", err) 111 } 112 } 113 114 // Output the value to STDOUT 115 fmt.Print(metaData.Value) 116 }, 117 }