From 2ccbe264476c9b0167c2cb092bfe4a5c817773e0 Mon Sep 17 00:00:00 2001 From: "Jonathan A. Sternberg" Date: Wed, 27 Apr 2016 16:55:07 -0500 Subject: [PATCH] Fix the CLI not to enter an infinite loop when the liner has an error This also removes the dependency on `os/user` and uses the `HOME` environment variable which is more common on Linux and Mac OS X for customizing the history file location. Removing this import also lets the `influx` binary be cross-compiled as `os/user` relies on cgo. --- CHANGELOG.md | 5 +++-- cmd/influx/cli/cli.go | 17 +++++++++++------ 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 55cda7d288..aa5b862fae 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -43,8 +43,9 @@ - [#6462](https://github.com/influxdata/influxdb/pull/6462): Add safer locking to CreateFieldIfNotExists - [#6361](https://github.com/influxdata/influxdb/pull/6361): Fix cluster/pool release of connection - [#6470](https://github.com/influxdata/influxdb/pull/6470): Remove SHOW SERVERS & DROP SERVER support -- [#6477] (https://github.com/influxdata/influxdb/pull/6477): Don't catch SIGQUIT or SIGHUP signals. -- [#6468](https://github.com/influxdata/influxdb/issues/6468): Panic with truncated wal segments +- [#6477](https://github.com/influxdata/influxdb/pull/6477): Don't catch SIGQUIT or SIGHUP signals. +- [#6468](https://github.com/influxdata/influxdb/issues/6468): Panic with truncated wal segments +- [#6491](https://github.com/influxdata/influxdb/pull/6491): Fix the CLI not to enter an infinite loop when the liner has an error. ## v0.12.2 [2016-04-20] diff --git a/cmd/influx/cli/cli.go b/cmd/influx/cli/cli.go index 60ad87e216..d6eaf11aa0 100644 --- a/cmd/influx/cli/cli.go +++ b/cmd/influx/cli/cli.go @@ -11,7 +11,6 @@ import ( "net/url" "os" "os/signal" - "os/user" "path/filepath" "sort" "strconv" @@ -168,10 +167,10 @@ func (c *CommandLine) Run() error { c.Version() - usr, err := user.Current() - // Only load/write history if we can get the user - if err == nil { - c.historyFilePath = filepath.Join(usr.HomeDir, ".influx_history") + // Only load/write history if HOME environment variable is set. + if homeDir := os.Getenv("HOME"); homeDir != "" { + // Attempt to load the history file. + c.historyFilePath = filepath.Join(homeDir, ".influx_history") if historyFile, err := os.Open(c.historyFilePath); err == nil { c.Line.ReadHistory(historyFile) historyFile.Close() @@ -179,6 +178,11 @@ func (c *CommandLine) Run() error { } // read from prompt until exit is run + return c.mainLoop() +} + +// mainLoop runs the main prompt loop for the CLI. +func (c *CommandLine) mainLoop() error { for { select { case <-c.osSignals: @@ -193,7 +197,8 @@ func (c *CommandLine) Run() error { // Instead of die, register that someone exited the program gracefully l = "exit" } else if e != nil { - break + c.exit() + return e } if err := c.ParseCommand(l); err != ErrBlankCommand { c.Line.AppendHistory(l)