2019-06-19 17:18:01 +00:00
|
|
|
package logfile
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2019-06-26 21:16:26 +00:00
|
|
|
"path/filepath"
|
2019-06-19 17:18:01 +00:00
|
|
|
|
|
|
|
"github.com/go-flucky/flucky/pkg/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
type jsonLogfile struct {
|
2019-06-26 21:16:26 +00:00
|
|
|
logfile string
|
2019-06-25 20:22:34 +00:00
|
|
|
}
|
|
|
|
|
2019-07-02 20:33:01 +00:00
|
|
|
func (jl *jsonLogfile) Read() ([]*types.MeasuredValue, error) {
|
2019-06-25 20:22:34 +00:00
|
|
|
|
2019-06-26 21:16:26 +00:00
|
|
|
if _, err := os.Stat(jl.logfile); os.IsNotExist(err) {
|
2019-09-29 16:29:06 +00:00
|
|
|
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()
|
2019-06-26 21:16:26 +00:00
|
|
|
}
|
2019-06-25 20:22:34 +00:00
|
|
|
|
2019-07-02 20:33:01 +00:00
|
|
|
f, err := os.Open(jl.logfile)
|
2019-06-25 20:22:34 +00:00
|
|
|
if err != nil {
|
2019-07-02 20:33:01 +00:00
|
|
|
return nil, fmt.Errorf("%v %v: %v", errorLogfileOpen, jl.logfile, err)
|
2019-06-25 20:22:34 +00:00
|
|
|
}
|
|
|
|
|
2019-07-02 20:33:01 +00:00
|
|
|
measuredValues := make([]*types.MeasuredValue, 0)
|
2019-06-26 21:16:26 +00:00
|
|
|
|
2019-07-14 17:06:26 +00:00
|
|
|
if err := json.NewDecoder(f).Decode(&measuredValues); err != nil {
|
2019-07-02 20:33:01 +00:00
|
|
|
return nil, fmt.Errorf("%v %v: %v", errorLogfileDecode, jl.logfile, err)
|
2019-06-25 20:22:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return measuredValues, nil
|
2019-06-19 17:18:01 +00:00
|
|
|
}
|
|
|
|
|
2019-07-02 20:33:01 +00:00
|
|
|
func (jl *jsonLogfile) Write(measuredValues []*types.MeasuredValue) error {
|
2019-06-19 17:18:01 +00:00
|
|
|
|
2019-06-26 21:16:26 +00:00
|
|
|
if _, err := os.Stat(filepath.Dir(jl.logfile)); os.IsNotExist(err) {
|
|
|
|
if err := os.MkdirAll(filepath.Dir(jl.logfile), 755); err != nil {
|
|
|
|
return fmt.Errorf("Directory for the logfile can not be created: %v", err)
|
|
|
|
}
|
2019-06-19 17:18:01 +00:00
|
|
|
}
|
|
|
|
|
2019-06-26 21:16:26 +00:00
|
|
|
f, err := os.Create(jl.logfile)
|
2019-06-19 17:18:01 +00:00
|
|
|
if err != nil {
|
2019-06-26 21:16:26 +00:00
|
|
|
return fmt.Errorf("%v %v: %v", errorLogfileCreate, jl.logfile, err)
|
2019-06-19 17:18:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
jsonEncoder := json.NewEncoder(f)
|
|
|
|
jsonEncoder.SetIndent("", " ")
|
2019-06-26 21:16:26 +00:00
|
|
|
err = jsonEncoder.Encode(measuredValues)
|
2019-06-19 17:18:01 +00:00
|
|
|
if err != nil {
|
2019-06-26 21:16:26 +00:00
|
|
|
return fmt.Errorf("%v %v: %v", errorLogfileEncode, jl.logfile, err)
|
2019-06-19 17:18:01 +00:00
|
|
|
}
|
2019-06-26 21:16:26 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
|
2019-06-19 17:18:01 +00:00
|
|
|
}
|