Переглянути джерело

Remove non-crossplatform syscalls from `/api/about`.

The only way to get precise OS information cross platform is to have
a separate file for each platform with a build tag that returns the
info, which seems excessive for this usecase.
Kailash Nadh 1 рік тому
батько
коміт
104c4fc993
3 змінених файлів з 13 додано та 22 видалено
  1. 9 12
      cmd/init.go
  2. 3 4
      cmd/settings.go
  3. 1 6
      cmd/utils.go

+ 9 - 12
cmd/init.go

@@ -701,24 +701,22 @@ func initBounceManager(app *App) *bounce.Manager {
 
 func initAbout(q *models.Queries, db *sqlx.DB) about {
 	var (
-		mem     runtime.MemStats
-		utsname syscall.Utsname
+		mem runtime.MemStats
 	)
 
 	// Memory / alloc stats.
 	runtime.ReadMemStats(&mem)
 
-	// OS info.
-	if err := syscall.Uname(&utsname); err != nil {
-		lo.Printf("WARNING: error getting system info: %v", err)
-	}
-
-	// DB dbv.
 	info := types.JSONText(`{}`)
 	if err := db.QueryRow(q.GetDBInfo).Scan(&info); err != nil {
 		lo.Printf("WARNING: error getting database version: %v", err)
 	}
 
+	hostname, err := os.Hostname()
+	if err != nil {
+		lo.Printf("WARNING: error getting hostname: %v", err)
+	}
+
 	return about{
 		Version:   versionString,
 		Build:     buildString,
@@ -729,10 +727,9 @@ func initAbout(q *models.Queries, db *sqlx.DB) about {
 			NumCPU: runtime.NumCPU(),
 		},
 		Host: aboutHost{
-			OS:        int8ToStr(utsname.Sysname[:]),
-			OSRelease: int8ToStr(utsname.Release[:]),
-			Machine:   int8ToStr(utsname.Machine[:]),
-			Hostname:  int8ToStr(utsname.Nodename[:]),
+			OS:       runtime.GOOS,
+			Machine:  runtime.GOARCH,
+			Hostname: hostname,
 		},
 	}
 

+ 3 - 4
cmd/settings.go

@@ -24,10 +24,9 @@ import (
 const pwdMask = "•"
 
 type aboutHost struct {
-	OS        string `json:"os"`
-	OSRelease string `json:"os_release"`
-	Machine   string `json:"arch"`
-	Hostname  string `json:"hostname"`
+	OS       string `json:"os"`
+	Machine  string `json:"arch"`
+	Hostname string `json:"hostname"`
 }
 type aboutSystem struct {
 	NumCPU  int    `json:"num_cpu"`

+ 1 - 6
cmd/utils.go

@@ -101,11 +101,6 @@ func strSliceContains(str string, sl []string) bool {
 	return false
 }
 
-func int8ToStr(bs []int8) string {
-	b := make([]byte, len(bs))
-	for i, v := range bs {
-		b[i] = byte(v)
-	}
-
+func trimNullBytes(b []byte) string {
 	return string(bytes.Trim(b, "\x00"))
 }