Skip to content

Commit

Permalink
Merge pull request #25 from takashabe/add-history
Browse files Browse the repository at this point in the history
Add command histories
  • Loading branch information
takashabe authored Aug 26, 2018
2 parents 4a4943f + e8b0476 commit 1bb4965
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ lint: ## Check golint
generate: ## Run go generate
go generate $(SUBPACKAGES)

test-data: ## Initialize test data
./_tools/setup_bt.sh test-project test-instance dummy

##### Utilities

.PHONY: help
Expand Down
38 changes: 34 additions & 4 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,5 +111,17 @@ func (c *CLI) preparePrompt(conf *config.Config) (*prompt.Prompt, error) {
return prompt.New(
executor.Do,
completer.Do,
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: 5 additions & 0 deletions 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,6 +48,10 @@ func (e *Executor) Do(s string) {

for _, c := range commands {
if cmd == c.Name {
if e.history != nil {
fmt.Fprintln(e.history, strings.Join(args, " "))
}

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

0 comments on commit 1bb4965

Please sign in to comment.