Skip to content

Commit

Permalink
Separate hardware and software from eachother and sysinfo
Browse files Browse the repository at this point in the history
  • Loading branch information
Sploder12 committed Feb 3, 2025
1 parent 6573713 commit 87817ff
Show file tree
Hide file tree
Showing 100 changed files with 1,066 additions and 900 deletions.
34 changes: 34 additions & 0 deletions internal/cmdutils/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Package cmdutils provides utility functions for running commands.
package cmdutils

import (
"bytes"
"context"
"os"
"os/exec"
"time"
)

// Run executes the command specified by cmd with arguments args using the provided context.
// Returns stdout and stderr output and error code.
func Run(ctx context.Context, cmd string, args ...string) (stdout, stderr *bytes.Buffer, err error) {
stdout = &bytes.Buffer{}
stderr = &bytes.Buffer{}

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

return stdout, stderr, err
}

// RunWithTimeout calls Run but a timeout is added to the provided context.
func RunWithTimeout(ctx context.Context, timeout time.Duration, cmd string, args ...string) (stdout, stderr *bytes.Buffer, err error) {
c, cancel := context.WithTimeout(ctx, timeout)
defer cancel()

return Run(c, cmd, args...)
}
31 changes: 0 additions & 31 deletions internal/collector/sysinfo/cmd.go

This file was deleted.

14 changes: 10 additions & 4 deletions internal/collector/sysinfo/export_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package sysinfo

import (
"log/slog"
"github.com/ubuntu/ubuntu-insights/internal/collector/sysinfo/hardware"
"github.com/ubuntu/ubuntu-insights/internal/collector/sysinfo/software"
)

// WithLogger overrides the default logger.
func WithLogger(logger slog.Handler) Options {
func WithHardwareCollector(hw hardware.Collector) Options {
return func(o *options) {
o.log = slog.New(logger)
o.hw = hw
}
}

func WithSoftwareCollector(sw software.Collector) Options {
return func(o *options) {
o.sw = sw
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package sysinfo
package hardware

// WithRoot overrides default root directory of the system.
func WithRoot(root string) Options {
Expand Down
12 changes: 12 additions & 0 deletions internal/collector/sysinfo/hardware/export_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package hardware

import (
"log/slog"
)

// WithLogger overrides the default logger.
func WithLogger(logger slog.Handler) Options {
return func(o *options) {
o.log = slog.New(logger)
}
}
103 changes: 103 additions & 0 deletions internal/collector/sysinfo/hardware/hardware.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
// Package hardware handles collecting "common" hardware information for all insight reports.
package hardware

// Collector describes a type that collects hardware information.
type Collector interface {
Collect() (Info, error)
}

// Info aggregates hardware info.
type Info struct {
Product product

CPU cpu
GPUs []gpu
Mem memory
Blks []disk
Screens []screen
}

type product map[string]string
type cpu map[string]string
type gpu map[string]string
type memory map[string]int

// DiskInfo contains information of a disk or partition.
type disk struct {
Name string `json:"name"`
Size string `json:"size"`

Partitions []disk `json:"partitions,omitempty"`
}

// Screen contains information for a screen.
type screen struct {
Name string `json:"name"`
PhysicalResolution string `json:"physicalResolution"`
Size string `json:"size"`
Resolution string `json:"resolution"`
RefreshRate string `json:"refreshRate"`
}

// Manager handles dependencies for collecting hardware information.
// Manager implements hardware.Collector.
type Manager struct {
opts options
}

// Options are the variadic options available to the manager.
type Options func(*options)

// New returns a new Manager.
func New(args ...Options) Manager {
// options defaults are platform dependent.
opts := defaultOptions()
for _, opt := range args {
opt(opts)
}

return Manager{
opts: *opts,
}
}

// Collect aggregates the data from all the other hardware collect functions.
func (s Manager) Collect() (info Info, err error) {
info.Product, err = s.collectProduct()
if err != nil {
s.opts.log.Warn("failed to collect Product info", "error", err)
info.Product = product{}
}

info.CPU, err = s.collectCPU()
if err != nil {
s.opts.log.Warn("failed to collect CPU info", "error", err)
info.CPU = cpu{}
}

info.GPUs, err = s.collectGPUs()
if err != nil {
s.opts.log.Warn("failed to collect GPU info", "error", err)
info.GPUs = []gpu{}
}

info.Mem, err = s.collectMemory()
if err != nil {
s.opts.log.Warn("failed to collect memory info", "error", err)
info.Mem = memory{}
}

info.Blks, err = s.collectDisks()
if err != nil {
s.opts.log.Warn("failed to collect disk info", "error", err)
info.Blks = []disk{}
}

info.Screens, err = s.collectScreens()
if err != nil {
s.opts.log.Warn("failed to collect screen info", "error", err)
info.Screens = []screen{}
}

return info, nil
}
35 changes: 35 additions & 0 deletions internal/collector/sysinfo/hardware/hardware_darwin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package hardware

import "log/slog"

type options struct {
log *slog.Logger
}

func defaultOptions() *options {
return &options{}
}

func (s Manager) collectProduct() (product, error) {
return product{}, nil
}

func (s Manager) collectCPU() (cpu, error) {
return cpu{}, nil
}

func (s Manager) collectGPUs() ([]gpu, error) {
return []gpu{}, nil
}

func (s Manager) collectMemory() (memory, error) {
return memory{}, nil
}

func (s Manager) collectDisks() ([]disk, error) {
return []disk{}, nil
}

func (s Manager) collectScreens() ([]screen, error) {
return []screen{}, nil
}
Loading

0 comments on commit 87817ff

Please sign in to comment.