add sys.RunCmdBuffered & util.InspectStruct
This commit is contained in:
parent
a3679be86b
commit
2d804b3e59
10
sys/sys.go
10
sys/sys.go
|
@ -1,6 +1,7 @@
|
|||
package sys
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
|
@ -13,6 +14,15 @@ import (
|
|||
|
||||
var TERM_WIDTH = 0
|
||||
|
||||
func RunCmdBuffered(cmd *exec.Cmd, out *bytes.Buffer) int {
|
||||
rs1, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
fmt.Println("Error: ", err)
|
||||
}
|
||||
out.Write(rs1)
|
||||
return ManageStatusCmd(cmd, err)
|
||||
}
|
||||
|
||||
func RunCmd(cmd *exec.Cmd) int {
|
||||
cmd.Stdin = os.Stdin
|
||||
cmd.Stdout = os.Stdout
|
||||
|
|
21
util/util.go
21
util/util.go
|
@ -7,6 +7,7 @@ import (
|
|||
"log"
|
||||
"math"
|
||||
"path"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
|
@ -106,3 +107,23 @@ func EscapeDquote(str string) string {
|
|||
func EscapeSquote(str string) string {
|
||||
return Escape(str, '\'')
|
||||
}
|
||||
|
||||
func InspectStruct(a any) (map[string]string, bool) {
|
||||
done := false
|
||||
rs := make(map[string]string)
|
||||
n := reflect.ValueOf(a)
|
||||
for {
|
||||
if n.Kind() == reflect.Ptr {
|
||||
n = n.Elem()
|
||||
} else if n.Kind() == reflect.Struct {
|
||||
break
|
||||
} else {
|
||||
return rs, done
|
||||
}
|
||||
}
|
||||
for i := 0; i < n.NumField(); i++ {
|
||||
rs[n.Type().Field(i).Name] = n.Field(i).Type().String()
|
||||
}
|
||||
done = true
|
||||
return rs, done
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user