forked from knative/eventing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
100 lines (84 loc) · 2.52 KB
/
main.go
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
/*
Copyright 2018 The Knative Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Implements a simple utility for sending a JSON-encoded sample event.
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"log"
"os"
cloudevents "github.com/cloudevents/sdk-go"
"knative.dev/eventing/pkg/utils"
)
var (
eventID string
eventType string
source string
data string
)
func init() {
flag.StringVar(&eventID, "event-id", "", "Event ID to use. Defaults to a generated UUID")
flag.StringVar(&eventType, "event-type", "google.events.action.demo", "The Event Type to use.")
flag.StringVar(&source, "source", "", "Source URI to use. Defaults to the current machine's hostname")
flag.StringVar(&data, "data", `{"hello": "world!"}`, "Event data")
}
func main() {
flag.Parse()
if len(flag.Args()) != 1 {
fmt.Println("Usage: sendevent [flags] <webhook>\nFor details about valid flags, run sendevent --help")
os.Exit(1)
}
target := flag.Arg(0)
var untyped map[string]interface{}
if err := json.Unmarshal([]byte(data), &untyped); err != nil {
fmt.Println("Currently sendevent only supports JSON event data")
os.Exit(1)
}
if source == "" {
source = fmt.Sprintf("http://%s", utils.GetClusterDomainName())
}
t, err := cloudevents.NewHTTPTransport(
cloudevents.WithTarget(target),
cloudevents.WithBinaryEncoding(),
)
if err != nil {
log.Printf("failed to create transport, %v", err)
os.Exit(1)
}
c, err := cloudevents.NewClient(t,
cloudevents.WithTimeNow(),
cloudevents.WithUUIDs(),
)
if err != nil {
log.Printf("failed to create client, %v", err)
os.Exit(1)
}
event := cloudevents.NewEvent()
if eventID != "" {
event.SetID(eventID)
}
event.SetType(eventType)
event.SetSource(source)
if err := event.SetData(untyped); err != nil {
log.Printf("failed to set data, %v", err)
os.Exit(1)
}
if _, resp, err := c.Send(context.Background(), event); err != nil {
fmt.Printf("Failed to send event to %s: %s\n", target, err)
os.Exit(1)
} else if resp != nil {
fmt.Printf("Got response from %s\n%s\n", target, resp)
}
}