create neccessary parent folder if path does not exists

This commit is contained in:
raziman 2020-09-25 17:42:26 +08:00
parent 1689a6208b
commit d672b15108

View File

@ -213,10 +213,18 @@ func contains(needle int, haystack []int) bool {
return false
}
// appendFile appends to a file, create the file if not exists
func appendFile(path string, content string) error {
f, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return tracerr.Wrap(err)
if err != os.ErrNotExist {
return tracerr.Wrap(err)
}
// create the neccessary parent directory
err = os.MkdirAll(filepath.Dir(expandFilePath(path)), os.ModePerm)
if err != nil {
return err
}
}
defer f.Close()
if _, err := f.WriteString(content); err != nil {