Skip to content

Commit

Permalink
Support history
Browse files Browse the repository at this point in the history
  • Loading branch information
takashabe committed Aug 26, 2018
1 parent 54354a3 commit 82c8ccc
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
40 changes: 34 additions & 6 deletions api/interfaces/cli.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package interfaces

import (
"bufio"
"flag"
"fmt"
"io"
"os"
"os/user"

prompt "github.com/c-bata/go-prompt"
"github.com/pkg/errors"
Expand Down Expand Up @@ -39,7 +41,21 @@ func (c *CLI) Run(args []string) int {
return ExitCodeParseError
}

p, err := c.preparePrompt(conf)
histories := []string{}
f, err := loadHistoryFile(conf)
if err != nil {
// NOTE: Continue processing even if an error occurred at open a file
fmt.Fprintf(c.ErrStream, "failed to a history file open: %v\n", err)
} else {
// TODO: Read lines and set to histories
s := bufio.NewScanner(f)
for s.Scan() {
histories = append(histories, s.Text())
}
defer f.Close()
}

p, err := c.preparePrompt(conf, f, histories)
if err != nil {
fmt.Fprintf(c.ErrStream, "failed to initialized prompt: %v\n", err)
return ExitCodeError
Expand Down Expand Up @@ -72,7 +88,7 @@ func usage(w io.Writer) {
flag.CommandLine.PrintDefaults()
}

func (c *CLI) preparePrompt(conf *config.Config) (*prompt.Prompt, error) {
func (c *CLI) preparePrompt(conf *config.Config, writer io.Writer, histories []string) (*prompt.Prompt, error) {
repository, err := bigtable.NewBigtableRepository(conf.Project, conf.Instance)
if err != nil {
return nil, errors.Wrapf(err, "failed to initialized bigtable repository:%v", err)
Expand All @@ -81,8 +97,10 @@ func (c *CLI) preparePrompt(conf *config.Config) (*prompt.Prompt, error) {
rowsInteractor := application.NewRowsInteractor(repository)

executor := Executor{
outStream: c.OutStream,
errStream: c.ErrStream,
outStream: c.OutStream,
errStream: c.ErrStream,
history: writer,

rowsInteractor: rowsInteractor,
tableInteractor: tableInteractor,
}
Expand All @@ -93,7 +111,17 @@ func (c *CLI) preparePrompt(conf *config.Config) (*prompt.Prompt, error) {
return prompt.New(
executor.Do,
completer.Do,
// TODO: Add histories from the history file.
// prompt.OptionHistory(),
prompt.OptionHistory(histories),
prompt.OptionPreviewSuggestionTextColor(prompt.Blue),
prompt.OptionSelectedSuggestionBGColor(prompt.LightGray),
prompt.OptionSuggestionBGColor(prompt.DarkGray),
), nil
}

func loadHistoryFile(conf *config.Config) (*os.File, error) {
u, err := user.Current()
if err != nil {
return nil, err
}
return os.OpenFile(u.HomeDir+"/.btcli_history", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
}
5 changes: 4 additions & 1 deletion api/interfaces/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func init() {
type Executor struct {
outStream io.Writer
errStream io.Writer
history io.Writer

tableInteractor *application.TableInteractor
rowsInteractor *application.RowsInteractor
Expand All @@ -47,7 +48,9 @@ func (e *Executor) Do(s string) {

for _, c := range commands {
if cmd == c.Name {
// TODO: Add command histories to history file
if e.history != nil {
fmt.Fprintln(e.history, strings.Join(args, " "))
}

// TODO: extract args[0]
c.Runner(ctx, e, args...)
Expand Down

0 comments on commit 82c8ccc

Please sign in to comment.