-
Notifications
You must be signed in to change notification settings - Fork 819
/
Copy pathmain.go
159 lines (135 loc) · 4.26 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Copyright 2018 The Go Cloud Development Kit 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.
// gocdk-pubsub demonstrates the use of the Go CDK pubsub package in a
// simple command-line application.
package main
import (
"bufio"
"context"
"flag"
"fmt"
"log"
"os"
"github.com/google/subcommands"
"gocloud.dev/pubsub"
// Import the pubsub driver packages we want to be able to open.
_ "gocloud.dev/pubsub/awssnssqs"
_ "gocloud.dev/pubsub/azuresb"
_ "gocloud.dev/pubsub/gcppubsub"
_ "gocloud.dev/pubsub/kafkapubsub"
_ "gocloud.dev/pubsub/natspubsub"
_ "gocloud.dev/pubsub/rabbitpubsub"
)
const helpSuffix = `
See https://gocloud.dev/concepts/urls/ for more background on
Go CDK URLs, and sub-packages under gocloud.dev/pubsub
(https://godoc.org/gocloud.dev/pubsub#pkg-subdirectories)
for details on the topic/subscription URL format.
`
func main() {
os.Exit(run())
}
func run() int {
subcommands.Register(subcommands.HelpCommand(), "")
subcommands.Register(&pubCmd{}, "")
subcommands.Register(&subCmd{}, "")
log.SetFlags(0)
log.SetPrefix("gocdk-pubsub: ")
flag.Parse()
return int(subcommands.Execute(context.Background()))
}
type pubCmd struct{}
func (*pubCmd) Name() string { return "pub" }
func (*pubCmd) Synopsis() string { return "Publish a message to a topic" }
func (*pubCmd) Usage() string {
return `pub <topic URL>
Read messages from stdin, one per line and send them to <topic URL>.
Example:
gocdk-pubsub pub gcppubsub://myproject/mytopic` + helpSuffix
}
func (*pubCmd) SetFlags(_ *flag.FlagSet) {}
func (*pubCmd) Execute(ctx context.Context, f *flag.FlagSet, _ ...any) subcommands.ExitStatus {
if f.NArg() != 1 {
f.Usage()
return subcommands.ExitUsageError
}
topicURL := f.Arg(0)
// Open a *pubsub.Topic using the URL.
topic, err := pubsub.OpenTopic(ctx, topicURL)
if err != nil {
log.Print(err)
return subcommands.ExitFailure
}
defer topic.Shutdown(ctx)
// Read lines from stdin and send them as messages to the topic.
fmt.Fprintf(os.Stderr, "Enter messages, one per line, to be published to %q.\n", topicURL)
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
line := scanner.Text()
if line == "" {
log.Print("Skipping empty message.")
continue
}
m := &pubsub.Message{Body: []byte(line)}
if err := topic.Send(ctx, m); err != nil {
log.Print(err)
return subcommands.ExitFailure
}
}
if err := scanner.Err(); err != nil {
log.Print(err)
return subcommands.ExitFailure
}
return subcommands.ExitSuccess
}
type subCmd struct {
n int // number of messages to receive, or 0 for infinite
}
func (*subCmd) Name() string { return "sub" }
func (*subCmd) Synopsis() string { return "Receive messages from a subscription" }
func (*subCmd) Usage() string {
return `sub [-n N] <subscription URL>
Receive messages from <subscription URL> and send them to stdout, one per line.
Example:
gocdk-pubsub sub gcppubsub://myproject/mytopic` + helpSuffix
}
func (cmd *subCmd) SetFlags(f *flag.FlagSet) {
f.IntVar(&cmd.n, "n", 0, "number of messages to receive, or 0 for unlimited")
}
func (cmd *subCmd) Execute(ctx context.Context, f *flag.FlagSet, _ ...any) subcommands.ExitStatus {
if f.NArg() != 1 {
f.Usage()
return subcommands.ExitUsageError
}
subURL := f.Arg(0)
// Open a *pubsub.Subscription using the URL.
sub, err := pubsub.OpenSubscription(ctx, subURL)
if err != nil {
log.Print(err)
return subcommands.ExitFailure
}
defer sub.Shutdown(ctx)
// Receive messages from the subscription and print them to stdout.
fmt.Printf("Receiving messages from %q...\n", subURL)
for i := 0; cmd.n == 0 || i < cmd.n; i++ {
m, err := sub.Receive(ctx)
if err != nil {
log.Print(err)
return subcommands.ExitFailure
}
fmt.Printf("%s\n", m.Body)
m.Ack()
}
return subcommands.ExitSuccess
}