-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpublish.go
62 lines (54 loc) · 1.47 KB
/
publish.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
package main
import (
"context"
"encoding/json"
"log"
"cloud.google.com/go/pubsub"
"github.com/spf13/cobra"
)
func newPublishCommand(ctx context.Context) *cobra.Command {
command := cobra.Command{
Use: "publish",
Short: "Publish a topic to the emulator",
Args: cobra.NoArgs,
Run: func(command *cobra.Command, args []string) {
host := command.Flag("host").Value.String()
projectID := command.Flag("project_id").Value.String()
client, err := setupClient(ctx, host, projectID)
if err != nil {
log.Fatalln(err)
}
topicID := command.Flag("topic_id").Value.String()
message := command.Flag("message").Value.String()
if err := publishTopic(ctx, client, topicID, message); err != nil {
log.Fatalln(err)
}
},
}
command.Flags().StringP("topic_id", "t", "", "topic id (required)")
command.Flags().StringP("message", "m", "", "message (required)")
if err := command.MarkFlagRequired("topic_id"); err != nil {
log.Fatalln(err)
}
if err := command.MarkFlagRequired("message"); err != nil {
log.Fatalln(err)
}
return &command
}
// Publish a topic to the emulator
func publishTopic(ctx context.Context, client *pubsub.Client, topicID string, message string) error {
topic, err := getTopic(ctx, client, topicID)
if err != nil {
return err
}
data, err := json.Marshal(message)
if err != nil {
return err
}
result := topic.Publish(ctx, &pubsub.Message{Data: data})
_, err = result.Get(ctx)
if err != nil {
return err
}
return nil
}