Some minor code cleanups
This commit is contained in:
parent
d35d69a983
commit
1f50e69fa2
47
main.go
47
main.go
|
@ -33,27 +33,27 @@ func main() {
|
||||||
plugin.Param("vargs", &vargs)
|
plugin.Param("vargs", &vargs)
|
||||||
plugin.MustParse()
|
plugin.MustParse()
|
||||||
|
|
||||||
if len(vargs.Method) == 0 {
|
if vargs.Method == "" {
|
||||||
vargs.Method = "POST"
|
vargs.Method = "POST"
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(vargs.ContentType) == 0 {
|
if vargs.ContentType == "" {
|
||||||
vargs.ContentType = "application/json"
|
vargs.ContentType = "application/json"
|
||||||
}
|
}
|
||||||
|
|
||||||
data := struct {
|
// Creates the payload, by default the payload
|
||||||
System drone.System `json:"system"`
|
|
||||||
Repo drone.Repo `json:"repo"`
|
|
||||||
Build drone.Build `json:"build"`
|
|
||||||
}{system, repo, build}
|
|
||||||
|
|
||||||
// creates the payload. by default the payload
|
|
||||||
// is the build details in json format, but a custom
|
// is the build details in json format, but a custom
|
||||||
// template may also be used.
|
// template may also be used.
|
||||||
|
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
|
|
||||||
if len(vargs.Template) == 0 {
|
if vargs.Template == "" {
|
||||||
|
data := struct {
|
||||||
|
System drone.System `json:"system"`
|
||||||
|
Repo drone.Repo `json:"repo"`
|
||||||
|
Build drone.Build `json:"build"`
|
||||||
|
}{system, repo, build}
|
||||||
|
|
||||||
if err := json.NewEncoder(&buf).Encode(&data); err != nil {
|
if err := json.NewEncoder(&buf).Encode(&data); err != nil {
|
||||||
fmt.Printf("Error encoding json payload. %s\n", err)
|
fmt.Printf("Error encoding json payload. %s\n", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
@ -100,12 +100,8 @@ func main() {
|
||||||
req.Header.Set(key, value)
|
req.Header.Set(key, value)
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(vargs.Auth.Username) > 0 {
|
if vargs.Auth.Username != "" {
|
||||||
if len(vargs.Auth.Password) > 0 {
|
req.SetBasicAuth(vargs.Auth.Username, vargs.Auth.Password)
|
||||||
req.SetBasicAuth(vargs.Auth.Username, vargs.Auth.Password)
|
|
||||||
} else {
|
|
||||||
req.SetBasicAuth(vargs.Auth.Username, "")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := http.DefaultClient.Do(req)
|
resp, err := http.DefaultClient.Do(req)
|
||||||
|
@ -127,9 +123,24 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
if vargs.Debug {
|
if vargs.Debug {
|
||||||
fmt.Printf("[debug] Webhook %d\n URL: %s\n METHOD: %s\n HEADERS: %s\n REQUEST BODY: %s\n RESPONSE STATUS: %s\n RESPONSE BODY: %s\n", i+1, req.URL, req.Method, req.Header, string(b), resp.Status, string(body))
|
fmt.Printf(
|
||||||
|
"Webhook %d\n URL: %s\n METHOD: %s\n HEADERS: %s\n REQUEST BODY: %s\n RESPONSE STATUS: %s\n RESPONSE BODY: %s\n",
|
||||||
|
i+1,
|
||||||
|
req.URL,
|
||||||
|
req.Method,
|
||||||
|
req.Header,
|
||||||
|
string(b),
|
||||||
|
resp.Status,
|
||||||
|
string(body),
|
||||||
|
)
|
||||||
} else {
|
} else {
|
||||||
fmt.Printf("[info] Webhook %d\n URL: %s\n RESPONSE STATUS: %s\n RESPONSE BODY: %s\n", i+1, req.URL, resp.Status, string(body))
|
fmt.Printf(
|
||||||
|
"Webhook %d\n URL: %s\n RESPONSE STATUS: %s\n RESPONSE BODY: %s\n",
|
||||||
|
i+1,
|
||||||
|
req.URL,
|
||||||
|
resp.Status,
|
||||||
|
string(body),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue