Skip to content

Commit ed6c09a

Browse files
authored
Implement --proto-descriptor-file (#195)
* Implement --proto-descriptor-file * Update README.md * Fix UseStatement with --proto-descriptor-file * Do go mod tidy * Add comments * Update a comment * go mod tidy with Go 1.19.13 * Fix to reflect review comment * Fix variable name
1 parent dd056dc commit ed6c09a

File tree

8 files changed

+65
-41
lines changed

8 files changed

+65
-41
lines changed

README.md

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,24 +37,25 @@ Usage:
3737
spanner-cli [OPTIONS]
3838
3939
spanner:
40-
-p, --project= (required) GCP Project ID. [$SPANNER_PROJECT_ID]
41-
-i, --instance= (required) Cloud Spanner Instance ID [$SPANNER_INSTANCE_ID]
42-
-d, --database= (required) Cloud Spanner Database ID. [$SPANNER_DATABASE_ID]
43-
-e, --execute= Execute SQL statement and quit.
44-
-f, --file= Execute SQL statement from file and quit.
45-
-t, --table Display output in table format for batch mode.
46-
-v, --verbose Display verbose output.
47-
--credential= Use the specific credential file
48-
--prompt= Set the prompt to the specified format
49-
--history= Set the history file to the specified path
50-
--priority= Set default request priority (HIGH|MEDIUM|LOW)
51-
--role= Use the specific database role
52-
--endpoint= Set the Spanner API endpoint (host:port)
53-
--directed-read= Directed read option (replica_location:replica_type). The replicat_type is optional and either READ_ONLY or READ_WRITE
54-
--skip-tls-verify Insecurely skip TLS verify
40+
-p, --project= (required) GCP Project ID. [$SPANNER_PROJECT_ID]
41+
-i, --instance= (required) Cloud Spanner Instance ID [$SPANNER_INSTANCE_ID]
42+
-d, --database= (required) Cloud Spanner Database ID. [$SPANNER_DATABASE_ID]
43+
-e, --execute= Execute SQL statement and quit.
44+
-f, --file= Execute SQL statement from file and quit.
45+
-t, --table Display output in table format for batch mode.
46+
-v, --verbose Display verbose output.
47+
--credential= Use the specific credential file
48+
--prompt= Set the prompt to the specified format
49+
--history= Set the history file to the specified path
50+
--priority= Set default request priority (HIGH|MEDIUM|LOW)
51+
--role= Use the specific database role
52+
--endpoint= Set the Spanner API endpoint (host:port)
53+
--directed-read= Directed read option (replica_location:replica_type). The replicat_type is optional and either READ_ONLY or READ_WRITE
54+
--skip-tls-verify Insecurely skip TLS verify
55+
--proto-descriptor-file= Path of a file that contains a protobuf-serialized google.protobuf.FileDescriptorSet message to use in this invocation.
5556
5657
Help Options:
57-
-h, --help Show this help message
58+
-h, --help Show this help message
5859
```
5960

6061
### Authentication

cli.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,11 @@ type command struct {
7979
Vertical bool
8080
}
8181

82-
func NewCli(projectId, instanceId, databaseId, prompt, historyFile string, credential []byte, inStream io.ReadCloser, outStream io.Writer, errStream io.Writer, verbose bool, priority pb.RequestOptions_Priority, role string, endpoint string, directedRead *pb.DirectedReadOptions, skipTLSVerify bool) (*Cli, error) {
83-
session, err := createSession(projectId, instanceId, databaseId, credential, priority, role, endpoint, directedRead, skipTLSVerify)
82+
func NewCli(projectId, instanceId, databaseId, prompt, historyFile string, credential []byte,
83+
inStream io.ReadCloser, outStream, errStream io.Writer, verbose bool,
84+
priority pb.RequestOptions_Priority, role, endpoint string, directedRead *pb.DirectedReadOptions,
85+
skipTLSVerify bool, protoDescriptor []byte) (*Cli, error) {
86+
session, err := createSession(projectId, instanceId, databaseId, credential, priority, role, endpoint, directedRead, skipTLSVerify, protoDescriptor)
8487
if err != nil {
8588
return nil, err
8689
}
@@ -153,7 +156,8 @@ func (c *Cli) RunInteractive() int {
153156
}
154157

155158
if s, ok := stmt.(*UseStatement); ok {
156-
newSession, err := createSession(c.Session.projectId, c.Session.instanceId, s.Database, c.Credential, c.Priority, s.Role, c.Endpoint, c.Session.directedRead, c.SkipTLSVerify)
159+
newSession, err := createSession(c.Session.projectId, c.Session.instanceId, s.Database, c.Credential, c.Priority,
160+
s.Role, c.Endpoint, c.Session.directedRead, c.SkipTLSVerify, c.Session.protoDescriptor)
157161
if err != nil {
158162
c.PrintInteractiveError(err)
159163
continue
@@ -315,7 +319,9 @@ func (c *Cli) getInterpolatedPrompt() string {
315319
return prompt
316320
}
317321

318-
func createSession(projectId string, instanceId string, databaseId string, credential []byte, priority pb.RequestOptions_Priority, role string, endpoint string, directedRead *pb.DirectedReadOptions, skipTLSVerify bool) (*Session, error) {
322+
func createSession(projectId string, instanceId string, databaseId string, credential []byte,
323+
priority pb.RequestOptions_Priority, role string, endpoint string, directedRead *pb.DirectedReadOptions,
324+
skipTLSVerify bool, protoDescriptor []byte) (*Session, error) {
319325
var opts []option.ClientOption
320326
if credential != nil {
321327
opts = append(opts, option.WithCredentialsJSON(credential))
@@ -327,7 +333,7 @@ func createSession(projectId string, instanceId string, databaseId string, crede
327333
creds := credentials.NewTLS(&tls.Config{InsecureSkipVerify: true})
328334
opts = append(opts, option.WithGRPCDialOption(grpc.WithTransportCredentials(creds)))
329335
}
330-
return NewSession(projectId, instanceId, databaseId, priority, role, directedRead, opts...)
336+
return NewSession(projectId, instanceId, databaseId, priority, role, directedRead, protoDescriptor, opts...)
331337
}
332338

333339
func readInteractiveInput(rl *readline.Instance, prompt string) (*inputStatement, error) {

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ require (
77
cloud.google.com/go/spanner v1.62.0
88
github.com/apstndb/gsqlsep v0.0.0-20230324124551-0e8335710080
99
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e
10-
github.com/davecgh/go-spew v1.1.1
1110
github.com/google/go-cmp v0.6.0
1211
github.com/jessevdk/go-flags v1.4.0
1312
github.com/olekukonko/tablewriter v0.0.5

integration_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ func setup(t *testing.T, ctx context.Context, dmls []string) (*Session, string,
8686
if testCredential != "" {
8787
options = append(options, option.WithCredentialsJSON([]byte(testCredential)))
8888
}
89-
session, err := NewSession(testProjectId, testInstanceId, testDatabaseId, pb.RequestOptions_PRIORITY_UNSPECIFIED, "", nil, options...)
89+
session, err := NewSession(testProjectId, testInstanceId, testDatabaseId, pb.RequestOptions_PRIORITY_UNSPECIFIED, "", nil, nil, options...)
9090
if err != nil {
9191
t.Fatalf("failed to create test session: err=%s", err)
9292
}

main.go

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,22 @@ type globalOptions struct {
3333
}
3434

3535
type spannerOptions struct {
36-
ProjectId string `short:"p" long:"project" env:"SPANNER_PROJECT_ID" description:"(required) GCP Project ID."`
37-
InstanceId string `short:"i" long:"instance" env:"SPANNER_INSTANCE_ID" description:"(required) Cloud Spanner Instance ID"`
38-
DatabaseId string `short:"d" long:"database" env:"SPANNER_DATABASE_ID" description:"(required) Cloud Spanner Database ID."`
39-
Execute string `short:"e" long:"execute" description:"Execute SQL statement and quit."`
40-
File string `short:"f" long:"file" description:"Execute SQL statement from file and quit."`
41-
Table bool `short:"t" long:"table" description:"Display output in table format for batch mode."`
42-
Verbose bool `short:"v" long:"verbose" description:"Display verbose output."`
43-
Credential string `long:"credential" description:"Use the specific credential file"`
44-
Prompt string `long:"prompt" description:"Set the prompt to the specified format"`
45-
HistoryFile string `long:"history" description:"Set the history file to the specified path"`
46-
Priority string `long:"priority" description:"Set default request priority (HIGH|MEDIUM|LOW)"`
47-
Role string `long:"role" description:"Use the specific database role"`
48-
Endpoint string `long:"endpoint" description:"Set the Spanner API endpoint (host:port)"`
49-
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"`
50-
SkipTLSVerify bool `long:"skip-tls-verify" description:"Insecurely skip TLS verify"`
36+
ProjectId string `short:"p" long:"project" env:"SPANNER_PROJECT_ID" description:"(required) GCP Project ID."`
37+
InstanceId string `short:"i" long:"instance" env:"SPANNER_INSTANCE_ID" description:"(required) Cloud Spanner Instance ID"`
38+
DatabaseId string `short:"d" long:"database" env:"SPANNER_DATABASE_ID" description:"(required) Cloud Spanner Database ID."`
39+
Execute string `short:"e" long:"execute" description:"Execute SQL statement and quit."`
40+
File string `short:"f" long:"file" description:"Execute SQL statement from file and quit."`
41+
Table bool `short:"t" long:"table" description:"Display output in table format for batch mode."`
42+
Verbose bool `short:"v" long:"verbose" description:"Display verbose output."`
43+
Credential string `long:"credential" description:"Use the specific credential file"`
44+
Prompt string `long:"prompt" description:"Set the prompt to the specified format"`
45+
HistoryFile string `long:"history" description:"Set the history file to the specified path"`
46+
Priority string `long:"priority" description:"Set default request priority (HIGH|MEDIUM|LOW)"`
47+
Role string `long:"role" description:"Use the specific database role"`
48+
Endpoint string `long:"endpoint" description:"Set the Spanner API endpoint (host:port)"`
49+
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"`
50+
SkipTLSVerify bool `long:"skip-tls-verify" description:"Insecurely skip TLS verify"`
51+
ProtoDescriptorFile string `long:"proto-descriptor-file" description:"Path of a file that contains a protobuf-serialized google.protobuf.FileDescriptorSet message to use in this invocation."`
5152
}
5253

