posadka/internal/primitive.go
Simon de Vlieger 0877745cda
sketch: commandline interface
Signed-off-by: Simon de Vlieger <supakeen@redhat.com>
2024-12-28 10:30:02 +01:00

47 lines
667 B
Go

package internal
import (
"bytes"
"os"
"os/user"
)
// Am I root?
func AmIRoot() (bool, error) {
u, err := user.Current()
if err != nil {
return false, err
}
return u.Username == "root", nil
}
// 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)
}