mirror of
https://github.com/speatzle/nfsense.git
synced 2025-09-13 07:19:07 +00:00
Move OverwriteFile to util
This commit is contained in:
parent
53598e5d78
commit
6f396b3833
2 changed files with 37 additions and 31 deletions
34
internal/util/file.go
Normal file
34
internal/util/file.go
Normal file
|
@ -0,0 +1,34 @@
|
|||
package util
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
func OverwriteFile(path, content string) error {
|
||||
f, err := os.OpenFile(path, os.O_RDWR, 0644)
|
||||
if err != nil {
|
||||
return fmt.Errorf("opening File: %w", err)
|
||||
}
|
||||
|
||||
err = f.Truncate(0)
|
||||
if err != nil {
|
||||
return fmt.Errorf("truncate File: %w", err)
|
||||
}
|
||||
|
||||
_, err = f.Seek(0, 0)
|
||||
if err != nil {
|
||||
return fmt.Errorf("seek File: %w", err)
|
||||
}
|
||||
|
||||
_, err = f.WriteString(content + "\n")
|
||||
if err != nil {
|
||||
return fmt.Errorf("writing File: %w", err)
|
||||
}
|
||||
|
||||
err = f.Sync()
|
||||
if err != nil {
|
||||
return fmt.Errorf("syncing File: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue