mirror of
https://github.com/kairos-io/kairos-agent.git
synced 2025-09-22 02:36:58 +00:00
refactor: move from io/ioutil to io and os packages The io/ioutil package has been deprecated as of Go 1.16 [1]. This commit replaces the existing io/ioutil functions with their new definitions in io and os packages. [1]: https://golang.org/doc/go1.16#ioutil Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
45 lines
857 B
Go
45 lines
857 B
Go
package utils
|
|
|
|
import (
|
|
"bytes"
|
|
"os"
|
|
"os/exec"
|
|
|
|
"github.com/joho/godotenv"
|
|
)
|
|
|
|
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 WriteEnv(envFile string, config map[string]string) error {
|
|
content, _ := os.ReadFile(envFile)
|
|
env, _ := godotenv.Unmarshal(string(content))
|
|
|
|
for key, val := range config {
|
|
env[key] = val
|
|
}
|
|
|
|
return godotenv.Write(env, envFile)
|
|
}
|
|
|
|
func Shell() *exec.Cmd {
|
|
cmd := exec.Command("/bin/sh")
|
|
cmd.Stdout = os.Stdout
|
|
cmd.Stderr = os.Stderr
|
|
cmd.Stdin = os.Stdin
|
|
return cmd
|
|
}
|
|
|
|
func ShellSTDIN(s, c string) (string, error) {
|
|
cmd := exec.Command("/bin/sh", "-c", c)
|
|
cmd.Stdout = os.Stdout
|
|
cmd.Stderr = os.Stderr
|
|
cmd.Stdin = bytes.NewBuffer([]byte(s))
|
|
o, err := cmd.CombinedOutput()
|
|
return string(o), err
|
|
}
|