✨ Only add newline to file when needed
This commit is contained in:
parent
2fc60c16c6
commit
5b8e4696ea
4 changed files with 82 additions and 5 deletions
|
|
@ -2,6 +2,7 @@ package files
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
fp "path/filepath"
|
||||
"strings"
|
||||
|
|
@ -31,18 +32,32 @@ func Append(l models.Log) error {
|
|||
return err
|
||||
}
|
||||
|
||||
f, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0640)
|
||||
f, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_RDWR, 0640)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
f.Seek(-1, os.SEEK_END)
|
||||
last := make([]byte, 1, 1)
|
||||
n, err := f.Read(last)
|
||||
if err != nil && err != io.EOF {
|
||||
return err
|
||||
}
|
||||
|
||||
if err == nil && n > 0 {
|
||||
if last[0] != 10 {
|
||||
f.Write([]byte{10})
|
||||
}
|
||||
}
|
||||
|
||||
for _, e := range l.Entries {
|
||||
by, err := e.MarshalText()
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
f.Write(by)
|
||||
f.Write([]byte{10})
|
||||
}
|
||||
|
||||
return nil
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue