PKGBUILD/pkg/storage/logfile/logfile.go

58 lines
1.2 KiB
Go

package logfile
import (
"path/filepath"
"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
}
// 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,
}
case ".xml":
return &xmlLogfile{
logfile: logfile,
}
default:
return &jsonLogfile{
logfile: logfile,
}
}
}
func writeCreationDate(measuredValues []*types.MeasuredValue) error {
now := format.FormatedTime()
for _, measuredValue := range measuredValues {
if measuredValue.CreationDate == nil {
measuredValue.CreationDate = &now
}
}
return nil
}