From 4d3ca3fa2ba2dffb013618321bff3458043b970a Mon Sep 17 00:00:00 2001 From: kaidaguerre Date: Tue, 7 Jun 2022 14:08:02 +0100 Subject: [PATCH] Fix interactive query failing with EOF error if the history.json is empty. Closes #2151 --- query/queryhistory/history.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/query/queryhistory/history.go b/query/queryhistory/history.go index 5fecb7c29..a98f5493d 100644 --- a/query/queryhistory/history.go +++ b/query/queryhistory/history.go @@ -2,6 +2,7 @@ package queryhistory import ( "encoding/json" + "io" "os" "path/filepath" "strings" @@ -17,7 +18,7 @@ type QueryHistory struct { // New creates a new QueryHistory object func New() (*QueryHistory, error) { - history := new(QueryHistory) + history := &QueryHistory{history: []string{}} err := history.load() if err != nil { return nil, err @@ -87,7 +88,6 @@ func (q *QueryHistory) load() error { path := filepath.Join(filepaths.EnsureInternalDir(), constants.HistoryFile) file, err := os.Open(path) if err != nil { - q.history = []string{} // ignore not exists errors if os.IsNotExist(err) { return nil @@ -98,5 +98,10 @@ func (q *QueryHistory) load() error { defer file.Close() decoder := json.NewDecoder(file) - return decoder.Decode(&q.history) + err = decoder.Decode(&q.history) + // ignore EOF (caused by empty file) + if err == io.EOF { + return nil + } + return err }