38 lines
783 B
Go
38 lines
783 B
Go
package sysctl
|
|
|
|
// inspired from https://github.com/lorenzosaino/go-sysctl/blob/main/sysctl.go
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
)
|
|
|
|
const defaultPath = "/proc/sys/"
|
|
|
|
// Get returns a sysctl from a given key.
|
|
func Get(key string) (string, error) {
|
|
return readFile(pathFromKey(key))
|
|
}
|
|
|
|
// Set updates the value of a sysctl.
|
|
func Set(key, value string) error {
|
|
return writeFile(pathFromKey(key), value)
|
|
}
|
|
|
|
func pathFromKey(key string) string {
|
|
return filepath.Join(defaultPath, strings.Replace(key, ".", "/", -1))
|
|
}
|
|
|
|
func readFile(path string) (string, error) {
|
|
data, err := os.ReadFile(path)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return strings.TrimSpace(string(data)), nil
|
|
}
|
|
|
|
func writeFile(path, value string) error {
|
|
return os.WriteFile(path, []byte(value), 0o644)
|
|
}
|