Fix interactive query failing with EOF error if the history.json is empty. Closes #2151

This commit is contained in:
kaidaguerre
2022-06-07 14:08:02 +01:00
committed by GitHub
parent a7db94aff9
commit 4d3ca3fa2b

View File

@@ -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
}