42 lines
739 B
Go
42 lines
739 B
Go
package files
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
fp "path/filepath"
|
|
"strings"
|
|
|
|
"codeberg.org/danjones000/my-log/config"
|
|
"codeberg.org/danjones000/my-log/models"
|
|
)
|
|
|
|
func Append(l models.Log) error {
|
|
conf, err := config.Load()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
filename := fmt.Sprintf("%s.%s", strings.ReplaceAll(l.Name, ".", string(os.PathSeparator)), conf.Input.Ext)
|
|
path := fp.Join(conf.Input.Path, filename)
|
|
dir := fp.Dir(path)
|
|
err = os.MkdirAll(dir, 0750)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
f, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0640)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer f.Close()
|
|
|
|
for _, e := range l.Entries {
|
|
by, err := e.MarshalText()
|
|
if err != nil {
|
|
continue
|
|
}
|
|
f.Write(by)
|
|
}
|
|
|
|
return nil
|
|
}
|