5354
func main() {
@@ -97,7 +98,19 @@ func main() {
9798
}
9899
}
99100

100-
cli, err := NewCli(opts.ProjectId, opts.InstanceId, opts.DatabaseId, opts.Prompt, opts.HistoryFile, cred, os.Stdin, os.Stdout, os.Stderr, opts.Verbose, priority, opts.Role, opts.Endpoint, directedRead, opts.SkipTLSVerify)
101+
// Don't need to unmarshal into descriptorpb.FileDescriptorSet because the UpdateDDL API just accepts []byte.
102+
var protoDescriptor []byte
103+
if opts.ProtoDescriptorFile != "" {
104+
var err error
105+
protoDescriptor, err = os.ReadFile(opts.ProtoDescriptorFile)
106+
if err != nil {
107+
exitf("Failed to read proto descriptor file: %v\n", err)
108+
}
109+
}
110+
111+
cli, err := NewCli(opts.ProjectId, opts.InstanceId, opts.DatabaseId, opts.Prompt, opts.HistoryFile, cred,
112+
os.Stdin, os.Stdout, os.Stderr, opts.Verbose, priority, opts.Role, opts.Endpoint, directedRead,
113+
opts.SkipTLSVerify, protoDescriptor)
101114
if err != nil {
102115
exitf("Failed to connect to Spanner: %v", err)
103116
}

