Skip to content

Commit

Permalink
Merge pull request #4702 from pires/4628-cli_history
Browse files Browse the repository at this point in the history
Implement CLI history command
  • Loading branch information
otoolep committed Nov 9, 2015
2 parents 421e31b + b411316 commit fbfa175
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
14 changes: 14 additions & 0 deletions cmd/influx/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/influxdb/influxdb/cluster"
"github.com/influxdb/influxdb/importer/v8"
"github.com/peterh/liner"
"io/ioutil"
)

// These variables are populated via the Go linker.
Expand Down Expand Up @@ -273,6 +274,8 @@ func (c *CommandLine) ParseCommand(cmd string) bool {
c.SetAuth(cmd)
case strings.HasPrefix(lcmd, "help"):
c.help()
case strings.HasPrefix(lcmd, "history"):
c.history()
case strings.HasPrefix(lcmd, "format"):
c.SetFormat(cmd)
case strings.HasPrefix(lcmd, "precision"):
Expand Down Expand Up @@ -755,6 +758,17 @@ func (c *CommandLine) help() {
`)
}

func (c *CommandLine) history() {
usr, err := user.Current()
// Only load history if we can get the user
if err == nil {
historyFile := filepath.Join(usr.HomeDir, ".influx_history")
if history, err := ioutil.ReadFile(historyFile); err == nil {
fmt.Print(string(history))
}
}
}

func (c *CommandLine) gopher() {
fmt.Println(`
.-::-::://:-::- .:/++/'
Expand Down
20 changes: 20 additions & 0 deletions cmd/influx/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func TestParseCommand_CommandsExist(t *testing.T) {
{cmd: "gopher"},
{cmd: "connect"},
{cmd: "help"},
{cmd: "history"},
{cmd: "pretty"},
{cmd: "use"},
{cmd: ""}, // test that a blank command just returns
Expand Down Expand Up @@ -217,3 +218,22 @@ func TestParseCommand_InsertInto(t *testing.T) {
}
}
}

func TestParseCommand_History(t *testing.T) {
t.Parallel()
c := main.CommandLine{}
tests := []struct {
cmd string
}{
{cmd: "history"},
{cmd: " history"},
{cmd: "history "},
{cmd: "History "},
}

for _, test := range tests {
if !c.ParseCommand(test.cmd) {
t.Fatalf(`Command "history" failed for %q.`, test.cmd)
}
}
}

0 comments on commit fbfa175

Please sign in to comment.