Use unix.Uname to get Darwin system info (#404)

This prevents the client from needing to use command line tools
This commit is contained in:
Maycon Santos 2022-07-30 11:31:27 +02:00 committed by GitHub
parent 966661fe91
commit d1c2b3d703
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -4,41 +4,25 @@ import (
"bytes" "bytes"
"context" "context"
"fmt" "fmt"
"golang.org/x/sys/unix"
"os" "os"
"os/exec"
"runtime" "runtime"
"strings"
"time"
) )
// GetInfo retrieves and parses the system information // GetInfo retrieves and parses the system information
func GetInfo(ctx context.Context) *Info { func GetInfo(ctx context.Context) *Info {
out := _getInfo() utsname := unix.Utsname{}
for strings.Contains(out, "broken pipe") { err := unix.Uname(&utsname)
out = _getInfo() if err != nil {
time.Sleep(500 * time.Millisecond) fmt.Println("getInfo:", err)
} }
osStr := strings.Replace(out, "\n", "", -1) sysName := string(bytes.Split(utsname.Sysname[:], []byte{0})[0])
osStr = strings.Replace(osStr, "\r\n", "", -1) machine := string(bytes.Split(utsname.Machine[:], []byte{0})[0])
osInfo := strings.Split(osStr, " ") release := string(bytes.Split(utsname.Release[:], []byte{0})[0])
gio := &Info{Kernel: osInfo[0], OSVersion: osInfo[1], Core: osInfo[1], Platform: osInfo[2], OS: osInfo[0], GoOS: runtime.GOOS, CPUs: runtime.NumCPU()} gio := &Info{Kernel: sysName, OSVersion: release, Core: release, Platform: machine, OS: sysName, GoOS: runtime.GOOS, CPUs: runtime.NumCPU()}
gio.Hostname, _ = os.Hostname() gio.Hostname, _ = os.Hostname()
gio.WiretrusteeVersion = NetbirdVersion() gio.WiretrusteeVersion = NetbirdVersion()
gio.UIVersion = extractUserAgent(ctx) gio.UIVersion = extractUserAgent(ctx)
return gio return gio
} }
func _getInfo() string {
cmd := exec.Command("uname", "-srm")
cmd.Stdin = strings.NewReader("some input")
var out bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
fmt.Println("getInfo:", err)
}
return out.String()
}