2019-01-01 08:23:01 +00:00
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
func WriteFile(name string, content string) error {
|
2019-01-09 16:54:15 +00:00
|
|
|
os.MkdirAll(filepath.Dir(name), 0755)
|
2022-10-08 00:36:57 +00:00
|
|
|
err := os.WriteFile(name, []byte(content), 0644)
|
2019-01-01 08:23:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "writing %s", name)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2022-06-08 08:38:07 +00:00
|
|
|
|
2023-08-01 15:55:34 +00:00
|
|
|
func CopyFile(sourceFile string, destinationFile string, ignoreNotExist bool) error {
|
2022-06-08 08:38:07 +00:00
|
|
|
os.MkdirAll(filepath.Dir(destinationFile), 0755)
|
2022-10-08 00:36:57 +00:00
|
|
|
input, err := os.ReadFile(sourceFile)
|
2023-08-01 15:55:34 +00:00
|
|
|
if errors.Is(err, os.ErrNotExist) && ignoreNotExist {
|
|
|
|
return nil
|
|
|
|
} else if err != nil {
|
2022-06-08 08:38:07 +00:00
|
|
|
return errors.Wrapf(err, "copying %s to %s", sourceFile, destinationFile)
|
|
|
|
}
|
2022-10-08 00:36:57 +00:00
|
|
|
err = os.WriteFile(destinationFile, input, 0644)
|
2022-06-08 08:38:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "copying %s to %s", sourceFile, destinationFile)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|