|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "flag" |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/franela/goreq" |
| 11 | +) |
| 12 | + |
| 13 | +// Config repo from which extract the configuration and user to use |
| 14 | +type Config struct { |
| 15 | + ReceiveUser string `json:"receive_user"` |
| 16 | + ReceiveRepo string `json:"receive_repo"` |
| 17 | +} |
| 18 | + |
| 19 | +// Release application configuration |
| 20 | +type Release struct { |
| 21 | + Owner string `json:"owner"` |
| 22 | + App string `json:"app"` |
| 23 | + Values map[string]interface{} `json:"values"` |
| 24 | + Memory map[string]interface{} `json:"memory"` |
| 25 | + CPU map[string]interface{} `json:"cpu"` |
| 26 | + Tags map[string]interface{} `json:"tags"` |
| 27 | + UUID string `json:"uuid"` |
| 28 | + Created time.Time `json:"created"` |
| 29 | + Updated time.Time `json:"updated"` |
| 30 | +} |
| 31 | + |
| 32 | +const ( |
| 33 | + contentType string = "application/json" |
| 34 | + userAgent string = "deis-builder" |
| 35 | +) |
| 36 | + |
| 37 | +func init() { |
| 38 | + flag.Usage = func() { |
| 39 | + fmt.Fprintf(os.Stderr, "Usage: [options]\n\n") |
| 40 | + flag.PrintDefaults() |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +func main() { |
| 45 | + url := flag.String("url", "", "Controller hook URL") |
| 46 | + builderKey := flag.String("key", "", "Builder Key") |
| 47 | + user := flag.String("user", "", "Controller username") |
| 48 | + app := flag.String("app", "", "Controller application name") |
| 49 | + |
| 50 | + flag.Parse() |
| 51 | + |
| 52 | + if flag.NFlag() < 4 { |
| 53 | + flag.Usage() |
| 54 | + os.Exit(1) |
| 55 | + } |
| 56 | + |
| 57 | + if *url == "" { |
| 58 | + fmt.Println("invalid url") |
| 59 | + os.Exit(64) |
| 60 | + } |
| 61 | + |
| 62 | + if *builderKey == "" { |
| 63 | + fmt.Println("invalid builder key") |
| 64 | + os.Exit(64) |
| 65 | + } |
| 66 | + |
| 67 | + if *user == "" { |
| 68 | + fmt.Println("invalid user") |
| 69 | + os.Exit(64) |
| 70 | + } |
| 71 | + |
| 72 | + if *app == "" { |
| 73 | + fmt.Println("invalid app") |
| 74 | + os.Exit(64) |
| 75 | + } |
| 76 | + |
| 77 | + data := Config{ReceiveUser: *user, ReceiveRepo: *app} |
| 78 | + |
| 79 | + req := goreq.Request{ |
| 80 | + Method: "POST", |
| 81 | + Uri: *url, |
| 82 | + Body: data, |
| 83 | + ContentType: contentType, |
| 84 | + Accept: contentType, |
| 85 | + UserAgent: userAgent, |
| 86 | + } |
| 87 | + |
| 88 | + req.AddHeader("X-Deis-Builder-Auth", *builderKey) |
| 89 | + |
| 90 | + res, err := req.Do() |
| 91 | + |
| 92 | + if res.StatusCode == 404 { |
| 93 | + fmt.Println("check the deis-controller. Is not running") |
| 94 | + os.Exit(2) |
| 95 | + } |
| 96 | + |
| 97 | + if err != nil || res.StatusCode != 200 { |
| 98 | + fmt.Println("failed retrieving config from controller") |
| 99 | + body, _ := res.Body.ToString() |
| 100 | + fmt.Println(body) |
| 101 | + os.Exit(1) |
| 102 | + } |
| 103 | + |
| 104 | + var release Release |
| 105 | + res.Body.FromJsonTo(&release) |
| 106 | + toSring, _ := json.Marshal(release) |
| 107 | + fmt.Println(string(toSring)) |
| 108 | +} |
0 commit comments