command
This commit is contained in:
47
utils/utils.go
Normal file
47
utils/utils.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
)
|
||||
|
||||
//go:embed assets/*
|
||||
var FS embed.FS
|
||||
|
||||
func Exec(args ...string) error {
|
||||
fArgs := []string{"-c"}
|
||||
if len(args) > 0 {
|
||||
fArgs = append(fArgs, args...)
|
||||
}
|
||||
cmd := exec.Command("bash", fArgs...)
|
||||
cmd.Stdin = os.Stdin
|
||||
cmd.Stderr = os.Stderr
|
||||
|
||||
return cmd.Run()
|
||||
}
|
||||
|
||||
func ReadFS(filePath string) (string, error) {
|
||||
b, err := FS.ReadFile(filePath)
|
||||
return string(b), err
|
||||
}
|
||||
|
||||
func Extract(content string, key string) (string, bool) {
|
||||
key = key + ": "
|
||||
idx := strings.Index(content, key)
|
||||
if idx == -1 {
|
||||
return "", false
|
||||
}
|
||||
|
||||
// start right after "host: "
|
||||
start := idx + len(key)
|
||||
|
||||
// stop at end of line
|
||||
end := strings.Index(content[start:], "\n")
|
||||
if end == -1 {
|
||||
return strings.TrimSpace(content[start:]), true
|
||||
}
|
||||
|
||||
return strings.TrimSpace(content[start : start+end]), true
|
||||
}
|
||||
Reference in New Issue
Block a user