Skip to content

Commit

Permalink
Remove non-crossplatform syscalls from /api/about.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
knadh committed Aug 6, 2023
1 parent a1c507b commit 104c4fc
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 22 deletions.
21 changes: 9 additions & 12 deletions cmd/init.go
Expand Up @@ -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,
Expand All @@ -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,
},
}

Expand Down
7 changes: 3 additions & 4 deletions cmd/settings.go
Expand Up @@ -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"`
Expand Down
7 changes: 1 addition & 6 deletions cmd/utils.go
Expand Up @@ -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"))
}

0 comments on commit 104c4fc

Please sign in to comment.