-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
409 lines (350 loc) · 14.4 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
//
// Copyright 2020 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.
//
// Package main is a command line tool for Cloud Spanner
package main
import (
"cmp"
"context"
"fmt"
"io"
"maps"
"os"
"os/user"
"path/filepath"
"runtime/debug"
"strconv"
"strings"
"time"
"cloud.google.com/go/spanner"
"github.com/apstndb/spanemuboost"
"github.com/cloudspannerecosystem/memefish"
"github.com/cloudspannerecosystem/memefish/ast"
sppb "cloud.google.com/go/spanner/apiv1/spannerpb"
"github.com/jessevdk/go-flags"
"github.com/samber/lo"
"spheric.cloud/xiter"
)
type globalOptions struct {
Spanner spannerOptions `group:"spanner"`
}
// We can't use `default` because spanner-mycli uses multiple flags.NewParser() to process config files and flags.
type spannerOptions struct {
ProjectId string `long:"project" short:"p" env:"SPANNER_PROJECT_ID" description:"(required) GCP Project ID."`
InstanceId string `long:"instance" short:"i" env:"SPANNER_INSTANCE_ID" description:"(required) Cloud Spanner Instance ID"`
DatabaseId string `long:"database" short:"d" env:"SPANNER_DATABASE_ID" description:"(required) Cloud Spanner Database ID."`
Execute string `long:"execute" short:"e" description:"Execute SQL statement and quit. --sql is an alias."`
File string `long:"file" short:"f" description:"Execute SQL statement from file and quit."`
Table bool `long:"table" short:"t" description:"Display output in table format for batch mode."`
Verbose bool `long:"verbose" short:"v" description:"Display verbose output."`
Credential string `long:"credential" description:"Use the specific credential file"`
Prompt *string `long:"prompt" description:"Set the prompt to the specified format" default-mask:"spanner%t> "`
Prompt2 *string `long:"prompt2" description:"Set the prompt2 to the specified format" default-mask:"%P%R> "`
LogMemefish bool `long:"log-memefish" description:"Emit SQL parse log using memefish"`
HistoryFile *string `long:"history" description:"Set the history file to the specified path" default-mask:"/tmp/spanner_mycli_readline.tmp"`
Priority string `long:"priority" description:"Set default request priority (HIGH|MEDIUM|LOW)"`
Role string `long:"role" description:"Use the specific database role"`
Endpoint string `long:"endpoint" description:"Set the Spanner API endpoint (host:port)"`
DirectedRead string `long:"directed-read" description:"Directed read option (replica_location:replica_type). The replicat_type is optional and either READ_ONLY or READ_WRITE"`
SQL string `long:"sql" hidden:"true" description:"alias of --execute"`
Set map[string]string `long:"set" key-value-delimiter:"=" description:"Set system variables e.g. --set=name1=value1 --set=name2=value2"`
Param map[string]string `long:"param" key-value-delimiter:"=" description:"Set query parameters, it can be literal or type(EXPLAIN/DESCRIBE only) e.g. --param=\"p1='string_value'\" --param=p2=FLOAT64"`
ProtoDescriptorFile string `long:"proto-descriptor-file" description:"Path of a file that contains a protobuf-serialized google.protobuf.FileDescriptorSet message."`
Insecure bool `long:"insecure" description:"Skip TLS verification and permit plaintext gRPC. --skip-tls-verify is an alias."`
SkipTlsVerify bool `long:"skip-tls-verify" description:"An alias of --insecure" hidden:"true"`
EmbeddedEmulator bool `long:"embedded-emulator" description:"Use embedded Cloud Spanner Emulator. --project, --instance, --database, --endpoint, --insecure will be automatically configured."`
EmulatorImage string `long:"emulator-image" description:"container image for --embedded-emulator"`
Help bool `long:"help" short:"h" hidden:"true"`
Debug bool `long:"debug" hidden:"true"`
LogGrpc bool `long:"log-grpc" description:"Show gRPC logs"`
QueryMode string `long:"query-mode" description:"Mode in which the query must be processed." choice:"NORMAL" choice:"PLAN" choice:"PROFILE"`
Strong bool `long:"strong" description:"Perform a strong query."`
ReadTimestamp string `long:"read-timestamp" description:"Perform a query at the given timestamp."`
VertexAIProject string `long:"vertexai-project" description:"VertexAI project" ini-name:"vertexai_project"`
DatabaseDialect string `long:"database-dialect" description:"The SQL dialect of the Cloud Spanner Database." choice:"POSTGRESQL" choice:"GOOGLE_STANDARD_SQL"`
ImpersonateServiceAccount string `long:"impersonate-service-account" description:"Impersonate service account email"`
Version bool `long:"version" description:"Show version string."`
}
func addEmulatorImageOption(parser *flags.Parser) {
parser.Groups()[0].Find("spanner").FindOptionByLongName("emulator-image").DefaultMask = spanemuboost.DefaultEmulatorImage
}
const (
defaultPrompt = "spanner%t> "
defaultPrompt2 = "%P%R> "
defaultHistoryFile = "/tmp/spanner_mycli_readline.tmp"
)
var logMemefish bool
var (
// https://rhysd.hatenablog.com/entry/2021/06/27/222254
version = ""
installFrom = "built from source"
)
func getVersion() string {
if version != "" {
return version
}
info, ok := debug.ReadBuildInfo()
if !ok {
return "(unknown)"
}
return info.Main.Version
}
// Overwrite genkit/logger's init
func init() {
/*
h := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{
Level: slog.LevelWarn,
}))
slog.SetDefault(h)
*/
}
func main() {
var gopts globalOptions
// process config files at first
configFileParser := flags.NewParser(&gopts, flags.Default)
addEmulatorImageOption(configFileParser)
if err := readConfigFile(configFileParser); err != nil {
exitf("Invalid config file format\n")
}
// then, process environment variables and command line options
// use another parser to process environment variables with higher precedence than configuration files
// flagParser := flags.NewParser(&gopts, flags.PrintErrors|flags.PassDoubleDash)
flagParser := flags.NewParser(&gopts, flags.PrintErrors|flags.PassDoubleDash)
addEmulatorImageOption(flagParser)
// TODO: Workaround to avoid to display config value as default
parserForHelp := flags.NewParser(&globalOptions{}, flags.Default)
addEmulatorImageOption(parserForHelp)
if _, err := flagParser.Parse(); flags.WroteHelp(err) {
// exit successfully
return
} else if err != nil {
parserForHelp.WriteHelp(os.Stderr)
exitf("Invalid options\n")
} else if gopts.Spanner.Help {
parserForHelp.WriteHelp(os.Stderr)
return
} else if gopts.Spanner.Version {
fmt.Printf("%v\n%v\n", getVersion(), installFrom)
return
}
opts := gopts.Spanner
logMemefish = opts.LogMemefish
if opts.Insecure && opts.SkipTlsVerify {
exitf("invalid parameters: --insecure and --skip-tls-verify are mutually exclusive\n")
}
if opts.Strong && opts.ReadTimestamp != "" {
exitf("invalid parameters: --strong and --read-timestamp are mutually exclusive\n")
}
if !opts.EmbeddedEmulator && (opts.ProjectId == "" || opts.InstanceId == "" || opts.DatabaseId == "") {
exitf("Missing parameters: -p, -i, -d are required\n")
}
params := make(map[string]ast.Node)
for k, v := range opts.Param {
if typ, err := memefish.ParseType("", v); err == nil {
params[k] = typ
continue
}
// ignore ParseType error
if expr, err := memefish.ParseExpr("", v); err != nil {
exitf("error on parsing --param=%v=%v, err: %v", k, v, err)
} else {
params[k] = expr
}
}
sysVars := systemVariables{
Project: opts.ProjectId,
Instance: opts.InstanceId,
Database: opts.DatabaseId,
Verbose: opts.Verbose,
Prompt: lo.FromPtrOr(opts.Prompt, defaultPrompt),
Prompt2: lo.FromPtrOr(opts.Prompt2, defaultPrompt2),
HistoryFile: lo.FromPtrOr(opts.HistoryFile, defaultHistoryFile),
Role: opts.Role,
Endpoint: opts.Endpoint,
Insecure: opts.Insecure || opts.SkipTlsVerify,
Debug: opts.Debug,
Params: params,
LogGrpc: opts.LogGrpc,
ImpersonateServiceAccount: opts.ImpersonateServiceAccount,
VertexAIProject: opts.VertexAIProject,
EnableADCPlus: true,
}
if opts.Strong {
sysVars.ReadOnlyStaleness = lo.ToPtr(spanner.StrongRead())
}
if opts.ReadTimestamp != "" {
ts, err := time.Parse(time.RFC3339Nano, opts.ReadTimestamp)
if err != nil {
exitf("error on parsing --read-timestamp=%v, err: %v\n", opts.ReadTimestamp, err)
}
sysVars.ReadOnlyStaleness = lo.ToPtr(spanner.ReadTimestamp(ts))
}
if opts.DatabaseDialect != "" {
if err := sysVars.Set("CLI_DATABASE_DIALECT", opts.DatabaseDialect); err != nil {
exitf("invalid value of --database-dialect: %v, err: %v\n", opts.DatabaseDialect, err)
}
}
ss := lo.Ternary(opts.ProtoDescriptorFile != "", strings.Split(opts.ProtoDescriptorFile, ","), nil)
for _, s := range ss {
if err := sysVars.Add("CLI_PROTO_DESCRIPTOR_FILE", strconv.Quote(s)); err != nil {
exitf("error on --proto-descriptor-file, file: %v, err: %v\n", s, err)
}
}
if nonEmptyInputCount := xiter.Count(xiter.Of(opts.File, opts.Execute, opts.SQL), lo.IsNotEmpty); nonEmptyInputCount > 1 {
exitf("Invalid combination: -e, -f, --sql are exclusive\n")
}
var cred []byte
if opts.Credential != "" {
var err error
if cred, err = readCredentialFile(opts.Credential); err != nil {
exitf("Failed to read the credential file: %v\n", err)
}
}
if opts.Priority != "" {
priority, err := parsePriority(opts.Priority)
if err != nil {
exitf("priority must be either HIGH, MEDIUM, or LOW\n")
}
sysVars.RPCPriority = priority
} else {
sysVars.RPCPriority = defaultPriority
}
if opts.QueryMode != "" {
if err := sysVars.Set("CLI_QUERY_MODE", opts.QueryMode); err != nil {
exitf("invalid value of --query-mode: %v, err: %v\n", opts.QueryMode, err)
}
}
var directedRead *sppb.DirectedReadOptions
if opts.DirectedRead != "" {
var err error
directedRead, err = parseDirectedReadOption(opts.DirectedRead)
if err != nil {
exitf("Invalid directed read option: %v\n", err)
}
sysVars.DirectedRead = directedRead
}
sets := maps.Collect(xiter.MapKeys(maps.All(opts.Set), strings.ToUpper))
for k, v := range sets {
if err := sysVars.Set(k, v); err != nil {
exitf("failed to set system variable. name: %v, value: %v, err: %v\n", k, v, err)
}
}
ctx := context.Background()
if opts.EmbeddedEmulator {
sysVars.Project = "emulator-project"
sysVars.Instance = "emulator-instance"
sysVars.Database = "emulator-database"
sysVars.Insecure = true
container, teardown, err := spanemuboost.NewEmulator(ctx,
spanemuboost.WithProjectID(sysVars.Project),
spanemuboost.WithInstanceID(sysVars.Instance),
spanemuboost.WithDatabaseID(sysVars.Database),
spanemuboost.WithEmulatorImage(cmp.Or(opts.EmulatorImage, spanemuboost.DefaultEmulatorImage)),
spanemuboost.WithDatabaseDialect(sysVars.DatabaseDialect),
)
if err != nil {
exitf("failed to start Cloud Spanner Emulator: %v\n", err)
}
defer teardown()
sysVars.Endpoint = container.URI
}
cli, err := NewCli(ctx, cred, os.Stdin, os.Stdout, os.Stderr, &sysVars)
if err != nil {
exitf("Failed to connect to Spanner: %v", err)
}
if opts.Execute != "" && opts.SQL != "" {
exitf("--execute and --sql are mutually exclusive\n")
}
var input string
if opts.Execute != "" {
input = opts.Execute
} else if opts.SQL != "" {
input = opts.SQL
} else if opts.File == "-" {
b, err := io.ReadAll(os.Stdin)
if err != nil {
exitf("Read from stdin failed: %v", err)
}
input = string(b)
} else if opts.File != "" {
b, err := os.ReadFile(opts.File)
if err != nil {
exitf("Read from file %v failed: %v", opts.File, err)
}
input = string(b)
} else {
input, err = readStdin()
if err != nil {
exitf("Read from stdin failed: %v", err)
}
}
interactive := input == ""
if _, ok := sets["CLI_FORMAT"]; !ok {
sysVars.CLIFormat = lo.Ternary(interactive || opts.Table, DisplayModeTable, DisplayModeTab)
}
exitCode := lo.TernaryF(interactive,
func() int {
sysVars.EnableProgressBar = true
return cli.RunInteractive(ctx)
},
func() int {
return cli.RunBatch(ctx, input)
})
os.Exit(exitCode)
}
func exitf(format string, a ...interface{}) {
fmt.Fprintf(os.Stderr, format, a...)
os.Exit(1)
}
const cnfFileName = ".spanner_mycli.cnf"
func readConfigFile(parser *flags.Parser) error {
var cnfFiles []string
if currentUser, err := user.Current(); err == nil {
cnfFiles = append(cnfFiles, filepath.Join(currentUser.HomeDir, cnfFileName))
}
cwd, _ := os.Getwd() // ignore err
cwdCnfFile := filepath.Join(cwd, cnfFileName)
cnfFiles = append(cnfFiles, cwdCnfFile)
iniParser := flags.NewIniParser(parser)
for _, cnfFile := range cnfFiles {
// skip if missing
if _, err := os.Stat(cnfFile); err != nil {
continue
}
if err := iniParser.ParseFile(cnfFile); err != nil {
return err
}
}
return nil
}
func readCredentialFile(filepath string) ([]byte, error) {
f, err := os.Open(filepath)
if err != nil {
return nil, err
}
return io.ReadAll(f)
}
func readStdin() (string, error) {
stat, _ := os.Stdin.Stat()
if (stat.Mode() & os.ModeCharDevice) == 0 {
b, err := io.ReadAll(os.Stdin)
if err != nil {
return "", err
}
return string(b), nil
} else {
return "", nil
}
}