From e25e1af8cbd3c0d9f3f6474a3b8d09f92274ffcb Mon Sep 17 00:00:00 2001 From: rcourtman Date: Thu, 27 Nov 2025 09:16:53 +0000 Subject: [PATCH] chore: fix staticcheck SA warnings - Fix SA4006 unused value issues in ssh.go, validation.go, generator.go - Replace deprecated ioutil with io/os in config.go - Replace deprecated tar.TypeRegA with tar.TypeReg - Remove deprecated rand.Seed calls (auto-seeded in Go 1.20+) - Fix always-true nil check in main.go - Fix impossible nil comparison in tempproxy/client.go - Add nil check for config in monitor.New() --- cmd/pulse-sensor-proxy/ssh.go | 3 +-- cmd/pulse-sensor-proxy/validation.go | 5 +---- cmd/pulse/config.go | 8 ++++---- cmd/pulse/main.go | 10 ++++------ internal/agentbinaries/host_agent.go | 4 ++-- internal/mock/generator.go | 5 ++--- .../monitoring/metrics_history_concurrency_test.go | 4 +--- internal/monitoring/monitor.go | 6 +++++- internal/tempproxy/client.go | 5 +++-- 9 files changed, 23 insertions(+), 27 deletions(-) diff --git a/cmd/pulse-sensor-proxy/ssh.go b/cmd/pulse-sensor-proxy/ssh.go index b9bfbaf..f93a559 100644 --- a/cmd/pulse-sensor-proxy/ssh.go +++ b/cmd/pulse-sensor-proxy/ssh.go @@ -1023,8 +1023,7 @@ func (p *Proxy) getTemperatureLocal(ctx context.Context) (string, error) { if err != nil { // Try without -j flag as fallback cmd = exec.CommandContext(ctx, "sensors") - output, err = cmd.Output() - if err != nil { + if _, err = cmd.Output(); err != nil { return "", fmt.Errorf("failed to run sensors: %w", err) } // Return empty JSON object for non-JSON output diff --git a/cmd/pulse-sensor-proxy/validation.go b/cmd/pulse-sensor-proxy/validation.go index 21c180f..8a100ac 100644 --- a/cmd/pulse-sensor-proxy/validation.go +++ b/cmd/pulse-sensor-proxy/validation.go @@ -412,12 +412,9 @@ func (v *nodeValidator) Validate(ctx context.Context, node string) error { return nil } -func (v *nodeValidator) validateAsLocalhost(ctx context.Context, node string) error { +func (v *nodeValidator) validateAsLocalhost(_ context.Context, node string) error { // When cluster validation is unavailable, only allow access to localhost // This maintains security while allowing self-monitoring - if ctx == nil { - ctx = context.Background() - } // Try to discover local host addresses localAddrs, err := discoverLocalHostAddresses() diff --git a/cmd/pulse/config.go b/cmd/pulse/config.go index 6314c7b..3fb6789 100644 --- a/cmd/pulse/config.go +++ b/cmd/pulse/config.go @@ -4,7 +4,7 @@ import ( "bufio" "encoding/base64" "fmt" - "io/ioutil" + "io" "net/http" "net/url" "os" @@ -86,7 +86,7 @@ var configExportCmd = &cobra.Command{ // Write to file or stdout if exportFile != "" { - if err := ioutil.WriteFile(exportFile, []byte(exportedData), 0600); err != nil { + if err := os.WriteFile(exportFile, []byte(exportedData), 0600); err != nil { return fmt.Errorf("failed to write export file: %w", err) } fmt.Printf("Configuration exported to %s\n", exportFile) @@ -117,7 +117,7 @@ var configImportCmd = &cobra.Command{ } // Read import file - data, err := ioutil.ReadFile(importFile) + data, err := os.ReadFile(importFile) if err != nil { return fmt.Errorf("failed to read import file: %w", err) } @@ -245,7 +245,7 @@ var configAutoImportCmd = &cobra.Command{ return fmt.Errorf("failed to fetch configuration from URL: %s", resp.Status) } - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) if err != nil { return fmt.Errorf("failed to read configuration response: %w", err) } diff --git a/cmd/pulse/main.go b/cmd/pulse/main.go index 993d0e7..2797da5 100644 --- a/cmd/pulse/main.go +++ b/cmd/pulse/main.go @@ -253,12 +253,10 @@ func runServer() { configWatcher.ReloadConfig() } - if reloadFunc != nil { - if err := reloadFunc(); err != nil { - log.Error().Err(err).Msg("Failed to reload monitor after SIGHUP") - } else { - log.Info().Msg("Runtime configuration reloaded") - } + if err := reloadFunc(); err != nil { + log.Error().Err(err).Msg("Failed to reload monitor after SIGHUP") + } else { + log.Info().Msg("Runtime configuration reloaded") } case <-sigChan: diff --git a/internal/agentbinaries/host_agent.go b/internal/agentbinaries/host_agent.go index 448f2af..b51d735 100644 --- a/internal/agentbinaries/host_agent.go +++ b/internal/agentbinaries/host_agent.go @@ -236,7 +236,7 @@ func extractHostAgentBinaries(archivePath, targetDir string) error { continue } - if header.Typeflag != tar.TypeReg && header.Typeflag != tar.TypeRegA && header.Typeflag != tar.TypeSymlink { + if header.Typeflag != tar.TypeReg && header.Typeflag != tar.TypeSymlink { continue } @@ -252,7 +252,7 @@ func extractHostAgentBinaries(archivePath, targetDir string) error { destPath := filepath.Join(targetDir, base) switch header.Typeflag { - case tar.TypeReg, tar.TypeRegA: + case tar.TypeReg: if err := writeHostAgentFile(destPath, tr, header.FileInfo().Mode()); err != nil { return err } diff --git a/internal/mock/generator.go b/internal/mock/generator.go index 7ca6d43..4b5d037 100644 --- a/internal/mock/generator.go +++ b/internal/mock/generator.go @@ -226,7 +226,7 @@ func generateVirtualDisks() ([]models.Disk, models.Disk) { } func GenerateMockData(config MockConfig) models.StateSnapshot { - rand.Seed(time.Now().UnixNano()) + // rand is automatically seeded in Go 1.20+ data := models.StateSnapshot{ Nodes: generateNodes(config), @@ -273,8 +273,7 @@ func GenerateMockData(config MockConfig) models.StateSnapshot { } // Calculate VM count based on node role - vmCount := config.VMsPerNode - lxcCount := config.LXCsPerNode + var vmCount, lxcCount int switch nodeRole { case "vm-heavy": diff --git a/internal/monitoring/metrics_history_concurrency_test.go b/internal/monitoring/metrics_history_concurrency_test.go index 26105ee..2417496 100644 --- a/internal/monitoring/metrics_history_concurrency_test.go +++ b/internal/monitoring/metrics_history_concurrency_test.go @@ -59,6 +59,4 @@ func TestMetricsHistoryConcurrentAccess(t *testing.T) { wg.Wait() } -func init() { - rand.Seed(time.Now().UnixNano()) -} +// rand is automatically seeded in Go 1.20+ diff --git a/internal/monitoring/monitor.go b/internal/monitoring/monitor.go index e648e6d..050b463 100644 --- a/internal/monitoring/monitor.go +++ b/internal/monitoring/monitor.go @@ -3700,6 +3700,10 @@ func checkContainerizedTempMonitoring() { // New creates a new Monitor instance func New(cfg *config.Config) (*Monitor, error) { + if cfg == nil { + return nil, fmt.Errorf("config cannot be nil") + } + // Initialize temperature collector with sensors SSH key // Will use root user for now - can be made configurable later homeDir := os.Getenv("HOME") @@ -3712,7 +3716,7 @@ func New(cfg *config.Config) (*Monitor, error) { // Security warning if running in container with SSH temperature monitoring checkContainerizedTempMonitoring() - if cfg != nil && cfg.TemperatureMonitoringEnabled { + if cfg.TemperatureMonitoringEnabled { isContainer := os.Getenv("PULSE_DOCKER") == "true" || system.InContainer() if isContainer && tempCollector != nil && !tempCollector.SocketProxyAvailable() { log.Warn().Msg("Temperature monitoring is enabled but the container does not have access to pulse-sensor-proxy. Install the proxy on the host or disable temperatures until it is available.") diff --git a/internal/tempproxy/client.go b/internal/tempproxy/client.go index b4dd1b4..b5ac220 100644 --- a/internal/tempproxy/client.go +++ b/internal/tempproxy/client.go @@ -278,8 +278,9 @@ func (c *Client) callWithContext(ctx context.Context, method string, params map[ return resp, proxyErr } - lastErr = proxyErr - if lastErr == nil { + if proxyErr != nil { + lastErr = proxyErr + } else { lastErr = err }