dati

A Go library/binary to parse & execute data against template langauges.
git clone git://src.gearsix.net/dati
Log | Files | Refs | Atom | README | LICENSE

commit 94bcd4995d7b1c3291902e1226ba7a835c5cf4d4
parent d24769d1c4d6a3919992c1363c944ef40d82c8a9
Author: gearsix <gearsix@tuta.io>
Date:   Thu, 16 Mar 2023 15:07:50 +0000

data.go: WriteDataFile now creates a file if it doesn't exist.

Diffstat:
Mdata.go | 14++++++++++++--
1 file changed, 12 insertions(+), 2 deletions(-)

diff --git a/data.go b/data.go @@ -146,8 +146,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) (f *os.File, err error) { - if f, err = os.Open(path); err != nil { +func WriteDataFile(format DataFormat, data interface{}, path string, force bool) (f *os.File, err error) { + if f, err = os.Open(path); os.IsNotExist(err) { + 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()