guard

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 8, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package guard implements a background daemon that detects port conflicts.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsRunning

func IsRunning() (running bool, pid int)

IsRunning checks if a guard daemon is currently running.

func LogPath

func LogPath() string

LogPath returns the default log file path.

func OpenLog

func OpenLog(path string) (*os.File, error)

OpenLog opens the log file for appending, with rotation if it exceeds maxLogSize.

func PIDPath

func PIDPath() string

PIDPath returns the path to the guard PID file.

func ReadLogTail

func ReadLogTail(path string, n int) ([]string, error)

ReadLogTail returns the last n lines of the log file.

func ReadPID

func ReadPID() (int, error)

ReadPID reads the PID from the PID file.

func RemovePID

func RemovePID() error

RemovePID removes the PID file.

func RunDaemon

func RunDaemon(ctx context.Context, s scanner.PortScanner, watchPorts []uint16, interval time.Duration, notify bool) error

RunDaemon runs the guard daemon in the foreground. It writes its PID file, sets up signal handling, and starts the watcher loop. Blocks until canceled.

func SendNotification

func SendNotification(title, message string) error

SendNotification sends a desktop notification using notify-send on Linux. Falls back to logging if notify-send is not available.

func Stop

func Stop() error

Stop sends SIGTERM to the running guard daemon.

func WritePID

func WritePID() error

WritePID writes the current process PID to the PID file.

Types

type Event

type Event struct {
	OldProcess string
	NewProcess string
	Kind       EventKind
	Port       uint16
	OldPID     int
	NewPID     int
}

Event describes a port conflict or takeover detected by the watcher.

func (Event) String

func (e Event) String() string

String returns a human-readable description of the event.

type EventKind

type EventKind int

EventKind classifies a watcher event.

const (
	EventConflict EventKind = iota // Different PID took over a watched port.
	EventNewPort                   // New port appeared that wasn't in previous scan.
	EventPortGone                  // A watched port disappeared.
)

Event kinds.

type Notifier

type Notifier struct {
	Log    io.Writer
	Notify bool
}

Notifier logs events and optionally pops a desktop notification.

func NewNotifier

func NewNotifier(log io.Writer, notify bool) *Notifier

func (*Notifier) Handle

func (n *Notifier) Handle(ev Event)

type Watcher

type Watcher struct {
	Scanner    scanner.PortScanner
	WatchPorts []uint16
	// contains filtered or unexported fields
}

Watcher polls the scanner and detects port changes between scans.

func NewWatcher

func NewWatcher(s scanner.PortScanner, watchPorts []uint16) *Watcher

NewWatcher creates a Watcher. If watchPorts is empty, all ports are watched.

func (*Watcher) Poll

func (w *Watcher) Poll(ctx context.Context) ([]Event, error)

Poll runs one scan cycle and returns any events detected since the last poll.

func (*Watcher) Run

func (w *Watcher) Run(ctx context.Context, interval time.Duration, handler func(Event)) error

Run starts the polling loop, sending events to the handler until ctx is canceled.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL