Skip to content

Commit

Permalink
Refactor according to feedback
Browse files Browse the repository at this point in the history
Co-authored-by: Didier Roche-Tolomelli <[email protected]>
  • Loading branch information
Sploder12 and didrocks committed Jan 22, 2025
1 parent e4cdb91 commit f420821
Show file tree
Hide file tree
Showing 14 changed files with 356 additions and 349 deletions.
10 changes: 7 additions & 3 deletions internal/collector/sysinfo/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@ import (
"os/exec"
)

func runCmd(ctx context.Context, cmd string, args ...string) (stdout bytes.Buffer, stderr bytes.Buffer, err error) {
func runCmd(ctx context.Context, cmd string, args ...string) (stdout, stderr *bytes.Buffer, err error) {

Check failure on line 10 in internal/collector/sysinfo/cmd.go

View workflow job for this annotation

GitHub Actions / Go: Code sanity (windows-2022)

func `runCmd` is unused (unused)

Check failure on line 10 in internal/collector/sysinfo/cmd.go

View workflow job for this annotation

GitHub Actions / Go: Code sanity (macos-13)

func `runCmd` is unused (unused)

Check failure on line 10 in internal/collector/sysinfo/cmd.go

View workflow job for this annotation

GitHub Actions / Go: Code sanity (macos-14)

func `runCmd` is unused (unused)
stdout = &bytes.Buffer{}
stderr = &bytes.Buffer{}

c := exec.CommandContext(ctx, cmd, args...)
c.Stdout = &stdout
c.Stderr = &stderr
c.Stdout = stdout
c.Stderr = stderr
c.Env = append(c.Env, "LANG=C")
err = c.Run()

return stdout, stderr, err
Expand Down
14 changes: 6 additions & 8 deletions internal/collector/sysinfo/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,21 @@ package sysinfo

import (
"encoding/json"
"errors"
"fmt"
"io"
)

func parseJSON(r io.Reader, v any) (any, error) {
// Read the entire content of the io.Reader first to check for errors even if valid json is first
// parseJSON unmarshals the data in r into v.
func parseJSON(r io.Reader, v any) error {

Check failure on line 10 in internal/collector/sysinfo/json.go

View workflow job for this annotation

GitHub Actions / Go: Code sanity (windows-2022)

func `parseJSON` is unused (unused)

Check failure on line 10 in internal/collector/sysinfo/json.go

View workflow job for this annotation

GitHub Actions / Go: Code sanity (macos-13)

func `parseJSON` is unused (unused)

Check failure on line 10 in internal/collector/sysinfo/json.go

View workflow job for this annotation

GitHub Actions / Go: Code sanity (macos-14)

func `parseJSON` is unused (unused)
// Read the entire content of the io.Reader first to check for errors even if valid json is first.
buf, err := io.ReadAll(r)
if err != nil {
s := fmt.Sprintf("error reading from io.Reader: %v", err)
return nil, errors.New(s)
return fmt.Errorf("error reading from io.Reader: %v", err)
}

err = json.Unmarshal(buf, v)
if err != nil {
s := fmt.Sprintf("couldn't parse JSON: %v", err)
return nil, errors.New(s)
return fmt.Errorf("couldn't parse JSON: %v", err)
}
return v, nil
return nil
}
21 changes: 3 additions & 18 deletions internal/collector/sysinfo/sysinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,13 @@ type SysInfo struct {
type hwInfo struct {
Product map[string]string

CPU cpuInfo
GPUs []gpuInfo
Mem memInfo
CPU map[string]string
GPUs []map[string]string
Mem map[string]int
Blks []diskInfo
Screens []screenInfo
}

// CpuInfo contains CPU information of a machine.
type cpuInfo struct {
CPU map[string]string
}

// GpuInfo contains GPU information of a specific GPU.
type gpuInfo struct {
GPU map[string]string
}

// MemInfo contains Memory information of a machine.
type memInfo struct {
Mem map[string]int
}

// DiskInfo contains Disk information of a disk or partition.
type diskInfo struct {
Name string
Expand Down
Loading

0 comments on commit f420821

Please sign in to comment.