Truncate and trim files when saving
This commit is contained in:
parent
cbdb94a260
commit
b88260ff14
1 changed files with 3 additions and 2 deletions
5
files.go
5
files.go
|
@ -1,6 +1,7 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"errors"
|
"errors"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"os"
|
"os"
|
||||||
|
@ -32,14 +33,14 @@ func ReadFile(filename string) ([]byte, error) {
|
||||||
// SaveFile Writes request's contents to a file
|
// SaveFile Writes request's contents to a file
|
||||||
func SaveFile(filename string, contents []byte) error {
|
func SaveFile(filename string, contents []byte) error {
|
||||||
filename = "data/" + filename + ".txt"
|
filename = "data/" + filename + ".txt"
|
||||||
f, err := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY, 0644)
|
f, err := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Error("error opening/making file",
|
slog.Error("error opening/making file",
|
||||||
"error", err,
|
"error", err,
|
||||||
"file", filename)
|
"file", filename)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if _, err := f.Write(contents); err != nil {
|
if _, err := f.Write(bytes.TrimSpace(contents)); err != nil {
|
||||||
slog.Error("error writing to file",
|
slog.Error("error writing to file",
|
||||||
"error", err,
|
"error", err,
|
||||||
"file", filename)
|
"file", filename)
|
||||||
|
|
Loading…
Reference in a new issue