diff --git a/cli.go b/cli.go index 442eb4d..f3d5c3c 100644 --- a/cli.go +++ b/cli.go @@ -21,7 +21,6 @@ import ( "context" "errors" "fmt" - "google.golang.org/grpc/codes" "io" "os" "os/signal" @@ -33,8 +32,8 @@ import ( "github.com/chzyer/readline" "github.com/olekukonko/tablewriter" "google.golang.org/api/option" - pb "google.golang.org/genproto/googleapis/spanner/v1" + "google.golang.org/grpc/codes" ) type DisplayMode int @@ -44,7 +43,8 @@ const ( DisplayModeVertical DisplayModeTab - defaultPrompt = `spanner\t> ` + defaultPrompt = `spanner\t> ` + defaultHistoryFile = `/tmp/spanner_cli_readline.tmp` exitCodeSuccess = 0 exitCodeError = 1 @@ -58,14 +58,15 @@ var ( ) type Cli struct { - Session *Session - Prompt string - Credential []byte - InStream io.ReadCloser - OutStream io.Writer - ErrStream io.Writer - Verbose bool - Priority pb.RequestOptions_Priority + Session *Session + Prompt string + HistoryFile string + Credential []byte + InStream io.ReadCloser + OutStream io.Writer + ErrStream io.Writer + Verbose bool + Priority pb.RequestOptions_Priority } type command struct { @@ -73,7 +74,7 @@ type command struct { Vertical bool } -func NewCli(projectId, instanceId, databaseId string, prompt string, credential []byte, inStream io.ReadCloser, outStream io.Writer, errStream io.Writer, verbose bool, priority pb.RequestOptions_Priority) (*Cli, error) { +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) (*Cli, error) { session, err := createSession(projectId, instanceId, databaseId, credential, priority) if err != nil { return nil, err @@ -83,21 +84,26 @@ func NewCli(projectId, instanceId, databaseId string, prompt string, credential prompt = defaultPrompt } + if historyFile == "" { + historyFile = defaultHistoryFile + } + return &Cli{ - Session: session, - Prompt: prompt, - Credential: credential, - InStream: inStream, - OutStream: outStream, - ErrStream: errStream, - Verbose: verbose, + Session: session, + Prompt: prompt, + HistoryFile: historyFile, + Credential: credential, + InStream: inStream, + OutStream: outStream, + ErrStream: errStream, + Verbose: verbose, }, nil } func (c *Cli) RunInteractive() int { rl, err := readline.NewEx(&readline.Config{ Stdin: c.InStream, - HistoryFile: "/tmp/spanner_cli_readline.tmp", + HistoryFile: c.HistoryFile, }) if err != nil { return c.ExitOnError(err) diff --git a/main.go b/main.go index 50dcf21..329c0da 100644 --- a/main.go +++ b/main.go @@ -33,16 +33,17 @@ type globalOptions struct { } type spannerOptions struct { - ProjectId string `short:"p" long:"project" env:"SPANNER_PROJECT_ID" description:"(required) GCP Project ID."` - InstanceId string `short:"i" long:"instance" env:"SPANNER_INSTANCE_ID" description:"(required) Cloud Spanner Instance ID"` - DatabaseId string `short:"d" long:"database" env:"SPANNER_DATABASE_ID" description:"(required) Cloud Spanner Database ID."` - Execute string `short:"e" long:"execute" description:"Execute SQL statement and quit."` - File string `short:"f" long:"file" description:"Execute SQL statement from file and quit."` - Table bool `short:"t" long:"table" description:"Display output in table format for batch mode."` - Verbose bool `short:"v" long:"verbose" 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"` - Priority string `long:"priority" description:"Set default request priority (HIGH|MEDIUM|LOW)"` + ProjectId string `short:"p" long:"project" env:"SPANNER_PROJECT_ID" description:"(required) GCP Project ID."` + InstanceId string `short:"i" long:"instance" env:"SPANNER_INSTANCE_ID" description:"(required) Cloud Spanner Instance ID"` + DatabaseId string `short:"d" long:"database" env:"SPANNER_DATABASE_ID" description:"(required) Cloud Spanner Database ID."` + Execute string `short:"e" long:"execute" description:"Execute SQL statement and quit."` + File string `short:"f" long:"file" description:"Execute SQL statement from file and quit."` + Table bool `short:"t" long:"table" description:"Display output in table format for batch mode."` + Verbose bool `short:"v" long:"verbose" 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"` + 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)"` } func main() { @@ -83,7 +84,7 @@ func main() { } } - cli, err := NewCli(opts.ProjectId, opts.InstanceId, opts.DatabaseId, opts.Prompt, cred, os.Stdin, os.Stdout, os.Stderr, opts.Verbose, priority) + cli, err := NewCli(opts.ProjectId, opts.InstanceId, opts.DatabaseId, opts.Prompt, opts.HistoryFile, cred, os.Stdin, os.Stdout, os.Stderr, opts.Verbose, priority) if err != nil { exitf("Failed to connect to Spanner: %v", err) }