2024-12-28 09:09:25 +00:00
|
|
|
package internal
|
2024-10-28 10:08:27 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"os"
|
2024-12-28 09:09:25 +00:00
|
|
|
"os/user"
|
2024-10-28 10:08:27 +00:00
|
|
|
)
|
|
|
|
|
2024-12-28 09:09:25 +00:00
|
|
|
// Am I root?
|
|
|
|
func AmIRoot() (bool, error) {
|
|
|
|
u, err := user.Current()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return u.Username == "root", nil
|
|
|
|
}
|
|
|
|
|
2024-10-28 10:08:27 +00:00
|
|
|
// 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
|
|
|
|
}
|
2024-10-28 10:49:07 +00:00
|
|
|
|
|
|
|
func WriteToFile(path string, data []byte) error {
|
|
|
|
return os.WriteFile(path, data, 0644)
|
|
|
|
}
|