fix: use proper go version in module file (#12)

This commit is contained in:
Karol Broda
2025-12-24 12:12:46 +01:00
committed by GitHub
parent b0226d1286
commit 268226257b
12 changed files with 249 additions and 166 deletions

View File

@@ -14,6 +14,8 @@ import (
"sync"
"sync/atomic"
"time"
"github.com/karol-broda/snitch/internal/errutil"
)
// set SNITCH_DEBUG_TIMING=1 to enable timing diagnostics
@@ -138,7 +140,7 @@ func buildInodeToProcessMap() (map[int64]*processInfo, error) {
if err != nil {
return nil, err
}
defer procDir.Close()
defer errutil.Close(procDir)
entries, err := procDir.Readdir(-1)
if err != nil {
@@ -278,7 +280,7 @@ func getProcessInfo(pid int) (*processInfo, error) {
if err != nil {
return info, nil
}
defer statusFile.Close()
defer errutil.Close(statusFile)
scanner := bufio.NewScanner(statusFile)
for scanner.Scan() {
@@ -304,7 +306,7 @@ func parseProcNet(path, proto string, ipVersion int, inodeMap map[int64]*process
if err != nil {
return nil, err
}
defer file.Close()
defer errutil.Close(file)
var connections []Connection
scanner := bufio.NewScanner(file)
@@ -473,7 +475,7 @@ func GetUnixSockets() ([]Connection, error) {
if err != nil {
return connections, nil
}
defer file.Close()
defer errutil.Close(file)
scanner := bufio.NewScanner(file)
scanner.Scan()

View File

@@ -5,6 +5,8 @@ import (
"testing"
"github.com/fatih/color"
"github.com/karol-broda/snitch/internal/errutil"
)
func TestInit(t *testing.T) {
@@ -29,8 +31,8 @@ func TestInit(t *testing.T) {
origTerm := os.Getenv("TERM")
// Set test env vars
os.Setenv("NO_COLOR", tc.noColor)
os.Setenv("TERM", tc.term)
errutil.Setenv("NO_COLOR", tc.noColor)
errutil.Setenv("TERM", tc.term)
Init(tc.mode)
@@ -39,8 +41,8 @@ func TestInit(t *testing.T) {
}
// Restore original env vars
os.Setenv("NO_COLOR", origNoColor)
os.Setenv("TERM", origTerm)
errutil.Setenv("NO_COLOR", origNoColor)
errutil.Setenv("TERM", origTerm)
})
}
}

View File

@@ -0,0 +1,65 @@
package errutil
import (
"io"
"os"
"github.com/fatih/color"
)
func Ignore[T any](val T, _ error) T {
return val
}
func IgnoreErr(_ error) {}
func Close(c io.Closer) {
if c != nil {
_ = c.Close()
}
}
// color.Color wrappers - these discard the (int, error) return values
func Print(c *color.Color, a ...any) {
_, _ = c.Print(a...)
}
func Println(c *color.Color, a ...any) {
_, _ = c.Println(a...)
}
func Printf(c *color.Color, format string, a ...any) {
_, _ = c.Printf(format, a...)
}
func Fprintf(c *color.Color, w io.Writer, format string, a ...any) {
_, _ = c.Fprintf(w, format, a...)
}
// os function wrappers for test cleanup where errors are non-critical
func Setenv(key, value string) {
_ = os.Setenv(key, value)
}
func Unsetenv(key string) {
_ = os.Unsetenv(key)
}
func Remove(name string) {
_ = os.Remove(name)
}
func RemoveAll(path string) {
_ = os.RemoveAll(path)
}
// Flush calls Flush on a tabwriter and discards the error
type Flusher interface {
Flush() error
}
func Flush(f Flusher) {
_ = f.Flush()
}

View File

@@ -6,6 +6,7 @@ import (
"testing"
"github.com/karol-broda/snitch/internal/collector"
"github.com/karol-broda/snitch/internal/errutil"
)
// TestCollector wraps MockCollector for use in tests
@@ -47,13 +48,13 @@ func SetupTestEnvironment(t *testing.T) (string, func()) {
oldConfig := os.Getenv("SNITCH_CONFIG")
oldNoColor := os.Getenv("SNITCH_NO_COLOR")
os.Setenv("SNITCH_NO_COLOR", "1") // Disable colors in tests
errutil.Setenv("SNITCH_NO_COLOR", "1")
// Cleanup function
cleanup := func() {
os.RemoveAll(tempDir)
os.Setenv("SNITCH_CONFIG", oldConfig)
os.Setenv("SNITCH_NO_COLOR", oldNoColor)
errutil.RemoveAll(tempDir)
errutil.Setenv("SNITCH_CONFIG", oldConfig)
errutil.Setenv("SNITCH_NO_COLOR", oldNoColor)
}
return tempDir, cleanup
@@ -192,8 +193,8 @@ func (oc *OutputCapture) Stop() (string, string, error) {
os.Stderr = oc.oldStderr
// Close files
oc.stdout.Close()
oc.stderr.Close()
errutil.Close(oc.stdout)
errutil.Close(oc.stderr)
// Read captured content
stdoutContent, err := os.ReadFile(oc.stdoutFile)
@@ -207,9 +208,9 @@ func (oc *OutputCapture) Stop() (string, string, error) {
}
// Cleanup
os.Remove(oc.stdoutFile)
os.Remove(oc.stderrFile)
os.Remove(filepath.Dir(oc.stdoutFile))
errutil.Remove(oc.stdoutFile)
errutil.Remove(oc.stderrFile)
errutil.Remove(filepath.Dir(oc.stdoutFile))
return string(stdoutContent), string(stderrContent), nil
}