resources: Properly truncate files

Previously when rewriting specific template files they were not
overwritten properly. This commit updates the behaviour to use the right
os.Open flags.
This commit is contained in:
Tom Wiesing 2022-09-05 17:17:46 +02:00
parent 3c64689068
commit ef338cca05
No known key found for this signature in database
2 changed files with 2 additions and 6 deletions

View file

@ -54,10 +54,8 @@ func installFile(dst string, fsys embed.FS, src string, onInstallFile func(dst,
// determine if we need to create the destination file, or if it already exists
dstStat, dstErr := os.Stat(dst)
flag := os.O_WRONLY
switch {
case os.IsNotExist(dstErr):
flag |= os.O_CREATE
case dstErr != nil:
return errors.Wrapf(dstErr, "Error calling stat on destination %s", dst)
case dstStat.IsDir():
@ -65,7 +63,7 @@ func installFile(dst string, fsys embed.FS, src string, onInstallFile func(dst,
}
// Open the file
dstFile, err := os.OpenFile(dst, flag, srcStat.Mode())
dstFile, err := os.OpenFile(dst, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, srcStat.Mode())
if err != nil {
return errors.Wrapf(err, "Error opening destination %s", dst)
}

View file

@ -27,10 +27,8 @@ func InstallTemplate(dst, src string, context map[string]string) error {
// determine if we need to create the destination file, or if it already exists
dstStat, dstErr := os.Stat(dst)
flag := os.O_WRONLY
switch {
case os.IsNotExist(dstErr):
flag |= os.O_CREATE
case dstErr != nil:
return errors.Wrapf(dstErr, "Error calling stat on destination %s", dst)
case dstStat.IsDir():
@ -38,7 +36,7 @@ func InstallTemplate(dst, src string, context map[string]string) error {
}
// open and write the destination file
dstFile, err := os.OpenFile(dst, flag, srcMode)
dstFile, err := os.OpenFile(dst, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, srcMode)
if err != nil {
return errors.Wrapf(err, "Unable to open file %s", dst)
}