From a7722e028a43939dea18b4478b6954cc3ae5efdd Mon Sep 17 00:00:00 2001 From: Markus Pesch Date: Sun, 29 Sep 2019 18:29:06 +0200 Subject: [PATCH] fix(pkg/storage/logfile): create logfile if not exists --- pkg/storage/logfile/csv.go | 14 +++++++++++++- pkg/storage/logfile/errors.go | 10 ++++++---- pkg/storage/logfile/json.go | 13 ++++++++++++- pkg/storage/logfile/xml.go | 14 +++++++++++++- 4 files changed, 44 insertions(+), 7 deletions(-) diff --git a/pkg/storage/logfile/csv.go b/pkg/storage/logfile/csv.go index de4eaed..fcbd68a 100644 --- a/pkg/storage/logfile/csv.go +++ b/pkg/storage/logfile/csv.go @@ -4,6 +4,7 @@ import ( "encoding/csv" "fmt" "os" + "path/filepath" "strconv" "time" @@ -16,8 +17,19 @@ type csvLogfile struct { } func (cl *csvLogfile) Read() ([]*types.MeasuredValue, error) { + if _, err := os.Stat(cl.logfile); os.IsNotExist(err) { - return nil, fmt.Errorf("%v: %v", errorLogfileNotFound, cl.logfile) + if _, err := os.Stat(filepath.Dir(cl.logfile)); os.IsNotExist(err) { + if err := os.MkdirAll(filepath.Dir(cl.logfile), 0755); err != nil { + return nil, fmt.Errorf("%v: %v", errorDirectoryCreate, filepath.Dir(cl.logfile)) + } + } + + f, err := os.Create(cl.logfile) + if err != nil { + return nil, fmt.Errorf("%v: %v", errorLogfileCreate, cl.logfile) + } + f.Close() } f, err := os.Open(cl.logfile) diff --git a/pkg/storage/logfile/errors.go b/pkg/storage/logfile/errors.go index 2a078ca..621fed3 100644 --- a/pkg/storage/logfile/errors.go +++ b/pkg/storage/logfile/errors.go @@ -3,6 +3,8 @@ package logfile import "errors" var ( + errorDirectoryCreate = errors.New("Can not create directory") + errorLogfileCreate = errors.New("Can not create logfile") errorLogfileDecode = errors.New("Can not decode from reader") errorLogfileEncode = errors.New("Can not encode from writer") @@ -13,10 +15,6 @@ var ( errorLogfileUnmarshal = errors.New("Can not unmarshal values") errorLogfileWrite = errors.New("Can not write with given writer") - errorParseFloat = errors.New("Can not parse float") - errorParseMeasurementUnit = errors.New("Can not parse mesaurement unit") - errorParseTime = errors.New("Can not parse time") - errorNoValidHumidityID = errors.New("No valid humidity id detected or available") errorNoValidMesuredValue = errors.New("No mesured value detected or available") errorNoValidSensorID = errors.New("No sensor id detected or available") @@ -24,5 +22,9 @@ var ( errorNoValidTime = errors.New("No time detected or available") errorNoValidTimePeriods = errors.New("No valid time periods") + errorParseFloat = errors.New("Can not parse float") + errorParseMeasurementUnit = errors.New("Can not parse mesaurement unit") + errorParseTime = errors.New("Can not parse time") + errorTypeSwitch = errors.New("Can not detect type via type switch") ) diff --git a/pkg/storage/logfile/json.go b/pkg/storage/logfile/json.go index 5f9209b..d97f6f7 100644 --- a/pkg/storage/logfile/json.go +++ b/pkg/storage/logfile/json.go @@ -16,7 +16,18 @@ type jsonLogfile struct { func (jl *jsonLogfile) Read() ([]*types.MeasuredValue, error) { if _, err := os.Stat(jl.logfile); os.IsNotExist(err) { - return nil, fmt.Errorf("%v: %v", errorLogfileNotFound, jl.logfile) + if _, err := os.Stat(filepath.Dir(jl.logfile)); os.IsNotExist(err) { + if err := os.MkdirAll(filepath.Dir(jl.logfile), 0755); err != nil { + return nil, fmt.Errorf("%v: %v", errorDirectoryCreate, filepath.Dir(jl.logfile)) + } + } + + f, err := os.Create(jl.logfile) + if err != nil { + return nil, fmt.Errorf("%v: %v", errorLogfileCreate, jl.logfile) + } + f.WriteString("{}") + f.Close() } f, err := os.Open(jl.logfile) diff --git a/pkg/storage/logfile/xml.go b/pkg/storage/logfile/xml.go index cdc8e17..f7e2a8f 100644 --- a/pkg/storage/logfile/xml.go +++ b/pkg/storage/logfile/xml.go @@ -4,6 +4,7 @@ import ( "encoding/xml" "fmt" "os" + "path/filepath" "github.com/go-flucky/flucky/pkg/types" ) @@ -17,8 +18,19 @@ func (xl *xmlLogfile) GetLogfile() string { } func (xl *xmlLogfile) Read() ([]*types.MeasuredValue, error) { + if _, err := os.Stat(xl.logfile); os.IsNotExist(err) { - return nil, fmt.Errorf("%v: %v", errorLogfileNotFound, xl.logfile) + if _, err := os.Stat(filepath.Dir(xl.logfile)); os.IsNotExist(err) { + if err := os.MkdirAll(filepath.Dir(xl.logfile), 0755); err != nil { + return nil, fmt.Errorf("%v: %v", errorDirectoryCreate, filepath.Dir(xl.logfile)) + } + } + + f, err := os.Create(xl.logfile) + if err != nil { + return nil, fmt.Errorf("%v: %v", errorLogfileCreate, xl.logfile) + } + f.Close() } f, err := os.Open(xl.logfile)