session.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ type Session struct {
5858
directedRead *pb.DirectedReadOptions
5959
tc *transactionContext
6060
tcMutex sync.Mutex // Guard a critical section for transaction.
61+
protoDescriptor []byte
6162
}
6263

6364
type transactionContext struct {
@@ -68,7 +69,8 @@ type transactionContext struct {
6869
roTxn *spanner.ReadOnlyTransaction
6970
}
7071

71-
func NewSession(projectId string, instanceId string, databaseId string, priority pb.RequestOptions_Priority, role string, directedRead *pb.DirectedReadOptions, opts ...option.ClientOption) (*Session, error) {
72+
func NewSession(projectId string, instanceId string, databaseId string, priority pb.RequestOptions_Priority, role string, directedRead *pb.DirectedReadOptions,
73+
protoDescriptor []byte, opts ...option.ClientOption) (*Session, error) {
7274
ctx := context.Background()
7375
dbPath := fmt.Sprintf("projects/%s/instances/%s/databases/%s", projectId, instanceId, databaseId)
7476
clientConfig := defaultClientConfig
@@ -99,6 +101,7 @@ func NewSession(projectId string, instanceId string, databaseId string, priority
99101
adminClient: adminClient,
100102
defaultPriority: priority,
101103
directedRead: directedRead,
104+
protoDescriptor: protoDescriptor,
102105
}
103106
go session.startHeartbeat()
104107

session_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func TestRequestPriority(t *testing.T) {
6666
t.Run(test.desc, func(t *testing.T) {
6767
defer recorder.flush()
6868

69-
session, err := NewSession("project", "instance", "database", test.sessionPriority, "role", nil, option.WithGRPCConn(conn))
69+
session, err := NewSession("project", "instance", "database", test.sessionPriority, "role", nil, nil, option.WithGRPCConn(conn))
7070
if err != nil {
7171
t.Fatalf("failed to create spanner-cli session: %v", err)
7272
}

statement.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,8 @@ func executeDdlStatements(ctx context.Context, session *Session, ddls []string)
406406
op, err := session.adminClient.UpdateDatabaseDdl(ctx, &adminpb.UpdateDatabaseDdlRequest{
407407
Database: session.DatabasePath(),
408408
Statements: ddls,
409+
// There is no problem to send ProtoDescriptors with any DDL statements
410+
ProtoDescriptors: session.protoDescriptor,
409411
})
410412
if err != nil {
411413
return nil, err

0 commit comments

Comments
 (0)