Skip to content

Commit

Permalink
sysinfo: globalize
Browse files Browse the repository at this point in the history
  • Loading branch information
apprehensions committed Oct 23, 2023
1 parent 6bd6ab2 commit c56dfa1
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 98 deletions.
14 changes: 7 additions & 7 deletions sysinfo/gpu.go → sysinfo/card.go
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
//go:build linux && amd64

package sysinfo

import (
"os"
"strings"
"path"
"path/filepath"
"strings"
)

// ANY error here will be ignored. All of the filepath querying
// and such is done within /sys/, which is stored in memory.

type Card struct {
type card struct {
Path string
Driver string
Index int
Embedded bool
}

const drmPath = "/sys/class/drm"

var embeddedDisplays = []string{"eDP", "LVDS", "DP-2"}

func Cards() (cards []Card) {
func getCards() (cs []card) {
drmCards, _ := filepath.Glob(path.Join(drmPath, "card[0-9]"))

for i, c := range drmCards {
d, _ := filepath.EvalSymlinks(path.Join(c, "device/driver"))
d = path.Base(d)

cards = append(cards, Card{
cs = append(cs, card{
Path: c,
Driver: d,
Index: i,
Expand Down
32 changes: 32 additions & 0 deletions sysinfo/cpu.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//go:build linux

package sysinfo

import (
"bufio"
"os"
"regexp"
)

func cpuModel() string {
column := regexp.MustCompile("\t+: ")

f, _ := os.Open("/proc/cpuinfo")
defer f.Close()

s := bufio.NewScanner(f)

for s.Scan() {
sl := column.Split(s.Text(), 2)
if sl == nil {
continue
}

// pfft, who needs multiple cpus? just return if we got all we need
if sl[0] == "model name" {
return sl[1]
}
}

return "unknown cpu"
}
30 changes: 16 additions & 14 deletions sysinfo/distro.go
Original file line number Diff line number Diff line change
@@ -1,50 +1,52 @@
//go:build linux

package sysinfo

import (
"os"
"bufio"
"os"
"strings"
)

type Distro struct {
Name string
type distro struct {
Name string
Version string
}

func GetDistro() (Distro, error) {
var d Distro
func getDistro() (d distro) {
d = distro{
Name: "unknown distro name",
Version: "unknown distro version",
}

f, err := os.Open("/etc/os-release")
if err != nil {
return Distro{}, err
return
}
defer f.Close()

s := bufio.NewScanner(f)

for s.Scan() {
m := strings.SplitN(s.Text(), "=", 2)
if len(m) != 2 {
continue
}

val := strings.Trim(m[1], "\"")

switch m[0] {
case "PRETTY_NAME":
d.Name = val
case "VERSION_ID":
d.Version = val
}
}
if err := s.Err(); err != nil {
return Distro{}, err
}

return d, nil

return
}

func (d Distro) String() string {
func (d distro) String() string {
if d.Name == "" {
d.Name = "Linux"
}
Expand Down
36 changes: 36 additions & 0 deletions sysinfo/kernel.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package sysinfo

import (
"strings"
"syscall"
)

type kernel struct {
Release string
Version string
}

func getKernel() kernel {
var un syscall.Utsname
_ = syscall.Uname(&un)

return kernel{
Release: unameString(un.Release),
Version: unameString(un.Version),
}
}

func (k kernel) String() string {
return k.Release + " " + k.Version
}

func unameString(unarr [65]int8) string {
var sb strings.Builder
for _, b := range unarr[:] {
if b == 0 {
break
}
sb.WriteByte(byte(b))
}
return sb.String()
}
90 changes: 13 additions & 77 deletions sysinfo/sysinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,86 +2,22 @@ package sysinfo

import (
"os"
"bufio"
"regexp"
"syscall"
"strings"
)

type Kernel struct {
Release string
Version string
}

type CPU struct {
Model string
Flags []string
}

func GetKernel() Kernel {
var un syscall.Utsname
_ = syscall.Uname(&un)

unameString := func(unarr [65]int8) string {
var sb strings.Builder
for _, b := range unarr[:] {
if b == 0 {
break
}
sb.WriteByte(byte(b))
}
return sb.String()
}

return Kernel{
Release: unameString(un.Release),
Version: unameString(un.Version),
}
}

func (k Kernel) String() string {
return k.Release + " " + k.Version
}

func GetCPU() (cpu CPU) {
column := regexp.MustCompile("\t+: ")

f, _ := os.Open("/proc/cpuinfo")
defer f.Close()

s := bufio.NewScanner(f)

for s.Scan() {
sl := column.Split(s.Text(), 2)
if sl == nil {
continue
}

// pfft, who needs multiple cpus? just return if we got all we need
if cpu.Model != "" && cpu.Flags != nil {
break
}

switch sl[0] {
case "model name":
cpu.Model = sl[1]
case "flags":
cpu.Flags = strings.Split(sl[1], " ")
}
}

if s.Err() != nil {
return
}

return
}
var (
Kernel kernel
CPU string
Cards []card
Distro distro
InFlatpak bool
)

func (cpu *CPU) String() string {
return cpu.Model
}
func init() {
Kernel = getKernel()
CPU = cpuModel()
Cards = getCards()
Distro = getDistro()

func InFlatpak() bool {
_, err := os.Stat("/.flatpak-info")
return err == nil
InFlatpak = err == nil
}

0 comments on commit c56dfa1

Please sign in to comment.