Skip to content

Commit

Permalink
initial sysinfo support
Browse files Browse the repository at this point in the history
  • Loading branch information
apprehensions committed Oct 18, 2023
1 parent d23102f commit 4af4fce
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 16 deletions.
40 changes: 35 additions & 5 deletions cmd/vinegar/vinegar.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ import (
"os"
"os/signal"
"path/filepath"
"slices"
"syscall"

"github.com/vinegarhq/vinegar/internal/config"
"github.com/vinegarhq/vinegar/internal/config/editor"
"github.com/vinegarhq/vinegar/internal/config/state"
"github.com/vinegarhq/vinegar/internal/dirs"
"github.com/vinegarhq/vinegar/internal/logs"
"github.com/vinegarhq/vinegar/sysinfo"
"github.com/vinegarhq/vinegar/roblox"
"github.com/vinegarhq/vinegar/wine"
)
Expand All @@ -24,7 +26,7 @@ var BinPrefix string
func usage() {
fmt.Fprintln(os.Stderr, "usage: vinegar [-config filepath] player|studio [args...]")
fmt.Fprintln(os.Stderr, "usage: vinegar [-config filepath] exec prog [args...]")
fmt.Fprintln(os.Stderr, " vinegar [-config filepath] edit|kill|uninstall|delete|install-webview2|winetricks|reportinfo")
fmt.Fprintln(os.Stderr, " vinegar [-config filepath] edit|kill|uninstall|delete|install-webview2|winetricks|sysinfo")
os.Exit(1)
}

Expand All @@ -49,7 +51,7 @@ func main() {
}
// These commands (except player & studio) don't require a configuration,
// but they require a wineprefix, hence wineroot of configuration is required.
case "player", "studio", "exec", "kill", "install-webview2", "winetricks":
case "sysinfo", "player", "studio", "exec", "kill", "install-webview2", "winetricks":
pfxKilled := false
cfg, err := config.Load(*configPath)
if err != nil {
Expand Down Expand Up @@ -77,14 +79,17 @@ func main() {
}()

switch cmd {
case "reportinfo":
// TODO: implement this function
case "sysinfo":
Sysinfo(&pfx)
case "exec":
if len(args) < 2 {
usage()
}

if err := pfx.Wine(args[1], args[2:]...).Run(); err != nil {
cmd := pfx.Wine(args[1], args[2:]...)
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
if err := cmd.Run(); err != nil {
log.Fatal(err)
}
case "kill":
Expand Down Expand Up @@ -198,3 +203,28 @@ func Delete() {
log.Fatal(err)
}
}

func Sysinfo(pfx *wine.Prefix) {
cpu := sysinfo.GetCPU()
avx := slices.Contains(cpu.Flags, "avx")
k := sysinfo.GetKernel()
d, err := sysinfo.GetDistro()
if err != nil {
log.Fatal(err)
}

ver, err := pfx.Wine("--version").Output()
if err != nil {
log.Fatal(err)
}

info := `## System information
* Distro: %s
* Processor: %s
* Supports AVX: %t
* Kernel: %s
* Wine: %s
`

fmt.Printf(info, d, cpu.Model, avx, k, ver)
}
57 changes: 57 additions & 0 deletions sysinfo/distro.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package sysinfo

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

type Distro struct {
Name string
Version string
}

func GetDistro() (Distro, error) {
var d Distro

f, err := os.Open("/etc/os-release")
if err != nil {
return Distro{}, err
}
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
}

func (d Distro) String() string {
if d.Name == "" {
d.Name = "Linux"
}

if d.Version == "" {
d.Version = "Linux"
}

return d.Name + " " + d.Version
}
18 changes: 11 additions & 7 deletions sysinfo/sysinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"syscall"
"strings"
"path/filepath"
"slices"
)

type Kernel struct {
Expand All @@ -20,7 +19,6 @@ type Kernel struct {
type CPU struct {
Model string
Flags []string
AVX bool
}

type GPU struct {
Expand All @@ -32,7 +30,7 @@ type GPU struct {

type GPUs []GPU

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

Expand All @@ -53,7 +51,11 @@ func NewKernel() Kernel {
}
}

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

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

f, _ := os.Open("/proc/cpuinfo")
Expand All @@ -80,16 +82,18 @@ func NewCPU() (cpu CPU) {
}
}

cpu.AVX = slices.Contains(cpu.Flags, "avx")

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

return
}

func NewGPUs() (gpus GPUs) {
func (cpu *CPU) String() string {
return cpu.Model
}

func GetGPUs() (gpus GPUs) {
card := regexp.MustCompile(`card([0-9]+)(?:-eDP-\d+)?$`)

filepath.Walk("/sys/class/drm", func(p string, i fs.FileInfo, err error) error {
Expand Down
7 changes: 5 additions & 2 deletions wine/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ type Cmd struct {

func (p *Prefix) Command(name string, arg ...string) *Cmd {
cmd := exec.Command(name, arg...)
cmd.Stderr = p.Output
cmd.Stdout = p.Output
if p.Output != nil {
cmd.Stderr = p.Output
cmd.Stdout = p.Output
}

cmd.Env = append(cmd.Environ(),
"WINEPREFIX="+p.Dir,
)
Expand Down
2 changes: 0 additions & 2 deletions wine/prefix.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package wine
import (
"io"
"log"
"os"
)

type Prefix struct {
Expand All @@ -14,7 +13,6 @@ type Prefix struct {
func New(dir string) Prefix {
return Prefix{
Dir: dir,
Output: os.Stderr,
}
}

Expand Down

0 comments on commit 4af4fce

Please sign in to comment.