From b41131643dfb7330a5689b3069e8fa63993302a1 Mon Sep 17 00:00:00 2001 From: Paulo Pires Date: Fri, 6 Nov 2015 22:20:28 -0500 Subject: [PATCH] Implement CLI history command. Fixes #4628 --- cmd/influx/main.go | 14 ++++++++++++++ cmd/influx/main_test.go | 20 ++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/cmd/influx/main.go b/cmd/influx/main.go index bb1da6dad17..854cd90dd4a 100644 --- a/cmd/influx/main.go +++ b/cmd/influx/main.go @@ -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. @@ -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"): @@ -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(` .-::-::://:-::- .:/++/' diff --git a/cmd/influx/main_test.go b/cmd/influx/main_test.go index 045fa319ef3..927d71670db 100644 --- a/cmd/influx/main_test.go +++ b/cmd/influx/main_test.go @@ -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 @@ -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) + } + } +}