mirror of
https://github.com/kairos-io/kairos-sdk.git
synced 2025-04-27 19:15:23 +00:00
This had some side effects: - Have to add some utils from the kairos/machine modules, which IMHO should not be there, they should be here if the are generic enough - Dropping the sdk dir, just have the modules in the root dir Signed-off-by: Itxaka <itxaka.garcia@spectrocloud.com>
65 lines
1.2 KiB
Go
65 lines
1.2 KiB
Go
package utils
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/denisbrodbeck/machineid"
|
|
"github.com/joho/godotenv"
|
|
"os"
|
|
"os/exec"
|
|
)
|
|
|
|
func SH(c string) (string, error) {
|
|
cmd := exec.Command("/bin/sh", "-c", c)
|
|
cmd.Env = os.Environ()
|
|
o, err := cmd.CombinedOutput()
|
|
return string(o), err
|
|
}
|
|
|
|
func SHInDir(c, dir string) (string, error) {
|
|
cmd := exec.Command("/bin/sh", "-c", c)
|
|
cmd.Env = os.Environ()
|
|
cmd.Dir = dir
|
|
o, err := cmd.CombinedOutput()
|
|
return string(o), err
|
|
}
|
|
|
|
func Exists(path string) bool {
|
|
_, err := os.Stat(path)
|
|
|
|
return !os.IsNotExist(err)
|
|
}
|
|
|
|
// UUID TODO: move this into a machine submodule
|
|
func UUID() string {
|
|
if os.Getenv("UUID") != "" {
|
|
return os.Getenv("UUID")
|
|
}
|
|
id, _ := machineid.ID()
|
|
hostname, _ := os.Hostname()
|
|
return fmt.Sprintf("%s-%s", id, hostname)
|
|
}
|
|
|
|
func OSRelease(key string) (string, error) {
|
|
release, err := godotenv.Read("/etc/os-release")
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
v, exists := release[key]
|
|
if !exists {
|
|
return "", fmt.Errorf("key not found")
|
|
}
|
|
return v, nil
|
|
}
|
|
|
|
func FindCommand(def string, options []string) string {
|
|
for _, p := range options {
|
|
path, err := exec.LookPath(p)
|
|
if err == nil {
|
|
return path
|
|
}
|
|
}
|
|
|
|
// Otherwise return default
|
|
return def
|
|
}
|