Skip to content

Commit

Permalink
Add option for setting a custom API endpoint (#162)
Browse files Browse the repository at this point in the history
Useful for testing.
  • Loading branch information
c2nes authored Feb 19, 2024
1 parent a80699f commit 4ad906e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
20 changes: 12 additions & 8 deletions cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,16 @@ type Cli struct {
ErrStream io.Writer
Verbose bool
Priority pb.RequestOptions_Priority
Endpoint string
}

type command struct {
Stmt Statement
Vertical bool
}

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) {
session, err := createSession(projectId, instanceId, databaseId, credential, priority, role)
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) {
session, err := createSession(projectId, instanceId, databaseId, credential, priority, role, endpoint)
if err != nil {
return nil, err
}
Expand All @@ -97,6 +98,7 @@ func NewCli(projectId, instanceId, databaseId, prompt, historyFile string, crede
OutStream: outStream,
ErrStream: errStream,
Verbose: verbose,
Endpoint: endpoint,
}, nil
}

Expand Down Expand Up @@ -146,7 +148,7 @@ func (c *Cli) RunInteractive() int {
}

if s, ok := stmt.(*UseStatement); ok {
newSession, err := createSession(c.Session.projectId, c.Session.instanceId, s.Database, c.Credential, c.Priority, s.Role)
newSession, err := createSession(c.Session.projectId, c.Session.instanceId, s.Database, c.Credential, c.Priority, s.Role, c.Endpoint)
if err != nil {
c.PrintInteractiveError(err)
continue
Expand Down Expand Up @@ -308,13 +310,15 @@ func (c *Cli) getInterpolatedPrompt() string {
return prompt
}

func createSession(projectId string, instanceId string, databaseId string, credential []byte, priority pb.RequestOptions_Priority, role string) (*Session, error) {
func createSession(projectId string, instanceId string, databaseId string, credential []byte, priority pb.RequestOptions_Priority, role string, endpoint string) (*Session, error) {
var opts []option.ClientOption
if credential != nil {
credentialOption := option.WithCredentialsJSON(credential)
return NewSession(projectId, instanceId, databaseId, priority, role, credentialOption)
} else {
return NewSession(projectId, instanceId, databaseId, priority, role)
opts = append(opts, option.WithCredentialsJSON(credential))
}
if endpoint != "" {
opts = append(opts, option.WithEndpoint(endpoint))
}
return NewSession(projectId, instanceId, databaseId, priority, role, opts...)
}

func readInteractiveInput(rl *readline.Instance, prompt string) (*inputStatement, error) {
Expand Down
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type spannerOptions struct {
HistoryFile string `long:"history" description:"Set the history file to the specified path"`
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)"`
}

func main() {
Expand Down Expand Up @@ -85,7 +86,7 @@ func main() {
}
}

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)
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)
if err != nil {
exitf("Failed to connect to Spanner: %v", err)
}
Expand Down

0 comments on commit 4ad906e

Please sign in to comment.