PKGBUILD/pkg/storage/logfile/logfile.go

58 lines
1.2 KiB
Go
Raw Normal View History

package logfile
import (
"path/filepath"
2019-08-20 19:37:45 +00:00
"github.com/go-flucky/flucky/pkg/internal/format"
"github.com/go-flucky/flucky/pkg/types"
)
// Append adds an array of several measured values to a logfile
func Append(logfile Logfile, measuredValues []*types.MeasuredValue) error {
allMeasuredValues, err := logfile.Read()
if err != nil {
return err
}
allMeasuredValues = append(allMeasuredValues, measuredValues...)
err = logfile.Write(allMeasuredValues)
if err != nil {
return err
}
return nil
}
2019-06-21 09:48:44 +00:00
// New returns a log file with basic functions for reading and writing data. The
// file extension of the logfile is taken into account to format the logfile
// into the correct format.
func New(logfile string) Logfile {
ext := filepath.Ext(logfile)
switch ext {
case ".csv":
return &csvLogfile{
logfile: logfile,
}
case ".json":
return &jsonLogfile{
logfile: logfile,
}
2019-07-03 16:46:19 +00:00
case ".xml":
return &xmlLogfile{
logfile: logfile,
}
default:
return &jsonLogfile{
logfile: logfile,
}
}
}
2019-08-20 19:37:45 +00:00
func writeCreationDate(measuredValues []*types.MeasuredValue) error {
now := format.FormatedTime()
for _, measuredValue := range measuredValues {
if measuredValue.CreationDate == nil {
measuredValue.CreationDate = &now
}
}
2019-08-20 19:37:45 +00:00
return nil
}