piframe-go/utils/files.go

21 lines
513 B
Go
Raw Permalink Normal View History

package utils
import (
"io/ioutil"
"log"
"os"
)
func WriteFile(destinationPath string, tempPath string, dataForDisk []byte) {
existingStat, err := os.Lstat(destinationPath)
if err != nil {
log.Fatalf("Error checking if file exists : %s", err)
}
if err := ioutil.WriteFile(tempPath, dataForDisk, existingStat.Mode()); err != nil {
log.Fatalf("Error writing temp file : %s", err)
}
if err := os.Rename(tempPath, destinationPath); err != nil {
log.Fatalf("Error moving new in place : %s", err)
}
}