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