commit 73c48d398248fe82c617e7f9d4dfd326ef4888de
parent 702eecdf6b41a6e9b2109dea62d3582f28b1c093
Author: gearsix <gearsix@tuta.io>
Date: Thu, 16 Mar 2023 20:26:05 +0000
data.go, test.go: minor fixes
Diffstat:
2 files changed, 4 insertions(+), 7 deletions(-)
diff --git a/data.go b/data.go
@@ -147,20 +147,18 @@ func WriteData(format DataFormat, data interface{}, w io.Writer) error {
// WriteDataFile attempts to write `data` as `format` to the file at `path`.
// If `force` is *true*, then any existing files will be overwritten.
func WriteDataFile(format DataFormat, data interface{}, path string, force bool) (f *os.File, err error) {
- if f, err = os.Open(path); os.IsNotExist(err) {
+ if f, err = os.Open(path); force || os.IsNotExist(err) {
+ if force {
+ f.Close()
+ }
f, err = os.Create(path)
} else if !force {
err = os.ErrExist
- } else { // overwrite existing file data
- if err = f.Truncate(0); err == nil {
- _, err = f.Seek(0, 0)
- }
}
if err != nil {
return
}
- defer f.Close()
if err = WriteData(format, data, f); err != nil {
f = nil
diff --git a/template.go b/template.go
@@ -151,7 +151,6 @@ func (t *Template)ExecuteToFile(data interface{}, path string, force bool) (f *o
if err != nil {
return
}
- defer f.Close()
var out bytes.Buffer
if out, err = t.Execute(data); err != nil {