k3s/pkg/agent/util/file.go
Derek Nola 46cbbab263
Consolidate CopyFile functions (#8079)
* Consolidate CopyFile function

Signed-off-by: Derek Nola <derek.nola@suse.com>

* Copy to File, not destination folder

Signed-off-by: Derek Nola <derek.nola@suse.com>

---------

Signed-off-by: Derek Nola <derek.nola@suse.com>
2023-08-01 08:55:34 -07:00

33 lines
804 B
Go

package util
import (
"os"
"path/filepath"
"github.com/pkg/errors"
)
func WriteFile(name string, content string) error {
os.MkdirAll(filepath.Dir(name), 0755)
err := os.WriteFile(name, []byte(content), 0644)
if err != nil {
return errors.Wrapf(err, "writing %s", name)
}
return nil
}
func CopyFile(sourceFile string, destinationFile string, ignoreNotExist bool) error {
os.MkdirAll(filepath.Dir(destinationFile), 0755)
input, err := os.ReadFile(sourceFile)
if errors.Is(err, os.ErrNotExist) && ignoreNotExist {
return nil
} else if err != nil {
return errors.Wrapf(err, "copying %s to %s", sourceFile, destinationFile)
}
err = os.WriteFile(destinationFile, input, 0644)
if err != nil {
return errors.Wrapf(err, "copying %s to %s", sourceFile, destinationFile)
}
return nil
}