-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
167 lines (148 loc) · 4.71 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
160
161
162
163
164
165
166
167
//
// Copyright 2022 Google LLC
//
// 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
//
// http://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.
//
// spanner-change-streams-tail is a tool to tail Cloud Spanner Change Streams.
package main
import (
"context"
"flag"
"fmt"
"os"
"os/signal"
"strings"
"time"
"cloud.google.com/go/spanner"
"github.com/cloudspannerecosystem/spanner-change-streams-tail/changestreams"
)
func usage() {
command := os.Args[0]
fmt.Printf(`Usage:
%s [OPTIONS]
Options:
-p, --project= (required) GCP Project ID
-i, --instance= (required) Cloud Spanner Instance ID
-d, --database= (required) Cloud Spanner Database ID
-s, --stream= (required) Cloud Spanner Change Stream ID
-f, --format= Output format [text|json] (default: text)
--start= Start timestamp with RFC3339 format (default: current timestamp)
--end= End timestamp with RFC3339 format (default: none)
--role= Database role for fine-grained access control
--visualize-partitions Visualize the change stream partitions in Graphviz DOT
Help Options:
-h, -help Show this help message
`, command)
}
func main() {
var (
projectID, instanceID, databaseID, streamID, format, start, end, role string
startTimestamp, endTimestamp time.Time
verbose, visualizePartitions bool
)
// Long options.
flag.StringVar(&projectID, "project", "", "")
flag.StringVar(&instanceID, "instance", "", "")
flag.StringVar(&databaseID, "database", "", "")
flag.StringVar(&streamID, "stream", "", "")
flag.StringVar(&format, "format", formatText, "")
flag.StringVar(&start, "start", "", "")
flag.StringVar(&end, "end", "", "")
flag.StringVar(&role, "role", "", "")
flag.BoolVar(&verbose, "verbose", false, "")
flag.BoolVar(&visualizePartitions, "visualize-partitions", false, "")
// Short options.
flag.StringVar(&projectID, "p", "", "")
flag.StringVar(&instanceID, "i", "", "")
flag.StringVar(&databaseID, "d", "", "")
flag.StringVar(&streamID, "s", "", "")
flag.StringVar(&format, "f", formatText, "")
flag.BoolVar(&verbose, "v", false, "")
flag.Usage = usage
flag.Parse()
// Validate required options.
if projectID == "" || instanceID == "" || databaseID == "" || streamID == "" {
flag.Usage()
os.Exit(1)
}
// Validate optional options.
if format != formatText && format != formatJSON {
exitf("invalid format: %s", format)
}
if start != "" {
ts, err := time.Parse(time.RFC3339, start)
if err != nil {
exitf("invalid start timestamp: %v", err)
}
startTimestamp = ts
}
if end != "" {
ts, err := time.Parse(time.RFC3339, end)
if err != nil {
exitf("invalid end timestamp: %v", err)
}
endTimestamp = ts
}
if visualizePartitions {
if start == "" || end == "" {
exitf("To visualize partitions, specify --start and --end options as well")
}
}
ctx, cancel := context.WithCancel(context.Background())
go handleInterrupt(cancel)
config := changestreams.Config{
StartTimestamp: startTimestamp,
EndTimestamp: endTimestamp,
SpannerClientConfig: spanner.ClientConfig{
SessionPoolConfig: spanner.DefaultSessionPoolConfig,
DatabaseRole: role,
},
}
reader, err := changestreams.NewReaderWithConfig(ctx, projectID, instanceID, databaseID, streamID, config)
if err != nil {
exitf("failed to create a reader: %v", err)
}
defer reader.Close()
if visualizePartitions {
fmt.Fprintf(os.Stderr, "Reading the stream and analyzing partitions...\n\n")
visualizer := NewPartitionVisualizer(os.Stdout)
if err := reader.Read(ctx, visualizer.Read); err != nil {
exitf("failed to read stream: %v", err)
}
visualizer.Draw()
return
}
fmt.Fprintf(os.Stderr, "Reading the stream...\n")
logger := &Logger{
out: os.Stdout,
format: format,
verbose: verbose,
}
if err := reader.Read(ctx, logger.Read); err != nil {
exitf("failed to read stream: %v", err)
}
}
func exitf(format string, a ...interface{}) {
message := fmt.Sprintf(format, a...)
if !strings.HasSuffix(message, "\n") {
message += "\n"
}
fmt.Fprint(os.Stderr, message)
os.Exit(1)
}
func handleInterrupt(cancel context.CancelFunc) {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
<-c
cancel()
}