Simon de Vlieger
3ad2d0cd4c
Add a new `WriteToFile` primitive plus a usecase for it. Signed-off-by: Simon de Vlieger <supakeen@redhat.com>
35 lines
505 B
Go
35 lines
505 B
Go
package posadka
|
|
|
|
import (
|
|
"bytes"
|
|
"os"
|
|
)
|
|
|
|
// Replace `a` with `b` in a given file at `path`. This function is non-atomic
|
|
// so be careful with crashing.
|
|
func ReplaceInFile(path string, a, b []byte) error {
|
|
data, err := os.ReadFile(path)
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
data = bytes.Replace(
|
|
data,
|
|
a,
|
|
b,
|
|
1,
|
|
)
|
|
|
|
err = os.WriteFile(path, data, 0644)
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func WriteToFile(path string, data []byte) error {
|
|
return os.WriteFile(path, data, 0644)
|
|
}
|