Skip to content

Commit

Permalink
Merge pull request #5460 from sczk/issue-5436
Browse files Browse the repository at this point in the history
Prevent exponential growth in ~/.influx_history
  • Loading branch information
toddboom committed Jan 27, 2016
2 parents 9528c3e + 40e04d8 commit 3723680
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions cmd/influx/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ type CommandLine struct {
Quit chan struct{}
IgnoreSignals bool // Ignore signals normally caught by this process (used primarily for testing)
osSignals chan os.Signal
historyFile *os.File
historyFilePath string
}

// New returns an instance of CommandLine
Expand Down Expand Up @@ -160,14 +160,13 @@ func (c *CommandLine) Run() error {

c.Version()

var historyFilePath string
usr, err := user.Current()
// Only load/write history if we can get the user
if err == nil {
historyFilePath = filepath.Join(usr.HomeDir, ".influx_history")
if c.historyFile, err = os.OpenFile(historyFilePath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0640); err == nil {
defer c.historyFile.Close()
c.Line.ReadHistory(c.historyFile)
c.historyFilePath = filepath.Join(usr.HomeDir, ".influx_history")
if historyFile, err := os.Open(c.historyFilePath); err == nil {
c.Line.ReadHistory(historyFile)
historyFile.Close()
}
}

Expand All @@ -189,10 +188,7 @@ func (c *CommandLine) Run() error {
}
if c.ParseCommand(l) {
c.Line.AppendHistory(l)
_, err := c.Line.WriteHistory(c.historyFile)
if err != nil {
fmt.Printf("There was an error writing history file: %s\n", err)
}
c.saveHistory()
}
}
}
Expand Down Expand Up @@ -754,6 +750,15 @@ func (c *CommandLine) history() {
fmt.Print(buf.String())
}

func (c *CommandLine) saveHistory() {
if historyFile, err := os.Create(c.historyFilePath); err != nil {
fmt.Printf("There was an error writing history file: %s\n", err)
} else {
c.Line.WriteHistory(historyFile)
historyFile.Close()
}
}

func (c *CommandLine) gopher() {
fmt.Println(`
.-::-::://:-::- .:/++/'
Expand Down Expand Up @@ -818,10 +823,7 @@ func (c *CommandLine) Version() {

func (c *CommandLine) exit() {
// write to history file
_, err := c.Line.WriteHistory(c.historyFile)
if err != nil {
fmt.Printf("There was an error writing history file: %s\n", err)
}
c.saveHistory()
// release line resources
c.Line.Close()
c.Line = nil
Expand Down

0 comments on commit 3723680

Please sign in to comment.