Skip to content

Commit

Permalink
Make the history file path to be configurable (#138)
Browse files Browse the repository at this point in the history
  • Loading branch information
110y authored Jul 12, 2022
1 parent 06ce1ea commit baf8c22
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 31 deletions.
46 changes: 26 additions & 20 deletions cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"context"
"errors"
"fmt"
"google.golang.org/grpc/codes"
"io"
"os"
"os/signal"
Expand All @@ -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
Expand All @@ -44,7 +43,8 @@ const (
DisplayModeVertical
DisplayModeTab

defaultPrompt = `spanner\t> `
defaultPrompt = `spanner\t> `
defaultHistoryFile = `/tmp/spanner_cli_readline.tmp`

exitCodeSuccess = 0
exitCodeError = 1
Expand All @@ -58,22 +58,23 @@ 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 {
Stmt Statement
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
Expand All @@ -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)
Expand Down
23 changes: 12 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit baf8c22

Please sign in to comment.