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

@@ -32,9 +32,9 @@ jobs:
go-version: "1.25.0"
- name: lint
uses: golangci/golangci-lint-action@v6
uses: golangci/golangci-lint-action@v8
with:
version: latest
version: v2.5.0
nix-build:
strategy:

View File

@@ -6,6 +6,7 @@ import (
"strings"
"testing"
"github.com/karol-broda/snitch/internal/errutil"
"github.com/karol-broda/snitch/internal/testutil"
)
@@ -407,16 +408,16 @@ func TestEnvironmentVariables(t *testing.T) {
oldEnvVars := make(map[string]string)
for key, value := range tt.envVars {
oldEnvVars[key] = os.Getenv(key)
os.Setenv(key, value)
errutil.Setenv(key, value)
}
// Clean up environment variables
defer func() {
for key, oldValue := range oldEnvVars {
if oldValue == "" {
os.Unsetenv(key)
errutil.Unsetenv(key)
} else {
os.Setenv(key, oldValue)
errutil.Setenv(key, oldValue)
}
}
}()

View File

@@ -8,16 +8,18 @@ import (
"log"
"os"
"os/exec"
"github.com/karol-broda/snitch/internal/collector"
"github.com/karol-broda/snitch/internal/color"
"github.com/karol-broda/snitch/internal/config"
"github.com/karol-broda/snitch/internal/resolver"
"strconv"
"strings"
"text/tabwriter"
"github.com/charmbracelet/lipgloss"
"github.com/spf13/cobra"
"github.com/karol-broda/snitch/internal/collector"
"github.com/karol-broda/snitch/internal/color"
"github.com/karol-broda/snitch/internal/config"
"github.com/karol-broda/snitch/internal/errutil"
"github.com/karol-broda/snitch/internal/resolver"
"github.com/tidwall/pretty"
"golang.org/x/term"
)
@@ -185,7 +187,7 @@ func printCSV(conns []collector.Connection, headers bool, timestamp bool, select
func printPlainTable(conns []collector.Connection, headers bool, timestamp bool, selectedFields []string) {
w := tabwriter.NewWriter(os.Stdout, 0, 0, 3, ' ', 0)
defer w.Flush()
defer errutil.Flush(w)
if len(selectedFields) == 0 {
selectedFields = []string{"pid", "process", "user", "proto", "state", "laddr", "lport", "raddr", "rport"}
@@ -199,7 +201,7 @@ func printPlainTable(conns []collector.Connection, headers bool, timestamp bool,
for _, field := range selectedFields {
headerRow = append(headerRow, strings.ToUpper(field))
}
fmt.Fprintln(w, strings.Join(headerRow, "\t"))
errutil.Ignore(fmt.Fprintln(w, strings.Join(headerRow, "\t")))
}
for _, conn := range conns {
@@ -208,7 +210,7 @@ func printPlainTable(conns []collector.Connection, headers bool, timestamp bool,
for _, field := range selectedFields {
row = append(row, fieldMap[field])
}
fmt.Fprintln(w, strings.Join(row, "\t"))
errutil.Ignore(fmt.Fprintln(w, strings.Join(row, "\t")))
}
}

View File

@@ -8,7 +8,6 @@ import (
"log"
"os"
"os/signal"
"github.com/karol-broda/snitch/internal/collector"
"sort"
"strconv"
"strings"
@@ -17,6 +16,9 @@ import (
"time"
"github.com/spf13/cobra"
"github.com/karol-broda/snitch/internal/collector"
"github.com/karol-broda/snitch/internal/errutil"
)
type StatsData struct {
@@ -227,19 +229,19 @@ func printStatsCSV(stats *StatsData, headers bool) {
func printStatsTable(stats *StatsData, headers bool) {
w := tabwriter.NewWriter(os.Stdout, 0, 0, 3, ' ', 0)
defer w.Flush()
defer errutil.Flush(w)
if headers {
fmt.Fprintf(w, "TIMESTAMP\t%s\n", stats.Timestamp.Format(time.RFC3339))
fmt.Fprintf(w, "TOTAL CONNECTIONS\t%d\n", stats.Total)
fmt.Fprintln(w)
errutil.Ignore(fmt.Fprintf(w, "TIMESTAMP\t%s\n", stats.Timestamp.Format(time.RFC3339)))
errutil.Ignore(fmt.Fprintf(w, "TOTAL CONNECTIONS\t%d\n", stats.Total))
errutil.Ignore(fmt.Fprintln(w))
}
// Protocol breakdown
if len(stats.ByProto) > 0 {
if headers {
fmt.Fprintln(w, "BY PROTOCOL:")
fmt.Fprintln(w, "PROTO\tCOUNT")
errutil.Ignore(fmt.Fprintln(w, "BY PROTOCOL:"))
errutil.Ignore(fmt.Fprintln(w, "PROTO\tCOUNT"))
}
protocols := make([]string, 0, len(stats.ByProto))
for proto := range stats.ByProto {
@@ -247,16 +249,16 @@ func printStatsTable(stats *StatsData, headers bool) {
}
sort.Strings(protocols)
for _, proto := range protocols {
fmt.Fprintf(w, "%s\t%d\n", strings.ToUpper(proto), stats.ByProto[proto])
errutil.Ignore(fmt.Fprintf(w, "%s\t%d\n", strings.ToUpper(proto), stats.ByProto[proto]))
}
fmt.Fprintln(w)
errutil.Ignore(fmt.Fprintln(w))
}
// State breakdown
if len(stats.ByState) > 0 {
if headers {
fmt.Fprintln(w, "BY STATE:")
fmt.Fprintln(w, "STATE\tCOUNT")
errutil.Ignore(fmt.Fprintln(w, "BY STATE:"))
errutil.Ignore(fmt.Fprintln(w, "STATE\tCOUNT"))
}
states := make([]string, 0, len(stats.ByState))
for state := range stats.ByState {
@@ -264,16 +266,16 @@ func printStatsTable(stats *StatsData, headers bool) {
}
sort.Strings(states)
for _, state := range states {
fmt.Fprintf(w, "%s\t%d\n", state, stats.ByState[state])
errutil.Ignore(fmt.Fprintf(w, "%s\t%d\n", state, stats.ByState[state]))
}
fmt.Fprintln(w)
errutil.Ignore(fmt.Fprintln(w))
}
// Process breakdown (top 10)
if len(stats.ByProc) > 0 {
if headers {
fmt.Fprintln(w, "BY PROCESS (TOP 10):")
fmt.Fprintln(w, "PID\tPROCESS\tCOUNT")
errutil.Ignore(fmt.Fprintln(w, "BY PROCESS (TOP 10):"))
errutil.Ignore(fmt.Fprintln(w, "PID\tPROCESS\tCOUNT"))
}
limit := 10
if len(stats.ByProc) < limit {
@@ -281,7 +283,7 @@ func printStatsTable(stats *StatsData, headers bool) {
}
for i := 0; i < limit; i++ {
proc := stats.ByProc[i]
fmt.Fprintf(w, "%d\t%s\t%d\n", proc.PID, proc.Process, proc.Count)
errutil.Ignore(fmt.Fprintf(w, "%d\t%s\t%d\n", proc.PID, proc.Process, proc.Count))
}
}
}

View File

@@ -18,6 +18,7 @@ import (
"github.com/fatih/color"
"github.com/spf13/cobra"
"github.com/karol-broda/snitch/internal/errutil"
"github.com/karol-broda/snitch/internal/tui"
)
@@ -93,13 +94,13 @@ func runUpgrade(cmd *cobra.Command, args []string) error {
if currentClean == latestClean {
green := color.New(color.FgGreen)
green.Println(tui.SymbolSuccess + " you are running the latest version")
errutil.Println(green, tui.SymbolSuccess+" you are running the latest version")
return nil
}
if current == "dev" {
yellow := color.New(color.FgYellow)
yellow.Println(tui.SymbolWarning + " you are running a development build")
errutil.Println(yellow, tui.SymbolWarning+" you are running a development build")
fmt.Println()
fmt.Println("use one of the methods below to install a release version:")
fmt.Println()
@@ -108,7 +109,7 @@ func runUpgrade(cmd *cobra.Command, args []string) error {
}
green := color.New(color.FgGreen, color.Bold)
green.Printf(tui.SymbolSuccess+" update available: %s "+tui.SymbolArrowRight+" %s\n", current, latest)
errutil.Printf(green, tui.SymbolSuccess+" update available: %s "+tui.SymbolArrowRight+" %s\n", current, latest)
fmt.Println()
if !upgradeYes {
@@ -116,8 +117,8 @@ func runUpgrade(cmd *cobra.Command, args []string) error {
fmt.Println()
faint := color.New(color.Faint)
cmdStyle := color.New(color.FgCyan)
faint.Print(" in-place ")
cmdStyle.Println("snitch upgrade --yes")
errutil.Print(faint, " in-place ")
errutil.Println(cmdStyle, "snitch upgrade --yes")
return nil
}
@@ -134,17 +135,17 @@ func handleSpecificVersion(current, target string) error {
if isVersionLower(targetClean, firstUpgradeVersion) {
yellow := color.New(color.FgYellow)
yellow.Printf(tui.SymbolWarning+" warning: the upgrade command was introduced in v%s\n", firstUpgradeVersion)
errutil.Printf(yellow, tui.SymbolWarning+" warning: the upgrade command was introduced in v%s\n", firstUpgradeVersion)
faint := color.New(color.Faint)
faint.Printf(" version %s does not include this command\n", target)
faint.Println(" you will need to use other methods to upgrade from that version")
errutil.Printf(faint, " version %s does not include this command\n", target)
errutil.Println(faint, " you will need to use other methods to upgrade from that version")
fmt.Println()
}
currentClean := strings.TrimPrefix(current, "v")
if currentClean == targetClean {
green := color.New(color.FgGreen)
green.Println(tui.SymbolSuccess + " you are already running this version")
errutil.Println(green, tui.SymbolSuccess+" you are already running this version")
return nil
}
@@ -153,15 +154,15 @@ func handleSpecificVersion(current, target string) error {
cmdStyle := color.New(color.FgCyan)
if isVersionLower(targetClean, currentClean) {
yellow := color.New(color.FgYellow)
yellow.Printf(tui.SymbolArrowDown+" this will downgrade from %s to %s\n", current, target)
errutil.Printf(yellow, tui.SymbolArrowDown+" this will downgrade from %s to %s\n", current, target)
} else {
green := color.New(color.FgGreen)
green.Printf(tui.SymbolArrowUp+" this will upgrade from %s to %s\n", current, target)
errutil.Printf(green, tui.SymbolArrowUp+" this will upgrade from %s to %s\n", current, target)
}
fmt.Println()
faint.Print("run ")
cmdStyle.Printf("snitch upgrade --version %s --yes", target)
faint.Println(" to proceed")
errutil.Print(faint, "run ")
errutil.Printf(cmdStyle, "snitch upgrade --version %s --yes", target)
errutil.Println(faint, " to proceed")
return nil
}
@@ -175,20 +176,20 @@ func handleNixUpgrade(current, latest string) error {
currentCommit := extractCommitFromVersion(current)
dirty := isNixDirty(current)
faint.Print("current ")
version.Print(current)
errutil.Print(faint, "current ")
errutil.Print(version, current)
if currentCommit != "" {
faint.Printf(" (commit %s)", currentCommit)
errutil.Printf(faint, " (commit %s)", currentCommit)
}
fmt.Println()
faint.Print("latest ")
version.Println(latest)
errutil.Print(faint, "latest ")
errutil.Println(version, latest)
fmt.Println()
if dirty {
yellow := color.New(color.FgYellow)
yellow.Println(tui.SymbolWarning + " you are running a dirty nix build (uncommitted changes)")
errutil.Println(yellow, tui.SymbolWarning+" you are running a dirty nix build (uncommitted changes)")
fmt.Println()
printNixUpgradeInstructions()
return nil
@@ -196,8 +197,8 @@ func handleNixUpgrade(current, latest string) error {
if currentCommit == "" {
yellow := color.New(color.FgYellow)
yellow.Println(tui.SymbolWarning + " this is a nix installation")
faint.Println(" nix store is immutable; use nix commands to upgrade")
errutil.Println(yellow, tui.SymbolWarning+" this is a nix installation")
errutil.Println(faint, " nix store is immutable; use nix commands to upgrade")
fmt.Println()
printNixUpgradeInstructions()
return nil
@@ -205,11 +206,11 @@ func handleNixUpgrade(current, latest string) error {
releaseCommit, err := fetchCommitForTag(latest)
if err != nil {
faint.Printf(" (could not fetch release commit: %v)\n", err)
errutil.Printf(faint, " (could not fetch release commit: %v)\n", err)
fmt.Println()
yellow := color.New(color.FgYellow)
yellow.Println(tui.SymbolWarning + " this is a nix installation")
faint.Println(" nix store is immutable; use nix commands to upgrade")
errutil.Println(yellow, tui.SymbolWarning+" this is a nix installation")
errutil.Println(faint, " nix store is immutable; use nix commands to upgrade")
fmt.Println()
printNixUpgradeInstructions()
return nil
@@ -222,20 +223,20 @@ func handleNixUpgrade(current, latest string) error {
if strings.HasPrefix(releaseCommit, currentCommit) || strings.HasPrefix(currentCommit, releaseShort) {
green := color.New(color.FgGreen)
green.Printf(tui.SymbolSuccess+" you are running %s (commit %s)\n", latest, releaseShort)
errutil.Printf(green, tui.SymbolSuccess+" you are running %s (commit %s)\n", latest, releaseShort)
return nil
}
comparison, err := compareCommits(latest, currentCommit)
if err != nil {
green := color.New(color.FgGreen, color.Bold)
green.Printf(tui.SymbolSuccess+" update available: %s "+tui.SymbolArrowRight+" %s\n", currentCommit, latest)
faint.Printf(" your commit: %s\n", currentCommit)
faint.Printf(" release: %s (%s)\n", releaseShort, latest)
errutil.Printf(green, tui.SymbolSuccess+" update available: %s "+tui.SymbolArrowRight+" %s\n", currentCommit, latest)
errutil.Printf(faint, " your commit: %s\n", currentCommit)
errutil.Printf(faint, " release: %s (%s)\n", releaseShort, latest)
fmt.Println()
yellow := color.New(color.FgYellow)
yellow.Println(tui.SymbolWarning + " this is a nix installation")
faint.Println(" nix store is immutable; use nix commands to upgrade")
errutil.Println(yellow, tui.SymbolWarning+" this is a nix installation")
errutil.Println(faint, " nix store is immutable; use nix commands to upgrade")
fmt.Println()
printNixUpgradeInstructions()
return nil
@@ -243,30 +244,30 @@ func handleNixUpgrade(current, latest string) error {
if comparison.AheadBy > 0 {
cyan := color.New(color.FgCyan)
cyan.Printf(tui.SymbolArrowUp+" you are %d commit(s) ahead of %s\n", comparison.AheadBy, latest)
faint.Printf(" your commit: %s\n", currentCommit)
faint.Printf(" release: %s (%s)\n", releaseShort, latest)
errutil.Printf(cyan, tui.SymbolArrowUp+" you are %d commit(s) ahead of %s\n", comparison.AheadBy, latest)
errutil.Printf(faint, " your commit: %s\n", currentCommit)
errutil.Printf(faint, " release: %s (%s)\n", releaseShort, latest)
fmt.Println()
faint.Println("you are running a newer build than the latest release")
errutil.Println(faint, "you are running a newer build than the latest release")
return nil
}
if comparison.BehindBy > 0 {
green := color.New(color.FgGreen, color.Bold)
green.Printf(tui.SymbolSuccess+" update available: %d commit(s) behind %s\n", comparison.BehindBy, latest)
faint.Printf(" your commit: %s\n", currentCommit)
faint.Printf(" release: %s (%s)\n", releaseShort, latest)
errutil.Printf(green, tui.SymbolSuccess+" update available: %d commit(s) behind %s\n", comparison.BehindBy, latest)
errutil.Printf(faint, " your commit: %s\n", currentCommit)
errutil.Printf(faint, " release: %s (%s)\n", releaseShort, latest)
fmt.Println()
yellow := color.New(color.FgYellow)
yellow.Println(tui.SymbolWarning + " this is a nix installation")
faint.Println(" nix store is immutable; use nix commands to upgrade")
errutil.Println(yellow, tui.SymbolWarning+" this is a nix installation")
errutil.Println(faint, " nix store is immutable; use nix commands to upgrade")
fmt.Println()
printNixUpgradeInstructions()
return nil
}
green := color.New(color.FgGreen)
green.Printf(tui.SymbolSuccess+" you are running %s (commit %s)\n", latest, releaseShort)
errutil.Printf(green, tui.SymbolSuccess+" you are running %s (commit %s)\n", latest, releaseShort)
return nil
}
@@ -278,22 +279,22 @@ func handleNixSpecificVersion(current, target string) error {
printVersionComparisonTarget(current, target)
yellow := color.New(color.FgYellow)
yellow.Println(tui.SymbolWarning + " this is a nix installation")
errutil.Println(yellow, tui.SymbolWarning+" this is a nix installation")
faint := color.New(color.Faint)
faint.Println(" nix store is immutable; in-place upgrades are not supported")
errutil.Println(faint, " nix store is immutable; in-place upgrades are not supported")
fmt.Println()
bold := color.New(color.Bold)
cmd := color.New(color.FgCyan)
bold.Println("to install a specific version with nix:")
errutil.Println(bold, "to install a specific version with nix:")
fmt.Println()
faint.Print(" specific ref ")
cmd.Printf("nix profile install github:%s/%s/%s\n", repoOwner, repoName, target)
errutil.Print(faint, " specific ref ")
errutil.Printf(cmd, "nix profile install github:%s/%s/%s\n", repoOwner, repoName, target)
faint.Print(" latest ")
cmd.Printf("nix profile install github:%s/%s\n", repoOwner, repoName)
errutil.Print(faint, " latest ")
errutil.Printf(cmd, "nix profile install github:%s/%s\n", repoOwner, repoName)
return nil
}
@@ -333,7 +334,7 @@ func fetchLatestVersion() (string, error) {
if err != nil {
return "", err
}
defer resp.Body.Close()
defer errutil.Close(resp.Body)
if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("github api returned status %d", resp.StatusCode)
@@ -355,10 +356,10 @@ func printVersionComparison(current, latest string) {
faint := color.New(color.Faint)
version := color.New(color.FgCyan)
faint.Print("current ")
version.Println(current)
faint.Print("latest ")
version.Println(latest)
errutil.Print(faint, "current ")
errutil.Println(version, current)
errutil.Print(faint, "latest ")
errutil.Println(version, latest)
fmt.Println()
}
@@ -366,10 +367,10 @@ func printVersionComparisonTarget(current, target string) {
faint := color.New(color.Faint)
version := color.New(color.FgCyan)
faint.Print("current ")
version.Println(current)
faint.Print("target ")
version.Println(target)
errutil.Print(faint, "current ")
errutil.Println(version, current)
errutil.Print(faint, "target ")
errutil.Println(version, target)
fmt.Println()
}
@@ -378,20 +379,20 @@ func printUpgradeInstructions() {
faint := color.New(color.Faint)
cmd := color.New(color.FgCyan)
bold.Println("upgrade options:")
errutil.Println(bold, "upgrade options:")
fmt.Println()
faint.Print(" go install ")
cmd.Printf("go install github.com/%s/%s@latest\n", repoOwner, repoName)
errutil.Print(faint, " go install ")
errutil.Printf(cmd, "go install github.com/%s/%s@latest\n", repoOwner, repoName)
faint.Print(" shell script ")
cmd.Printf("curl -sSL https://raw.githubusercontent.com/%s/%s/master/install.sh | sh\n", repoOwner, repoName)
errutil.Print(faint, " shell script ")
errutil.Printf(cmd, "curl -sSL https://raw.githubusercontent.com/%s/%s/master/install.sh | sh\n", repoOwner, repoName)
faint.Print(" arch (aur) ")
cmd.Println("yay -S snitch-bin")
errutil.Print(faint, " arch (aur) ")
errutil.Println(cmd, "yay -S snitch-bin")
faint.Print(" nix ")
cmd.Printf("nix profile upgrade --inputs-from github:%s/%s\n", repoOwner, repoName)
errutil.Print(faint, " nix ")
errutil.Printf(cmd, "nix profile upgrade --inputs-from github:%s/%s\n", repoOwner, repoName)
}
func performUpgrade(version string) error {
@@ -407,7 +408,7 @@ func performUpgrade(version string) error {
if strings.HasPrefix(execPath, "/nix/store/") {
yellow := color.New(color.FgYellow)
yellow.Println(tui.SymbolWarning + " cannot perform in-place upgrade for nix installation")
errutil.Println(yellow, tui.SymbolWarning+" cannot perform in-place upgrade for nix installation")
fmt.Println()
printNixUpgradeInstructions()
return nil
@@ -423,15 +424,15 @@ func performUpgrade(version string) error {
faint := color.New(color.Faint)
cyan := color.New(color.FgCyan)
faint.Print(tui.SymbolDownload + " downloading ")
cyan.Printf("%s", archiveName)
faint.Println("...")
errutil.Print(faint, tui.SymbolDownload+" downloading ")
errutil.Printf(cyan, "%s", archiveName)
errutil.Println(faint, "...")
resp, err := http.Get(downloadURL)
if err != nil {
return fmt.Errorf("failed to download: %w", err)
}
defer resp.Body.Close()
defer errutil.Close(resp.Body)
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("download failed with status %d", resp.StatusCode)
@@ -441,7 +442,7 @@ func performUpgrade(version string) error {
if err != nil {
return fmt.Errorf("failed to create temp directory: %w", err)
}
defer os.RemoveAll(tmpDir)
defer errutil.RemoveAll(tmpDir)
binaryPath, err := extractBinaryFromTarGz(resp.Body, tmpDir)
if err != nil {
@@ -458,14 +459,14 @@ func performUpgrade(version string) error {
yellow := color.New(color.FgYellow)
cmdStyle := color.New(color.FgCyan)
yellow.Printf(tui.SymbolWarning+" elevated permissions required to install to %s\n", targetDir)
errutil.Printf(yellow, tui.SymbolWarning+" elevated permissions required to install to %s\n", targetDir)
fmt.Println()
faint.Println("run with sudo or install to a user-writable location:")
errutil.Println(faint, "run with sudo or install to a user-writable location:")
fmt.Println()
faint.Print(" sudo ")
cmdStyle.Println("sudo snitch upgrade --yes")
faint.Print(" custom dir ")
cmdStyle.Printf("curl -sSL https://raw.githubusercontent.com/%s/%s/master/install.sh | INSTALL_DIR=~/.local/bin sh\n",
errutil.Print(faint, " sudo ")
errutil.Println(cmdStyle, "sudo snitch upgrade --yes")
errutil.Print(faint, " custom dir ")
errutil.Printf(cmdStyle, "curl -sSL https://raw.githubusercontent.com/%s/%s/master/install.sh | INSTALL_DIR=~/.local/bin sh\n",
repoOwner, repoName)
return nil
}
@@ -491,11 +492,11 @@ func performUpgrade(version string) error {
if err := os.Remove(backupPath); err != nil {
// non-fatal, just warn
yellow := color.New(color.FgYellow)
yellow.Fprintf(os.Stderr, tui.SymbolWarning + " warning: failed to remove backup file %s: %v\n", backupPath, err)
errutil.Fprintf(yellow, os.Stderr, tui.SymbolWarning+" warning: failed to remove backup file %s: %v\n", backupPath, err)
}
green := color.New(color.FgGreen, color.Bold)
green.Printf(tui.SymbolSuccess + " successfully upgraded to %s\n", version)
errutil.Printf(green, tui.SymbolSuccess+" successfully upgraded to %s\n", version)
return nil
}
@@ -504,7 +505,7 @@ func extractBinaryFromTarGz(r io.Reader, destDir string) (string, error) {
if err != nil {
return "", err
}
defer gzr.Close()
defer errutil.Close(gzr)
tr := tar.NewReader(gzr)
@@ -534,10 +535,10 @@ func extractBinaryFromTarGz(r io.Reader, destDir string) (string, error) {
}
if _, err := io.Copy(outFile, tr); err != nil {
outFile.Close()
errutil.Close(outFile)
return "", err
}
outFile.Close()
errutil.Close(outFile)
return destPath, nil
}
@@ -551,8 +552,8 @@ func isWritable(path string) bool {
if err != nil {
return false
}
f.Close()
os.Remove(testFile)
errutil.Close(f)
errutil.Remove(testFile)
return true
}
@@ -561,13 +562,13 @@ func copyFile(src, dst string) error {
if err != nil {
return err
}
defer srcFile.Close()
defer errutil.Close(srcFile)
dstFile, err := os.Create(dst)
if err != nil {
return err
}
defer dstFile.Close()
defer errutil.Close(dstFile)
if _, err := io.Copy(dstFile, srcFile); err != nil {
return err
@@ -580,7 +581,7 @@ func removeQuarantine(path string) {
cmd := exec.Command("xattr", "-d", "com.apple.quarantine", path)
if err := cmd.Run(); err == nil {
faint := color.New(color.Faint)
faint.Println(" removed macOS quarantine attribute")
errutil.Println(faint, " removed macOS quarantine attribute")
}
}
@@ -633,7 +634,7 @@ func fetchCommitForTag(tag string) (string, error) {
if err != nil {
return "", err
}
defer resp.Body.Close()
defer errutil.Close(resp.Body)
if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("github api returned status %d", resp.StatusCode)
@@ -654,7 +655,7 @@ func compareCommits(base, head string) (*githubCompare, error) {
if err != nil {
return nil, err
}
defer resp.Body.Close()
defer errutil.Close(resp.Body)
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("github api returned status %d", resp.StatusCode)
@@ -673,16 +674,16 @@ func printNixUpgradeInstructions() {
faint := color.New(color.Faint)
cmd := color.New(color.FgCyan)
bold.Println("nix upgrade options:")
errutil.Println(bold, "nix upgrade options:")
fmt.Println()
faint.Print(" flake profile ")
cmd.Printf("nix profile install github:%s/%s\n", repoOwner, repoName)
errutil.Print(faint, " flake profile ")
errutil.Printf(cmd, "nix profile install github:%s/%s\n", repoOwner, repoName)
faint.Print(" flake update ")
cmd.Println("nix flake update snitch (in your system/home-manager config)")
errutil.Print(faint, " flake update ")
errutil.Println(cmd, "nix flake update snitch (in your system/home-manager config)")
faint.Print(" rebuild ")
cmd.Println("nixos-rebuild switch or home-manager switch")
errutil.Print(faint, " rebuild ")
errutil.Println(cmd, "nixos-rebuild switch or home-manager switch")
}

View File

@@ -6,6 +6,8 @@ import (
"github.com/fatih/color"
"github.com/spf13/cobra"
"github.com/karol-broda/snitch/internal/errutil"
)
var (
@@ -22,20 +24,20 @@ var versionCmd = &cobra.Command{
cyan := color.New(color.FgCyan)
faint := color.New(color.Faint)
bold.Print("snitch ")
cyan.Println(Version)
errutil.Print(bold, "snitch ")
errutil.Println(cyan, Version)
fmt.Println()
faint.Print(" commit ")
errutil.Print(faint, " commit ")
fmt.Println(Commit)
faint.Print(" built ")
errutil.Print(faint, " built ")
fmt.Println(Date)
faint.Print(" go ")
errutil.Print(faint, " go ")
fmt.Println(runtime.Version())
faint.Print(" os ")
errutil.Print(faint, " os ")
fmt.Printf("%s/%s\n", runtime.GOOS, runtime.GOARCH)
},
}

12
go.mod
View File

@@ -1,23 +1,26 @@
module github.com/karol-broda/snitch
go 1.24.0
go 1.25.0
require (
github.com/charmbracelet/bubbletea v1.3.6
github.com/charmbracelet/lipgloss v1.1.0
github.com/charmbracelet/x/exp/teatest v0.0.0-20251215102626-e0db08df7383
github.com/fatih/color v1.18.0
github.com/mattn/go-runewidth v0.0.16
github.com/spf13/cobra v1.9.1
github.com/spf13/viper v1.19.0
github.com/tidwall/pretty v1.2.1
golang.org/x/term v0.38.0
)
require (
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/aymanbagabas/go-udiff v0.3.1 // indirect
github.com/charmbracelet/colorprofile v0.3.2 // indirect
github.com/charmbracelet/lipgloss v1.1.0 // indirect
github.com/charmbracelet/x/ansi v0.10.1 // indirect
github.com/charmbracelet/x/cellbuf v0.0.13-0.20250311204145-2c3ea96c31dd // indirect
github.com/charmbracelet/x/exp/golden v0.0.0-20240806155701-69247e0abc2a // indirect
github.com/charmbracelet/x/exp/teatest v0.0.0-20251215102626-e0db08df7383 // indirect
github.com/charmbracelet/x/term v0.2.1 // indirect
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
@@ -28,7 +31,6 @@ require (
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-localereader v0.0.1 // indirect
github.com/mattn/go-runewidth v0.0.16 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 // indirect
github.com/muesli/cancelreader v0.2.2 // indirect
@@ -41,7 +43,6 @@ require (
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/spf13/pflag v1.0.6 // indirect
github.com/spf13/viper v1.19.0 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
go.uber.org/atomic v1.9.0 // indirect
@@ -49,7 +50,6 @@ require (
golang.org/x/exp v0.0.0-20231006140011-7918f672742d // indirect
golang.org/x/sync v0.16.0 // indirect
golang.org/x/sys v0.39.0 // indirect
golang.org/x/term v0.38.0 // indirect
golang.org/x/text v0.28.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect

29
go.sum
View File

@@ -4,14 +4,10 @@ github.com/aymanbagabas/go-udiff v0.3.1 h1:LV+qyBQ2pqe0u42ZsUEtPiCaUoqgA9gYRDs3v
github.com/aymanbagabas/go-udiff v0.3.1/go.mod h1:G0fsKmG+P6ylD0r6N/KgQD/nWzgfnl8ZBcNLgcbrw8E=
github.com/charmbracelet/bubbletea v1.3.6 h1:VkHIxPJQeDt0aFJIsVxw8BQdh/F/L2KKZGsK6et5taU=
github.com/charmbracelet/bubbletea v1.3.6/go.mod h1:oQD9VCRQFF8KplacJLo28/jofOI2ToOfGYeFgBBxHOc=
github.com/charmbracelet/colorprofile v0.2.3-0.20250311203215-f60798e515dc h1:4pZI35227imm7yK2bGPcfpFEmuY1gc2YSTShr4iJBfs=
github.com/charmbracelet/colorprofile v0.2.3-0.20250311203215-f60798e515dc/go.mod h1:X4/0JoqgTIPSFcRA/P6INZzIuyqdFY5rm8tb41s9okk=
github.com/charmbracelet/colorprofile v0.3.2 h1:9J27WdztfJQVAQKX2WOlSSRB+5gaKqqITmrvb1uTIiI=
github.com/charmbracelet/colorprofile v0.3.2/go.mod h1:mTD5XzNeWHj8oqHb+S1bssQb7vIHbepiebQ2kPKVKbI=
github.com/charmbracelet/lipgloss v1.1.0 h1:vYXsiLHVkK7fp74RkV7b2kq9+zDLoEU4MZoFqR/noCY=
github.com/charmbracelet/lipgloss v1.1.0/go.mod h1:/6Q8FR2o+kj8rz4Dq0zQc3vYf7X+B0binUUBwA0aL30=
github.com/charmbracelet/x/ansi v0.9.3 h1:BXt5DHS/MKF+LjuK4huWrC6NCvHtexww7dMayh6GXd0=
github.com/charmbracelet/x/ansi v0.9.3/go.mod h1:3RQDQ6lDnROptfpWuUVIUG64bD2g2BgntdxH0Ya5TeE=
github.com/charmbracelet/x/ansi v0.10.1 h1:rL3Koar5XvX0pHGfovN03f5cxLbCF2YvLeyz7D2jVDQ=
github.com/charmbracelet/x/ansi v0.10.1/go.mod h1:3RQDQ6lDnROptfpWuUVIUG64bD2g2BgntdxH0Ya5TeE=
github.com/charmbracelet/x/cellbuf v0.0.13-0.20250311204145-2c3ea96c31dd h1:vy0GVL4jeHEwG5YOXDmi86oYw2yuYUGqz6a8sLwg0X8=
@@ -25,16 +21,26 @@ github.com/charmbracelet/x/term v0.2.1/go.mod h1:oQ4enTYFV7QN4m0i9mzHrViD7TQKvNE
github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f h1:Y/CXytFA4m6baUTXGLOoWe4PQhGxaX0KpnayAqC48p4=
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f/go.mod h1:vw97MGsxSvLiUE2X8qFplwetxpGLQrlU1Q9AUEIzCaM=
github.com/fatih/color v1.18.0 h1:S8gINlzdQ840/4pfAwic/ZE0djQEH3wM94VfqLTZcOM=
github.com/fatih/color v1.18.0/go.mod h1:4FelSpRwEGDpQ12mAdzqdOukCy4u8WUtOY6lkT/6HfU=
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=
@@ -59,9 +65,13 @@ github.com/muesli/termenv v0.16.0/go.mod h1:ZRfOIKPFDYQoDFF4Olj7/QJbW60Ol/kL1pU3
github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM=
github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sagikazarmark/locafero v0.4.0 h1:HApY1R9zGo4DBgr7dqsTH/JJxLTTsOt7u6keLGt6kNQ=
github.com/sagikazarmark/locafero v0.4.0/go.mod h1:Pe1W6UlPYUk/+wc/6KFhbORCfqzgYEpgQ3O5fPuL3H4=
@@ -87,6 +97,7 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8=
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
@@ -98,28 +109,22 @@ go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE=
go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/multierr v1.9.0 h1:7fIwc/ZtS0q++VgcfqFDxSBZVv/Xo49/SYnDFupUwlI=
go.uber.org/multierr v1.9.0/go.mod h1:X2jQV1h+kxSjClGpnseKVIxpmcjrj7MNnI0bnlfKTVQ=
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjsbSXD66ic0XW0js0R9g=
golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k=
golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM8rJBtfilJ2qTU199MI=
golang.org/x/exp v0.0.0-20231006140011-7918f672742d/go.mod h1:ldy0pHrwJyGW56pPQzzkH36rKxoZW1tw7ZJpeKx+hdo=
golang.org/x/sync v0.15.0 h1:KWH3jNZsfyT6xfAfKiz6MRNmd46ByHDYaZ7KSkCtdW8=
golang.org/x/sync v0.15.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
golang.org/x/sync v0.16.0 h1:ycBJEhp9p4vXvUZNszeOq0kGTPghopOL8q0fq3vstxw=
golang.org/x/sync v0.16.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.33.0 h1:q3i8TbbEz+JRD9ywIRlyRAQbM0qF7hu24q3teo2hbuw=
golang.org/x/sys v0.33.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
golang.org/x/sys v0.39.0 h1:CvCKL8MeisomCi6qNZ+wbb0DN9E5AATixKsvNtMoMFk=
golang.org/x/sys v0.39.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
golang.org/x/term v0.38.0 h1:PQ5pkm/rLO6HnxFR7N2lJHOZX6Kez5Y1gDSJla6jo7Q=
golang.org/x/term v0.38.0/go.mod h1:bSEAKrOT1W+VSu9TSCMtoGEOUcKxOKgl3LE5QEF/xVg=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/text v0.28.0 h1:rhazDwis8INMIwQ4tpjLDzUhx6RlXqZNPEM0huQojng=
golang.org/x/text v0.28.0/go.mod h1:U8nCwOR8jO/marOQ0QbDiOngZVEBB7MAiitBuMjXiNU=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

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
}