-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
111 lines (91 loc) · 2.34 KB
/
main.go
File metadata and controls
111 lines (91 loc) · 2.34 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
102
103
104
105
106
107
108
109
110
111
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"
)
var strDestination = flag.String("destination", "", "Webhook destination (https://example.com/webhooks)")
var strBody = flag.String("body", "", "Webhook payload")
var strMethod = flag.String("method", "", "Webhook method (defaults to 'POST')")
var strAuth = flag.String("basic-auth", "", "Optional basic authentication in a 'user:pass' format")
func main() {
flag.Usage = func() {
fmt.Printf("Usage of %s:\n", os.Args[0])
fmt.Printf(" webhook-dispatcher --destination https://my.webhookrelay.com/v1/webhooks/.... --body hello\n")
flag.PrintDefaults()
}
if len(os.Args) == 1 {
flag.PrintDefaults()
os.Exit(1)
}
flag.Parse()
if *strDestination == "" && os.Getenv("DESTINATION") == "" {
fmt.Println("both --destination flag and DESTINATION env variable cannot be empty, usage:")
flag.PrintDefaults()
os.Exit(1)
}
client := http.DefaultClient
var (
user string
pass string
)
if *strAuth != "" {
parts := strings.SplitN(*strAuth, ":", 2)
if len(parts) > 0 {
user = parts[0]
pass = parts[1]
}
}
method := http.MethodPost
switch *strMethod {
case http.MethodGet, http.MethodHead, http.MethodDelete, http.MethodPost, http.MethodPut, http.MethodPatch:
method = *strMethod
default:
// something invalid
}
var destination string
if *strDestination != "" {
destination = *strDestination
}
if os.Getenv("DESTINATION") != "" {
destination = os.Getenv("DESTINATION")
}
req, err := http.NewRequest(method, destination, bytes.NewBufferString(*strBody))
if err != nil {
fmt.Printf("failed to create request: %s \n", err)
os.Exit(1)
}
if user != "" && pass != "" {
req.SetBasicAuth(user, pass)
}
resp, err := client.Do(req)
if err != nil {
fmt.Printf("request failed: %s \n", err)
os.Exit(1)
}
defer resp.Body.Close()
bodyBts, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Printf("response body read failed: %s \n", err)
os.Exit(1)
}
var wr webhookResponse
wr.Body = string(bodyBts)
wr.StatusCode = resp.StatusCode
encoded, err := json.Marshal(&wr)
if err != nil {
fmt.Printf("failed to encode response: %s \n", err)
os.Exit(1)
}
fmt.Println(string(encoded))
os.Exit(0)
}
type webhookResponse struct {
StatusCode int `json:"status_code"`
Body string `json:"body"`
}