-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathget-app-config.go
More file actions
101 lines (81 loc) · 1.82 KB
/
get-app-config.go
File metadata and controls
101 lines (81 loc) · 1.82 KB
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
98
99
100
101
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"net/http"
"os"
"github.com/deis/deis/builder"
)
const (
contentType string = "application/json"
userAgent string = "deis-builder"
)
func init() {
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: [options]\n\n")
flag.PrintDefaults()
}
}
func main() {
url := flag.String("url", "", "Controller hook URL")
builderKey := flag.String("key", "", "Builder Key")
user := flag.String("user", "", "Controller username")
app := flag.String("app", "", "Controller application name")
flag.Parse()
if flag.NFlag() < 4 {
flag.Usage()
os.Exit(1)
}
if *url == "" {
fmt.Println("invalid url")
os.Exit(1)
}
if *builderKey == "" {
fmt.Println("invalid builder key")
os.Exit(1)
}
if *user == "" {
fmt.Println("invalid user")
os.Exit(1)
}
if *app == "" {
fmt.Println("invalid app")
os.Exit(1)
}
data, err := json.Marshal(&builder.ConfigHook{ReceiveUser: *user, ReceiveRepo: *app})
if err != nil {
fmt.Println(err)
os.Exit(1)
}
b := bytes.NewReader(data)
client := &http.Client{}
req, err := http.NewRequest("POST", *url, b)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
req.Header.Add("Content-Type", contentType)
req.Header.Add("Accept", contentType)
req.Header.Add("User-Agent", userAgent)
req.Header.Add("X-Deis-Builder-Auth", *builderKey)
res, err := client.Do(req)
defer res.Body.Close()
if res.StatusCode == 404 {
fmt.Println("Check the Controller. Is it running?")
os.Exit(1)
}
if err != nil || res.StatusCode != 200 {
fmt.Println("failed retrieving config from controller")
fmt.Println(res.Body)
os.Exit(1)
}
config, err := builder.ParseConfig(res)
if err != nil {
fmt.Println("failed parsing config from controller")
os.Exit(1)
}
toString, err := json.Marshal(config)
fmt.Println(string(toString))
}