Skip to content

Commit 4ad906e

Browse files
authored
Add option for setting a custom API endpoint (#162)
Useful for testing.
1 parent a80699f commit 4ad906e

2 files changed

Lines changed: 14 additions & 9 deletions

File tree

cli.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,16 @@ type Cli struct {
6767
ErrStream io.Writer
6868
Verbose bool
6969
Priority pb.RequestOptions_Priority
70+
Endpoint string
7071
}
7172

7273
type command struct {
7374
Stmt Statement
7475
Vertical bool
7576
}
7677

77-
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) (*Cli, error) {
78-
session, err := createSession(projectId, instanceId, databaseId, credential, priority, role)
78+
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) (*Cli, error) {
79+
session, err := createSession(projectId, instanceId, databaseId, credential, priority, role, endpoint)
7980
if err != nil {
8081
return nil, err
8182
}
@@ -97,6 +98,7 @@ func NewCli(projectId, instanceId, databaseId, prompt, historyFile string, crede
9798
OutStream: outStream,
9899
ErrStream: errStream,
99100
Verbose: verbose,
101+
Endpoint: endpoint,
100102
}, nil
101103
}
102104

@@ -146,7 +148,7 @@ func (c *Cli) RunInteractive() int {
146148
}
147149

148150
if s, ok := stmt.(*UseStatement); ok {
149-
newSession, err := createSession(c.Session.projectId, c.Session.instanceId, s.Database, c.Credential, c.Priority, s.Role)
151+
newSession, err := createSession(c.Session.projectId, c.Session.instanceId, s.Database, c.Credential, c.Priority, s.Role, c.Endpoint)
150152
if err != nil {
151153
c.PrintInteractiveError(err)
152154
continue
@@ -308,13 +310,15 @@ func (c *Cli) getInterpolatedPrompt() string {
308310
return prompt
309311
}
310312

311-
func createSession(projectId string, instanceId string, databaseId string, credential []byte, priority pb.RequestOptions_Priority, role string) (*Session, error) {
313+
func createSession(projectId string, instanceId string, databaseId string, credential []byte, priority pb.RequestOptions_Priority, role string, endpoint string) (*Session, error) {
314+
var opts []option.ClientOption
312315
if credential != nil {
313-
credentialOption := option.WithCredentialsJSON(credential)
314-
return NewSession(projectId, instanceId, databaseId, priority, role, credentialOption)
315-
} else {
316-
return NewSession(projectId, instanceId, databaseId, priority, role)
316+
opts = append(opts, option.WithCredentialsJSON(credential))
317+
}
318+
if endpoint != "" {
319+
opts = append(opts, option.WithEndpoint(endpoint))
317320
}
321+
return NewSession(projectId, instanceId, databaseId, priority, role, opts...)
318322
}
319323

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

main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ type spannerOptions struct {
4545
HistoryFile string `long:"history" description:"Set the history file to the specified path"`
4646
Priority string `long:"priority" description:"Set default request priority (HIGH|MEDIUM|LOW)"`
4747
Role string `long:"role" description:"Use the specific database role"`
48+
Endpoint string `long:"endpoint" description:"Set the Spanner API endpoint (host:port)"`
4849
}
4950

5051
func main() {
@@ -85,7 +86,7 @@ func main() {
8586
}
8687
}
8788

88-
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)
89+
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)
8990
if err != nil {
9091
exitf("Failed to connect to Spanner: %v", err)
9192
}

0 commit comments

Comments
 (0)