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:
parent
3c64689068
commit
ef338cca05
2 changed files with 2 additions and 6 deletions
|
|
@ -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
|
// determine if we need to create the destination file, or if it already exists
|
||||||
dstStat, dstErr := os.Stat(dst)
|
dstStat, dstErr := os.Stat(dst)
|
||||||
flag := os.O_WRONLY
|
|
||||||
switch {
|
switch {
|
||||||
case os.IsNotExist(dstErr):
|
case os.IsNotExist(dstErr):
|
||||||
flag |= os.O_CREATE
|
|
||||||
case dstErr != nil:
|
case dstErr != nil:
|
||||||
return errors.Wrapf(dstErr, "Error calling stat on destination %s", dst)
|
return errors.Wrapf(dstErr, "Error calling stat on destination %s", dst)
|
||||||
case dstStat.IsDir():
|
case dstStat.IsDir():
|
||||||
|
|
@ -65,7 +63,7 @@ func installFile(dst string, fsys embed.FS, src string, onInstallFile func(dst,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Open the file
|
// 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 {
|
if err != nil {
|
||||||
return errors.Wrapf(err, "Error opening destination %s", dst)
|
return errors.Wrapf(err, "Error opening destination %s", dst)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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
|
// determine if we need to create the destination file, or if it already exists
|
||||||
dstStat, dstErr := os.Stat(dst)
|
dstStat, dstErr := os.Stat(dst)
|
||||||
flag := os.O_WRONLY
|
|
||||||
switch {
|
switch {
|
||||||
case os.IsNotExist(dstErr):
|
case os.IsNotExist(dstErr):
|
||||||
flag |= os.O_CREATE
|
|
||||||
case dstErr != nil:
|
case dstErr != nil:
|
||||||
return errors.Wrapf(dstErr, "Error calling stat on destination %s", dst)
|
return errors.Wrapf(dstErr, "Error calling stat on destination %s", dst)
|
||||||
case dstStat.IsDir():
|
case dstStat.IsDir():
|
||||||
|
|
@ -38,7 +36,7 @@ func InstallTemplate(dst, src string, context map[string]string) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// open and write the destination file
|
// 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 {
|
if err != nil {
|
||||||
return errors.Wrapf(err, "Unable to open file %s", dst)
|
return errors.Wrapf(err, "Unable to open file %s", dst)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue