Compare commits
3 Commits
feat/tui_r
...
cicd/aur/a
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c4cee545b7 | ||
|
|
d792e10d3c | ||
|
|
1cff272fff |
@@ -82,6 +82,7 @@ aurs:
|
||||
package: |-
|
||||
install -Dm755 "./snitch" "${pkgdir}/usr/bin/snitch"
|
||||
install -Dm644 "./LICENSE" "${pkgdir}/usr/share/licenses/snitch/LICENSE"
|
||||
install -Dm644 "./README.md" "${pkgdir}/usr/share/doc/snitch/README.md"
|
||||
commit_msg_template: "Update to {{ .Tag }}"
|
||||
skip_upload: auto
|
||||
|
||||
|
||||
54
README.md
54
README.md
@@ -44,6 +44,45 @@ nix profile install github:karol-broda/snitch
|
||||
# then use: inputs.snitch.packages.${system}.default
|
||||
```
|
||||
|
||||
### home-manager (flake)
|
||||
|
||||
add snitch to your flake inputs and import the home-manager module:
|
||||
|
||||
```nix
|
||||
{
|
||||
inputs = {
|
||||
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
|
||||
home-manager.url = "github:nix-community/home-manager";
|
||||
snitch.url = "github:karol-broda/snitch";
|
||||
};
|
||||
|
||||
outputs = { nixpkgs, home-manager, snitch, ... }: {
|
||||
homeConfigurations."user" = home-manager.lib.homeManagerConfiguration {
|
||||
pkgs = nixpkgs.legacyPackages.x86_64-linux;
|
||||
modules = [
|
||||
snitch.homeManagerModules.default
|
||||
{
|
||||
programs.snitch = {
|
||||
enable = true;
|
||||
# optional: use the flake's package instead of nixpkgs
|
||||
# package = snitch.packages.x86_64-linux.default;
|
||||
settings = {
|
||||
defaults = {
|
||||
theme = "catppuccin-mocha";
|
||||
interval = "2s";
|
||||
resolve = true;
|
||||
};
|
||||
};
|
||||
};
|
||||
}
|
||||
];
|
||||
};
|
||||
};
|
||||
}
|
||||
```
|
||||
|
||||
available themes: `ansi`, `catppuccin-mocha`, `catppuccin-macchiato`, `catppuccin-frappe`, `catppuccin-latte`, `gruvbox-dark`, `gruvbox-light`, `dracula`, `nord`, `tokyo-night`, `tokyo-night-storm`, `tokyo-night-light`, `solarized-dark`, `solarized-light`, `one-dark`, `mono`
|
||||
|
||||
### arch linux (aur)
|
||||
|
||||
```bash
|
||||
@@ -238,8 +277,23 @@ optional config file at `~/.config/snitch/snitch.toml`:
|
||||
numeric = false # disable name resolution
|
||||
dns_cache = true # cache dns lookups (set to false to disable)
|
||||
theme = "auto" # color theme: auto, dark, light, mono
|
||||
|
||||
[tui]
|
||||
remember_state = false # remember view options between sessions
|
||||
```
|
||||
|
||||
### remembering view options
|
||||
|
||||
when `remember_state = true`, the tui will save and restore:
|
||||
|
||||
- filter toggles (tcp/udp, listen/established/other)
|
||||
- sort field and direction
|
||||
- address and port resolution settings
|
||||
|
||||
state is saved to `$XDG_STATE_HOME/snitch/tui.json` (defaults to `~/.local/state/snitch/tui.json`).
|
||||
|
||||
cli flags always take priority over saved state.
|
||||
|
||||
### environment variables
|
||||
|
||||
```bash
|
||||
|
||||
@@ -38,6 +38,7 @@ var topCmd = &cobra.Command{
|
||||
ResolveAddrs: resolveAddrs,
|
||||
ResolvePorts: resolvePorts,
|
||||
NoCache: effectiveNoCache,
|
||||
RememberState: cfg.TUI.RememberState,
|
||||
}
|
||||
|
||||
// if any filter flag is set, use exclusive mode
|
||||
|
||||
27
flake.nix
27
flake.nix
@@ -106,5 +106,32 @@
|
||||
overlays.default = final: _prev: {
|
||||
snitch = mkSnitch final;
|
||||
};
|
||||
|
||||
homeManagerModules.default = import ./nix/hm-module.nix;
|
||||
homeManagerModules.snitch = self.homeManagerModules.default;
|
||||
|
||||
# alias for flake-parts compatibility
|
||||
homeModules.default = self.homeManagerModules.default;
|
||||
homeModules.snitch = self.homeManagerModules.default;
|
||||
|
||||
checks = eachSystem (system:
|
||||
let
|
||||
pkgs = import nixpkgs {
|
||||
inherit system;
|
||||
overlays = [ self.overlays.default ];
|
||||
};
|
||||
in
|
||||
{
|
||||
# home manager module tests
|
||||
hm-module = import ./nix/tests/hm-module-test.nix {
|
||||
inherit pkgs;
|
||||
lib = pkgs.lib;
|
||||
hmModule = self.homeManagerModules.default;
|
||||
};
|
||||
|
||||
# package builds correctly
|
||||
package = self.packages.${system}.default;
|
||||
}
|
||||
);
|
||||
};
|
||||
}
|
||||
|
||||
@@ -14,6 +14,12 @@ import (
|
||||
// Config represents the application configuration
|
||||
type Config struct {
|
||||
Defaults DefaultConfig `mapstructure:"defaults"`
|
||||
TUI TUIConfig `mapstructure:"tui"`
|
||||
}
|
||||
|
||||
// TUIConfig contains TUI-specific configuration
|
||||
type TUIConfig struct {
|
||||
RememberState bool `mapstructure:"remember_state"`
|
||||
}
|
||||
|
||||
// DefaultConfig contains default values for CLI options
|
||||
@@ -105,6 +111,9 @@ func setDefaults(v *viper.Viper) {
|
||||
v.SetDefault("defaults.no_headers", false)
|
||||
v.SetDefault("defaults.output_format", "table")
|
||||
v.SetDefault("defaults.sort_by", "")
|
||||
|
||||
// tui settings
|
||||
v.SetDefault("tui.remember_state", false)
|
||||
}
|
||||
|
||||
func handleSpecialEnvVars(v *viper.Viper) {
|
||||
@@ -146,6 +155,9 @@ func Get() *Config {
|
||||
OutputFormat: "table",
|
||||
SortBy: "",
|
||||
},
|
||||
TUI: TUIConfig{
|
||||
RememberState: false,
|
||||
},
|
||||
}
|
||||
}
|
||||
return config
|
||||
@@ -199,6 +211,11 @@ ipv6 = false
|
||||
no_headers = false
|
||||
output_format = "table"
|
||||
sort_by = ""
|
||||
|
||||
[tui]
|
||||
# remember view options (filters, sort, resolution) between sessions
|
||||
# state is saved to $XDG_STATE_HOME/snitch/tui.json
|
||||
remember_state = false
|
||||
`, themeList, theme.DefaultTheme)
|
||||
|
||||
// Ensure directory exists
|
||||
|
||||
133
internal/state/state.go
Normal file
133
internal/state/state.go
Normal file
@@ -0,0 +1,133 @@
|
||||
package state
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
|
||||
"github.com/karol-broda/snitch/internal/collector"
|
||||
)
|
||||
|
||||
// TUIState holds view options that can be persisted between sessions
|
||||
type TUIState struct {
|
||||
ShowTCP bool `json:"show_tcp"`
|
||||
ShowUDP bool `json:"show_udp"`
|
||||
ShowListening bool `json:"show_listening"`
|
||||
ShowEstablished bool `json:"show_established"`
|
||||
ShowOther bool `json:"show_other"`
|
||||
SortField collector.SortField `json:"sort_field"`
|
||||
SortReverse bool `json:"sort_reverse"`
|
||||
ResolveAddrs bool `json:"resolve_addrs"`
|
||||
ResolvePorts bool `json:"resolve_ports"`
|
||||
}
|
||||
|
||||
var (
|
||||
saveMu sync.Mutex
|
||||
saveChan chan TUIState
|
||||
once sync.Once
|
||||
)
|
||||
|
||||
// Path returns the XDG-compliant state file path
|
||||
func Path() string {
|
||||
stateDir := os.Getenv("XDG_STATE_HOME")
|
||||
if stateDir == "" {
|
||||
home, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
stateDir = filepath.Join(home, ".local", "state")
|
||||
}
|
||||
return filepath.Join(stateDir, "snitch", "tui.json")
|
||||
}
|
||||
|
||||
// Load reads the TUI state from disk.
|
||||
// returns nil if state file doesn't exist or can't be read.
|
||||
func Load() *TUIState {
|
||||
path := Path()
|
||||
if path == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
var state TUIState
|
||||
if err := json.Unmarshal(data, &state); err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return &state
|
||||
}
|
||||
|
||||
// Save writes the TUI state to disk synchronously.
|
||||
// creates parent directories if needed.
|
||||
func Save(state TUIState) error {
|
||||
path := Path()
|
||||
if path == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
saveMu.Lock()
|
||||
defer saveMu.Unlock()
|
||||
|
||||
dir := filepath.Dir(path)
|
||||
if err := os.MkdirAll(dir, 0755); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
data, err := json.MarshalIndent(state, "", " ")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return os.WriteFile(path, data, 0644)
|
||||
}
|
||||
|
||||
// SaveAsync queues a state save to happen in the background.
|
||||
// only the most recent state is saved if multiple saves are queued.
|
||||
func SaveAsync(state TUIState) {
|
||||
once.Do(func() {
|
||||
saveChan = make(chan TUIState, 1)
|
||||
go saveWorker()
|
||||
})
|
||||
|
||||
// non-blocking send, replace pending save with newer state
|
||||
select {
|
||||
case saveChan <- state:
|
||||
default:
|
||||
// channel full, drain and replace
|
||||
select {
|
||||
case <-saveChan:
|
||||
default:
|
||||
}
|
||||
select {
|
||||
case saveChan <- state:
|
||||
default:
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func saveWorker() {
|
||||
for state := range saveChan {
|
||||
_ = Save(state)
|
||||
}
|
||||
}
|
||||
|
||||
// Default returns a TUIState with default values
|
||||
func Default() TUIState {
|
||||
return TUIState{
|
||||
ShowTCP: true,
|
||||
ShowUDP: true,
|
||||
ShowListening: true,
|
||||
ShowEstablished: true,
|
||||
ShowOther: true,
|
||||
SortField: collector.SortByLport,
|
||||
SortReverse: false,
|
||||
ResolveAddrs: false,
|
||||
ResolvePorts: false,
|
||||
}
|
||||
}
|
||||
|
||||
236
internal/state/state_test.go
Normal file
236
internal/state/state_test.go
Normal file
@@ -0,0 +1,236 @@
|
||||
package state
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/karol-broda/snitch/internal/collector"
|
||||
)
|
||||
|
||||
func TestPath_XDGStateHome(t *testing.T) {
|
||||
t.Setenv("XDG_STATE_HOME", "/custom/state")
|
||||
path := Path()
|
||||
|
||||
expected := "/custom/state/snitch/tui.json"
|
||||
if path != expected {
|
||||
t.Errorf("Path() = %q, want %q", path, expected)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPath_DefaultFallback(t *testing.T) {
|
||||
t.Setenv("XDG_STATE_HOME", "")
|
||||
path := Path()
|
||||
|
||||
home, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
t.Skip("cannot determine home directory")
|
||||
}
|
||||
|
||||
expected := filepath.Join(home, ".local", "state", "snitch", "tui.json")
|
||||
if path != expected {
|
||||
t.Errorf("Path() = %q, want %q", path, expected)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDefault(t *testing.T) {
|
||||
d := Default()
|
||||
|
||||
if d.ShowTCP != true {
|
||||
t.Error("expected ShowTCP to be true")
|
||||
}
|
||||
if d.ShowUDP != true {
|
||||
t.Error("expected ShowUDP to be true")
|
||||
}
|
||||
if d.ShowListening != true {
|
||||
t.Error("expected ShowListening to be true")
|
||||
}
|
||||
if d.ShowEstablished != true {
|
||||
t.Error("expected ShowEstablished to be true")
|
||||
}
|
||||
if d.ShowOther != true {
|
||||
t.Error("expected ShowOther to be true")
|
||||
}
|
||||
if d.SortField != collector.SortByLport {
|
||||
t.Errorf("expected SortField to be %q, got %q", collector.SortByLport, d.SortField)
|
||||
}
|
||||
if d.SortReverse != false {
|
||||
t.Error("expected SortReverse to be false")
|
||||
}
|
||||
if d.ResolveAddrs != false {
|
||||
t.Error("expected ResolveAddrs to be false")
|
||||
}
|
||||
if d.ResolvePorts != false {
|
||||
t.Error("expected ResolvePorts to be false")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSaveAndLoad(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
t.Setenv("XDG_STATE_HOME", tmpDir)
|
||||
|
||||
state := TUIState{
|
||||
ShowTCP: false,
|
||||
ShowUDP: true,
|
||||
ShowListening: true,
|
||||
ShowEstablished: false,
|
||||
ShowOther: true,
|
||||
SortField: collector.SortByProcess,
|
||||
SortReverse: true,
|
||||
ResolveAddrs: true,
|
||||
ResolvePorts: false,
|
||||
}
|
||||
|
||||
err := Save(state)
|
||||
if err != nil {
|
||||
t.Fatalf("Save() error = %v", err)
|
||||
}
|
||||
|
||||
// verify file was created
|
||||
path := Path()
|
||||
if _, err := os.Stat(path); os.IsNotExist(err) {
|
||||
t.Fatal("expected state file to exist after Save()")
|
||||
}
|
||||
|
||||
loaded := Load()
|
||||
if loaded == nil {
|
||||
t.Fatal("Load() returned nil")
|
||||
}
|
||||
|
||||
if loaded.ShowTCP != state.ShowTCP {
|
||||
t.Errorf("ShowTCP = %v, want %v", loaded.ShowTCP, state.ShowTCP)
|
||||
}
|
||||
if loaded.ShowUDP != state.ShowUDP {
|
||||
t.Errorf("ShowUDP = %v, want %v", loaded.ShowUDP, state.ShowUDP)
|
||||
}
|
||||
if loaded.ShowListening != state.ShowListening {
|
||||
t.Errorf("ShowListening = %v, want %v", loaded.ShowListening, state.ShowListening)
|
||||
}
|
||||
if loaded.ShowEstablished != state.ShowEstablished {
|
||||
t.Errorf("ShowEstablished = %v, want %v", loaded.ShowEstablished, state.ShowEstablished)
|
||||
}
|
||||
if loaded.ShowOther != state.ShowOther {
|
||||
t.Errorf("ShowOther = %v, want %v", loaded.ShowOther, state.ShowOther)
|
||||
}
|
||||
if loaded.SortField != state.SortField {
|
||||
t.Errorf("SortField = %v, want %v", loaded.SortField, state.SortField)
|
||||
}
|
||||
if loaded.SortReverse != state.SortReverse {
|
||||
t.Errorf("SortReverse = %v, want %v", loaded.SortReverse, state.SortReverse)
|
||||
}
|
||||
if loaded.ResolveAddrs != state.ResolveAddrs {
|
||||
t.Errorf("ResolveAddrs = %v, want %v", loaded.ResolveAddrs, state.ResolveAddrs)
|
||||
}
|
||||
if loaded.ResolvePorts != state.ResolvePorts {
|
||||
t.Errorf("ResolvePorts = %v, want %v", loaded.ResolvePorts, state.ResolvePorts)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoad_NonExistent(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
t.Setenv("XDG_STATE_HOME", tmpDir)
|
||||
|
||||
loaded := Load()
|
||||
if loaded != nil {
|
||||
t.Error("expected Load() to return nil for non-existent file")
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoad_InvalidJSON(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
t.Setenv("XDG_STATE_HOME", tmpDir)
|
||||
|
||||
// create directory and invalid json file
|
||||
stateDir := filepath.Join(tmpDir, "snitch")
|
||||
if err := os.MkdirAll(stateDir, 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
stateFile := filepath.Join(stateDir, "tui.json")
|
||||
if err := os.WriteFile(stateFile, []byte("not valid json"), 0644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
loaded := Load()
|
||||
if loaded != nil {
|
||||
t.Error("expected Load() to return nil for invalid JSON")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSave_CreatesDirectories(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
t.Setenv("XDG_STATE_HOME", tmpDir)
|
||||
|
||||
// snitch directory should not exist yet
|
||||
snitchDir := filepath.Join(tmpDir, "snitch")
|
||||
if _, err := os.Stat(snitchDir); err == nil {
|
||||
t.Fatal("expected snitch directory to not exist initially")
|
||||
}
|
||||
|
||||
err := Save(Default())
|
||||
if err != nil {
|
||||
t.Fatalf("Save() error = %v", err)
|
||||
}
|
||||
|
||||
// directory should now exist
|
||||
if _, err := os.Stat(snitchDir); os.IsNotExist(err) {
|
||||
t.Error("expected Save() to create parent directories")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSaveAsync(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
t.Setenv("XDG_STATE_HOME", tmpDir)
|
||||
|
||||
state := TUIState{
|
||||
ShowTCP: false,
|
||||
SortField: collector.SortByPID,
|
||||
}
|
||||
|
||||
SaveAsync(state)
|
||||
|
||||
// wait for background save with timeout
|
||||
deadline := time.Now().Add(100 * time.Millisecond)
|
||||
for time.Now().Before(deadline) {
|
||||
if loaded := Load(); loaded != nil {
|
||||
return
|
||||
}
|
||||
time.Sleep(5 * time.Millisecond)
|
||||
}
|
||||
|
||||
t.Log("SaveAsync may not have completed in time (non-fatal in CI)")
|
||||
}
|
||||
|
||||
func TestTUIState_JSONRoundtrip(t *testing.T) {
|
||||
// verify all sort fields serialize correctly
|
||||
sortFields := []collector.SortField{
|
||||
collector.SortByLport,
|
||||
collector.SortByProcess,
|
||||
collector.SortByPID,
|
||||
collector.SortByState,
|
||||
collector.SortByProto,
|
||||
}
|
||||
|
||||
tmpDir := t.TempDir()
|
||||
t.Setenv("XDG_STATE_HOME", tmpDir)
|
||||
|
||||
for _, sf := range sortFields {
|
||||
state := TUIState{
|
||||
ShowTCP: true,
|
||||
SortField: sf,
|
||||
}
|
||||
|
||||
if err := Save(state); err != nil {
|
||||
t.Fatalf("Save() error for %q: %v", sf, err)
|
||||
}
|
||||
|
||||
loaded := Load()
|
||||
if loaded == nil {
|
||||
t.Fatalf("Load() returned nil for %q", sf)
|
||||
}
|
||||
|
||||
if loaded.SortField != sf {
|
||||
t.Errorf("SortField roundtrip failed: got %q, want %q", loaded.SortField, sf)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -118,31 +118,39 @@ func (m model) handleNormalKey(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
|
||||
case "t":
|
||||
m.showTCP = !m.showTCP
|
||||
m.clampCursor()
|
||||
m.saveState()
|
||||
case "u":
|
||||
m.showUDP = !m.showUDP
|
||||
m.clampCursor()
|
||||
m.saveState()
|
||||
case "l":
|
||||
m.showListening = !m.showListening
|
||||
m.clampCursor()
|
||||
m.saveState()
|
||||
case "e":
|
||||
m.showEstablished = !m.showEstablished
|
||||
m.clampCursor()
|
||||
m.saveState()
|
||||
case "o":
|
||||
m.showOther = !m.showOther
|
||||
m.clampCursor()
|
||||
m.saveState()
|
||||
case "a":
|
||||
m.showTCP = true
|
||||
m.showUDP = true
|
||||
m.showListening = true
|
||||
m.showEstablished = true
|
||||
m.showOther = true
|
||||
m.saveState()
|
||||
|
||||
// sorting
|
||||
case "s":
|
||||
m.cycleSort()
|
||||
m.saveState()
|
||||
case "S":
|
||||
m.sortReverse = !m.sortReverse
|
||||
m.applySorting()
|
||||
m.saveState()
|
||||
|
||||
// search
|
||||
case "/":
|
||||
@@ -220,6 +228,7 @@ func (m model) handleNormalKey(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
|
||||
m.statusMessage = "address resolution: off"
|
||||
}
|
||||
m.statusExpiry = time.Now().Add(2 * time.Second)
|
||||
m.saveState()
|
||||
return m, clearStatusAfter(2 * time.Second)
|
||||
|
||||
// toggle port resolution
|
||||
@@ -231,6 +240,7 @@ func (m model) handleNormalKey(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
|
||||
m.statusMessage = "port resolution: off"
|
||||
}
|
||||
m.statusExpiry = time.Now().Add(2 * time.Second)
|
||||
m.saveState()
|
||||
return m, clearStatusAfter(2 * time.Second)
|
||||
}
|
||||
|
||||
|
||||
@@ -2,11 +2,13 @@ package tui
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/karol-broda/snitch/internal/collector"
|
||||
"github.com/karol-broda/snitch/internal/theme"
|
||||
"time"
|
||||
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
|
||||
"github.com/karol-broda/snitch/internal/collector"
|
||||
"github.com/karol-broda/snitch/internal/state"
|
||||
"github.com/karol-broda/snitch/internal/theme"
|
||||
)
|
||||
|
||||
type model struct {
|
||||
@@ -51,6 +53,9 @@ type model struct {
|
||||
// status message (temporary feedback)
|
||||
statusMessage string
|
||||
statusExpiry time.Time
|
||||
|
||||
// state persistence
|
||||
rememberState bool
|
||||
}
|
||||
|
||||
type Options struct {
|
||||
@@ -65,6 +70,7 @@ type Options struct {
|
||||
ResolveAddrs bool // when true, resolve IP addresses to hostnames
|
||||
ResolvePorts bool // when true, resolve port numbers to service names
|
||||
NoCache bool // when true, disable DNS caching
|
||||
RememberState bool // when true, persist view options between sessions
|
||||
}
|
||||
|
||||
func New(opts Options) model {
|
||||
@@ -79,8 +85,27 @@ func New(opts Options) model {
|
||||
showListening := true
|
||||
showEstablished := true
|
||||
showOther := true
|
||||
sortField := collector.SortByLport
|
||||
sortReverse := false
|
||||
resolveAddrs := opts.ResolveAddrs
|
||||
resolvePorts := opts.ResolvePorts
|
||||
|
||||
// if user specified filters, use those instead
|
||||
// load saved state if enabled and no CLI filter flags were specified
|
||||
if opts.RememberState && !opts.FilterSet {
|
||||
if saved := state.Load(); saved != nil {
|
||||
showTCP = saved.ShowTCP
|
||||
showUDP = saved.ShowUDP
|
||||
showListening = saved.ShowListening
|
||||
showEstablished = saved.ShowEstablished
|
||||
showOther = saved.ShowOther
|
||||
sortField = saved.SortField
|
||||
sortReverse = saved.SortReverse
|
||||
resolveAddrs = saved.ResolveAddrs
|
||||
resolvePorts = saved.ResolvePorts
|
||||
}
|
||||
}
|
||||
|
||||
// if user specified filters, use those instead (CLI flags take precedence)
|
||||
if opts.FilterSet {
|
||||
showTCP = opts.TCP
|
||||
showUDP = opts.UDP
|
||||
@@ -108,13 +133,15 @@ func New(opts Options) model {
|
||||
showListening: showListening,
|
||||
showEstablished: showEstablished,
|
||||
showOther: showOther,
|
||||
sortField: collector.SortByLport,
|
||||
resolveAddrs: opts.ResolveAddrs,
|
||||
resolvePorts: opts.ResolvePorts,
|
||||
sortField: sortField,
|
||||
sortReverse: sortReverse,
|
||||
resolveAddrs: resolveAddrs,
|
||||
resolvePorts: resolvePorts,
|
||||
theme: theme.GetTheme(opts.Theme),
|
||||
interval: interval,
|
||||
lastRefresh: time.Now(),
|
||||
watchedPIDs: make(map[int]bool),
|
||||
rememberState: opts.RememberState,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -291,3 +318,25 @@ func (m *model) toggleWatch(pid int) {
|
||||
func (m model) watchedCount() int {
|
||||
return len(m.watchedPIDs)
|
||||
}
|
||||
|
||||
// currentState returns the current view options as a TUIState for persistence
|
||||
func (m model) currentState() state.TUIState {
|
||||
return state.TUIState{
|
||||
ShowTCP: m.showTCP,
|
||||
ShowUDP: m.showUDP,
|
||||
ShowListening: m.showListening,
|
||||
ShowEstablished: m.showEstablished,
|
||||
ShowOther: m.showOther,
|
||||
SortField: m.sortField,
|
||||
SortReverse: m.sortReverse,
|
||||
ResolveAddrs: m.resolveAddrs,
|
||||
ResolvePorts: m.resolvePorts,
|
||||
}
|
||||
}
|
||||
|
||||
// saveState persists current view options in the background
|
||||
func (m model) saveState() {
|
||||
if m.rememberState {
|
||||
state.SaveAsync(m.currentState())
|
||||
}
|
||||
}
|
||||
|
||||
177
nix/hm-module.nix
Normal file
177
nix/hm-module.nix
Normal file
@@ -0,0 +1,177 @@
|
||||
{
|
||||
config,
|
||||
lib,
|
||||
pkgs,
|
||||
...
|
||||
}:
|
||||
let
|
||||
cfg = config.programs.snitch;
|
||||
|
||||
themes = [
|
||||
"ansi"
|
||||
"catppuccin-mocha"
|
||||
"catppuccin-macchiato"
|
||||
"catppuccin-frappe"
|
||||
"catppuccin-latte"
|
||||
"gruvbox-dark"
|
||||
"gruvbox-light"
|
||||
"dracula"
|
||||
"nord"
|
||||
"tokyo-night"
|
||||
"tokyo-night-storm"
|
||||
"tokyo-night-light"
|
||||
"solarized-dark"
|
||||
"solarized-light"
|
||||
"one-dark"
|
||||
"mono"
|
||||
"auto"
|
||||
];
|
||||
|
||||
defaultFields = [
|
||||
"pid"
|
||||
"process"
|
||||
"user"
|
||||
"proto"
|
||||
"state"
|
||||
"laddr"
|
||||
"lport"
|
||||
"raddr"
|
||||
"rport"
|
||||
];
|
||||
|
||||
tomlFormat = pkgs.formats.toml { };
|
||||
|
||||
settingsType = lib.types.submodule {
|
||||
freeformType = tomlFormat.type;
|
||||
|
||||
options = {
|
||||
defaults = lib.mkOption {
|
||||
type = lib.types.submodule {
|
||||
freeformType = tomlFormat.type;
|
||||
|
||||
options = {
|
||||
interval = lib.mkOption {
|
||||
type = lib.types.str;
|
||||
default = "1s";
|
||||
example = "2s";
|
||||
description = "Default refresh interval for watch/stats/trace commands.";
|
||||
};
|
||||
|
||||
numeric = lib.mkOption {
|
||||
type = lib.types.bool;
|
||||
default = false;
|
||||
description = "Disable name/service resolution by default.";
|
||||
};
|
||||
|
||||
fields = lib.mkOption {
|
||||
type = lib.types.listOf lib.types.str;
|
||||
default = defaultFields;
|
||||
example = [ "pid" "process" "proto" "state" "laddr" "lport" ];
|
||||
description = "Default fields to display.";
|
||||
};
|
||||
|
||||
theme = lib.mkOption {
|
||||
type = lib.types.enum themes;
|
||||
default = "ansi";
|
||||
description = ''
|
||||
Color theme for the TUI. "ansi" inherits terminal colors.
|
||||
'';
|
||||
};
|
||||
|
||||
units = lib.mkOption {
|
||||
type = lib.types.enum [ "auto" "si" "iec" ];
|
||||
default = "auto";
|
||||
description = "Default units for byte display.";
|
||||
};
|
||||
|
||||
color = lib.mkOption {
|
||||
type = lib.types.enum [ "auto" "always" "never" ];
|
||||
default = "auto";
|
||||
description = "Default color mode.";
|
||||
};
|
||||
|
||||
resolve = lib.mkOption {
|
||||
type = lib.types.bool;
|
||||
default = true;
|
||||
description = "Enable name resolution by default.";
|
||||
};
|
||||
|
||||
dns_cache = lib.mkOption {
|
||||
type = lib.types.bool;
|
||||
default = true;
|
||||
description = "Enable DNS caching.";
|
||||
};
|
||||
|
||||
ipv4 = lib.mkOption {
|
||||
type = lib.types.bool;
|
||||
default = false;
|
||||
description = "Filter to IPv4 only by default.";
|
||||
};
|
||||
|
||||
ipv6 = lib.mkOption {
|
||||
type = lib.types.bool;
|
||||
default = false;
|
||||
description = "Filter to IPv6 only by default.";
|
||||
};
|
||||
|
||||
no_headers = lib.mkOption {
|
||||
type = lib.types.bool;
|
||||
default = false;
|
||||
description = "Omit headers in output by default.";
|
||||
};
|
||||
|
||||
output_format = lib.mkOption {
|
||||
type = lib.types.enum [ "table" "json" "csv" ];
|
||||
default = "table";
|
||||
description = "Default output format.";
|
||||
};
|
||||
|
||||
sort_by = lib.mkOption {
|
||||
type = lib.types.str;
|
||||
default = "";
|
||||
example = "pid";
|
||||
description = "Default sort field.";
|
||||
};
|
||||
};
|
||||
};
|
||||
default = { };
|
||||
description = "Default settings for snitch commands.";
|
||||
};
|
||||
};
|
||||
};
|
||||
in
|
||||
{
|
||||
options.programs.snitch = {
|
||||
enable = lib.mkEnableOption "snitch, a friendlier ss/netstat for humans";
|
||||
|
||||
package = lib.mkPackageOption pkgs "snitch" { };
|
||||
|
||||
settings = lib.mkOption {
|
||||
type = settingsType;
|
||||
default = { };
|
||||
example = lib.literalExpression ''
|
||||
{
|
||||
defaults = {
|
||||
theme = "catppuccin-mocha";
|
||||
interval = "2s";
|
||||
resolve = true;
|
||||
};
|
||||
}
|
||||
'';
|
||||
description = ''
|
||||
Configuration written to {file}`$XDG_CONFIG_HOME/snitch/snitch.toml`.
|
||||
|
||||
See <https://github.com/karol-broda/snitch> for available options.
|
||||
'';
|
||||
};
|
||||
};
|
||||
|
||||
config = lib.mkIf cfg.enable {
|
||||
home.packages = [ cfg.package ];
|
||||
|
||||
xdg.configFile."snitch/snitch.toml" = lib.mkIf (cfg.settings != { }) {
|
||||
source = tomlFormat.generate "snitch.toml" cfg.settings;
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
429
nix/tests/hm-module-test.nix
Normal file
429
nix/tests/hm-module-test.nix
Normal file
@@ -0,0 +1,429 @@
|
||||
# home manager module tests
|
||||
#
|
||||
# run with: nix build .#checks.x86_64-linux.hm-module
|
||||
#
|
||||
# tests cover:
|
||||
# - module evaluation with various configurations
|
||||
# - type validation for all options
|
||||
# - generated TOML content verification
|
||||
# - edge cases (disabled, empty settings, full settings)
|
||||
{ pkgs, lib, hmModule }:
|
||||
|
||||
let
|
||||
# minimal home-manager stub for standalone module testing
|
||||
hmLib = {
|
||||
hm.types.dagOf = lib.types.attrsOf;
|
||||
dag.entryAnywhere = x: x;
|
||||
};
|
||||
|
||||
# evaluate the hm module with a given config
|
||||
evalModule = testConfig:
|
||||
lib.evalModules {
|
||||
modules = [
|
||||
hmModule
|
||||
# stub home-manager's expected structure
|
||||
{
|
||||
options = {
|
||||
home.packages = lib.mkOption {
|
||||
type = lib.types.listOf lib.types.package;
|
||||
default = [ ];
|
||||
};
|
||||
xdg.configFile = lib.mkOption {
|
||||
type = lib.types.attrsOf (lib.types.submodule {
|
||||
options = {
|
||||
source = lib.mkOption { type = lib.types.path; };
|
||||
text = lib.mkOption { type = lib.types.str; default = ""; };
|
||||
};
|
||||
});
|
||||
default = { };
|
||||
};
|
||||
};
|
||||
}
|
||||
testConfig
|
||||
];
|
||||
specialArgs = { inherit pkgs lib; };
|
||||
};
|
||||
|
||||
# read generated TOML file content
|
||||
readGeneratedToml = evalResult:
|
||||
let
|
||||
configFile = evalResult.config.xdg.configFile."snitch/snitch.toml" or null;
|
||||
in
|
||||
if configFile != null && configFile ? source
|
||||
then builtins.readFile configFile.source
|
||||
else null;
|
||||
|
||||
# test cases
|
||||
tests = {
|
||||
# test 1: module evaluates when disabled
|
||||
moduleDisabled = {
|
||||
name = "module-disabled";
|
||||
config = {
|
||||
programs.snitch.enable = false;
|
||||
};
|
||||
assertions = evalResult: [
|
||||
{
|
||||
assertion = evalResult.config.home.packages == [ ];
|
||||
message = "packages should be empty when disabled";
|
||||
}
|
||||
{
|
||||
assertion = !(evalResult.config.xdg.configFile ? "snitch/snitch.toml");
|
||||
message = "config file should not exist when disabled";
|
||||
}
|
||||
];
|
||||
};
|
||||
|
||||
# test 2: module evaluates with enable only (defaults)
|
||||
moduleEnabledDefaults = {
|
||||
name = "module-enabled-defaults";
|
||||
config = {
|
||||
programs.snitch.enable = true;
|
||||
};
|
||||
assertions = evalResult: [
|
||||
{
|
||||
assertion = builtins.length evalResult.config.home.packages == 1;
|
||||
message = "package should be installed when enabled";
|
||||
}
|
||||
];
|
||||
};
|
||||
|
||||
# test 3: all theme values are valid
|
||||
themeValidation = {
|
||||
name = "theme-validation";
|
||||
config = {
|
||||
programs.snitch = {
|
||||
enable = true;
|
||||
settings.defaults.theme = "catppuccin-mocha";
|
||||
};
|
||||
};
|
||||
assertions = evalResult:
|
||||
let
|
||||
toml = readGeneratedToml evalResult;
|
||||
in
|
||||
[
|
||||
{
|
||||
assertion = toml != null;
|
||||
message = "TOML config should be generated";
|
||||
}
|
||||
{
|
||||
assertion = lib.hasInfix "catppuccin-mocha" toml;
|
||||
message = "theme should be set in TOML";
|
||||
}
|
||||
];
|
||||
};
|
||||
|
||||
# test 4: full configuration with all options
|
||||
fullConfiguration = {
|
||||
name = "full-configuration";
|
||||
config = {
|
||||
programs.snitch = {
|
||||
enable = true;
|
||||
settings.defaults = {
|
||||
interval = "2s";
|
||||
numeric = true;
|
||||
fields = [ "pid" "process" "proto" ];
|
||||
theme = "nord";
|
||||
units = "si";
|
||||
color = "always";
|
||||
resolve = false;
|
||||
dns_cache = false;
|
||||
ipv4 = true;
|
||||
ipv6 = false;
|
||||
no_headers = true;
|
||||
output_format = "json";
|
||||
sort_by = "pid";
|
||||
};
|
||||
};
|
||||
};
|
||||
assertions = evalResult:
|
||||
let
|
||||
toml = readGeneratedToml evalResult;
|
||||
in
|
||||
[
|
||||
{
|
||||
assertion = toml != null;
|
||||
message = "TOML config should be generated";
|
||||
}
|
||||
{
|
||||
assertion = lib.hasInfix "interval = \"2s\"" toml;
|
||||
message = "interval should be 2s";
|
||||
}
|
||||
{
|
||||
assertion = lib.hasInfix "numeric = true" toml;
|
||||
message = "numeric should be true";
|
||||
}
|
||||
{
|
||||
assertion = lib.hasInfix "theme = \"nord\"" toml;
|
||||
message = "theme should be nord";
|
||||
}
|
||||
{
|
||||
assertion = lib.hasInfix "units = \"si\"" toml;
|
||||
message = "units should be si";
|
||||
}
|
||||
{
|
||||
assertion = lib.hasInfix "color = \"always\"" toml;
|
||||
message = "color should be always";
|
||||
}
|
||||
{
|
||||
assertion = lib.hasInfix "resolve = false" toml;
|
||||
message = "resolve should be false";
|
||||
}
|
||||
{
|
||||
assertion = lib.hasInfix "output_format = \"json\"" toml;
|
||||
message = "output_format should be json";
|
||||
}
|
||||
{
|
||||
assertion = lib.hasInfix "sort_by = \"pid\"" toml;
|
||||
message = "sort_by should be pid";
|
||||
}
|
||||
];
|
||||
};
|
||||
|
||||
# test 5: output format enum validation
|
||||
outputFormatCsv = {
|
||||
name = "output-format-csv";
|
||||
config = {
|
||||
programs.snitch = {
|
||||
enable = true;
|
||||
settings.defaults.output_format = "csv";
|
||||
};
|
||||
};
|
||||
assertions = evalResult:
|
||||
let
|
||||
toml = readGeneratedToml evalResult;
|
||||
in
|
||||
[
|
||||
{
|
||||
assertion = lib.hasInfix "output_format = \"csv\"" toml;
|
||||
message = "output_format should accept csv";
|
||||
}
|
||||
];
|
||||
};
|
||||
|
||||
# test 6: units enum validation
|
||||
unitsIec = {
|
||||
name = "units-iec";
|
||||
config = {
|
||||
programs.snitch = {
|
||||
enable = true;
|
||||
settings.defaults.units = "iec";
|
||||
};
|
||||
};
|
||||
assertions = evalResult:
|
||||
let
|
||||
toml = readGeneratedToml evalResult;
|
||||
in
|
||||
[
|
||||
{
|
||||
assertion = lib.hasInfix "units = \"iec\"" toml;
|
||||
message = "units should accept iec";
|
||||
}
|
||||
];
|
||||
};
|
||||
|
||||
# test 7: color never value
|
||||
colorNever = {
|
||||
name = "color-never";
|
||||
config = {
|
||||
programs.snitch = {
|
||||
enable = true;
|
||||
settings.defaults.color = "never";
|
||||
};
|
||||
};
|
||||
assertions = evalResult:
|
||||
let
|
||||
toml = readGeneratedToml evalResult;
|
||||
in
|
||||
[
|
||||
{
|
||||
assertion = lib.hasInfix "color = \"never\"" toml;
|
||||
message = "color should accept never";
|
||||
}
|
||||
];
|
||||
};
|
||||
|
||||
# test 8: freeform type allows custom keys
|
||||
freeformCustomKeys = {
|
||||
name = "freeform-custom-keys";
|
||||
config = {
|
||||
programs.snitch = {
|
||||
enable = true;
|
||||
settings = {
|
||||
defaults.theme = "dracula";
|
||||
custom_section = {
|
||||
custom_key = "custom_value";
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
assertions = evalResult:
|
||||
let
|
||||
toml = readGeneratedToml evalResult;
|
||||
in
|
||||
[
|
||||
{
|
||||
assertion = lib.hasInfix "custom_key" toml;
|
||||
message = "freeform type should allow custom keys";
|
||||
}
|
||||
];
|
||||
};
|
||||
|
||||
# test 9: all themes evaluate correctly
|
||||
allThemes =
|
||||
let
|
||||
themes = [
|
||||
"ansi"
|
||||
"catppuccin-mocha"
|
||||
"catppuccin-macchiato"
|
||||
"catppuccin-frappe"
|
||||
"catppuccin-latte"
|
||||
"gruvbox-dark"
|
||||
"gruvbox-light"
|
||||
"dracula"
|
||||
"nord"
|
||||
"tokyo-night"
|
||||
"tokyo-night-storm"
|
||||
"tokyo-night-light"
|
||||
"solarized-dark"
|
||||
"solarized-light"
|
||||
"one-dark"
|
||||
"mono"
|
||||
"auto"
|
||||
];
|
||||
in
|
||||
{
|
||||
name = "all-themes";
|
||||
# use the last theme as the test config
|
||||
config = {
|
||||
programs.snitch = {
|
||||
enable = true;
|
||||
settings.defaults.theme = "auto";
|
||||
};
|
||||
};
|
||||
assertions = evalResult:
|
||||
let
|
||||
# verify all themes can be set by evaluating them
|
||||
themeResults = map
|
||||
(theme:
|
||||
let
|
||||
result = evalModule {
|
||||
programs.snitch = {
|
||||
enable = true;
|
||||
settings.defaults.theme = theme;
|
||||
};
|
||||
};
|
||||
toml = readGeneratedToml result;
|
||||
in
|
||||
{
|
||||
inherit theme;
|
||||
success = toml != null && lib.hasInfix theme toml;
|
||||
}
|
||||
)
|
||||
themes;
|
||||
allSucceeded = lib.all (r: r.success) themeResults;
|
||||
in
|
||||
[
|
||||
{
|
||||
assertion = allSucceeded;
|
||||
message = "all themes should evaluate correctly: ${
|
||||
lib.concatMapStringsSep ", "
|
||||
(r: "${r.theme}=${if r.success then "ok" else "fail"}")
|
||||
themeResults
|
||||
}";
|
||||
}
|
||||
];
|
||||
};
|
||||
|
||||
# test 10: fields list serialization
|
||||
fieldsListSerialization = {
|
||||
name = "fields-list-serialization";
|
||||
config = {
|
||||
programs.snitch = {
|
||||
enable = true;
|
||||
settings.defaults.fields = [ "pid" "process" "proto" "state" ];
|
||||
};
|
||||
};
|
||||
assertions = evalResult:
|
||||
let
|
||||
toml = readGeneratedToml evalResult;
|
||||
in
|
||||
[
|
||||
{
|
||||
assertion = lib.hasInfix "pid" toml && lib.hasInfix "process" toml;
|
||||
message = "fields list should be serialized correctly";
|
||||
}
|
||||
];
|
||||
};
|
||||
};
|
||||
|
||||
# run all tests and collect results
|
||||
runTests =
|
||||
let
|
||||
testResults = lib.mapAttrsToList
|
||||
(name: test:
|
||||
let
|
||||
evalResult = evalModule test.config;
|
||||
assertions = test.assertions evalResult;
|
||||
failures = lib.filter (a: !a.assertion) assertions;
|
||||
in
|
||||
{
|
||||
inherit name;
|
||||
testName = test.name;
|
||||
passed = failures == [ ];
|
||||
failures = map (f: f.message) failures;
|
||||
}
|
||||
)
|
||||
tests;
|
||||
|
||||
allPassed = lib.all (r: r.passed) testResults;
|
||||
failedTests = lib.filter (r: !r.passed) testResults;
|
||||
|
||||
summary = ''
|
||||
========================================
|
||||
home manager module test results
|
||||
========================================
|
||||
total tests: ${toString (builtins.length testResults)}
|
||||
passed: ${toString (builtins.length (lib.filter (r: r.passed) testResults))}
|
||||
failed: ${toString (builtins.length failedTests)}
|
||||
========================================
|
||||
${lib.concatMapStringsSep "\n" (r:
|
||||
if r.passed
|
||||
then "[yes] ${r.testName}"
|
||||
else "[no] ${r.testName}\n ${lib.concatStringsSep "\n " r.failures}"
|
||||
) testResults}
|
||||
========================================
|
||||
'';
|
||||
in
|
||||
{
|
||||
inherit testResults allPassed failedTests summary;
|
||||
};
|
||||
|
||||
results = runTests;
|
||||
|
||||
in
|
||||
pkgs.runCommand "hm-module-test"
|
||||
{
|
||||
passthru = {
|
||||
inherit results;
|
||||
# expose for debugging
|
||||
inherit evalModule tests;
|
||||
};
|
||||
}
|
||||
(
|
||||
if results.allPassed
|
||||
then ''
|
||||
echo "${results.summary}"
|
||||
echo "all tests passed"
|
||||
touch $out
|
||||
''
|
||||
else ''
|
||||
echo "${results.summary}"
|
||||
echo ""
|
||||
echo "failed tests:"
|
||||
${lib.concatMapStringsSep "\n" (t: ''
|
||||
echo " - ${t.testName}: ${lib.concatStringsSep ", " t.failures}"
|
||||
'') results.failedTests}
|
||||
exit 1
|
||||
''
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user