fix: stop truncating existing files in common.TouchFile

`common.TouchFile` opened files with `O_TRUNC`, so the call from `common.SshTask.Execute` against `~/.ssh/known_hosts` emptied the dokku user's previously trusted host keys before `goph.DefaultKnownHosts()` read them. Running `scheduler-k3s:cluster:add` without `--insecure-allow-unknown-hosts` then failed with `knownhosts: key is unknown` and left a zero-byte `known_hosts`. The function now matches `touch(1)` semantics: create the file if missing, otherwise leave its contents intact.
This commit is contained in:
Jose Diaz-Gonzalez
2026-05-31 16:37:15 -04:00
parent 213d89b1e4
commit cf84679133
2 changed files with 51 additions and 2 deletions

View File

@@ -212,10 +212,12 @@ func TouchDir(filename string) error {
return os.MkdirAll(filename, mode)
}
// TouchFile creates an empty file at the specified path
// TouchFile ensures a file exists at the specified path without truncating
// its contents. It matches the semantics of unix touch(1): create the file if
// it does not exist, otherwise leave the existing contents intact.
func TouchFile(filename string) error {
mode := os.FileMode(0600)
file, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, mode)
file, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE, mode)
if err != nil {
return fmt.Errorf("Error opening file %v for creation: %v", filename, err)
}