add sys.RunCmdBuffered & util.InspectStruct

This commit is contained in:
Mahdi Abu Yasmine 2023-11-06 23:43:10 +01:00
parent a3679be86b
commit 2d804b3e59
2 changed files with 31 additions and 0 deletions

View File

@ -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

View File

@ -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
}