update claude and readme

This commit is contained in:
2026-01-18 17:06:55 -05:00
parent 5e8e7cd41d
commit baf321ece0
2 changed files with 95 additions and 21 deletions

View File

@@ -15,9 +15,6 @@ go build ./cmd/...
go test ./...
# Run tests for a specific package
go test ./pkg/toughswitch/...
# Run a single test
go test ./pkg/toughswitch/... -run TestClient_AddDel
# Tidy module dependencies
@@ -26,17 +23,17 @@ go mod tidy
## Architecture
This repository provides Go client libraries for interacting with Ubiquiti network devices via reverse-engineered REST APIs.
This repository provides Go client libraries for interacting with Ubiquiti network devices via reverse-engineered REST APIs, plus a CLI tool.
### Package Structure
- `pkg/toughswitch/` - Client for ToughSwitch devices (e.g., TS-8-PRO)
- `pkg/edgeos/` - Client for EdgeOS devices (EdgeRouter, EdgeSwitch)
- `cmd/` - Optional CLI tool using cobra (work in progress)
- `cmd/` - CLI tool using cobra
### Client Design Pattern
### Library Client Design Pattern
Both packages follow the same multi-device client pattern:
Both `pkg/toughswitch` and `pkg/edgeos` follow the same multi-device client pattern:
1. **Client** - Top-level struct holding a map of `deviceClient` instances keyed by host
2. **deviceClient** - Per-device HTTP client with authentication state (token for ToughSwitch, cookies for EdgeOS)
@@ -45,16 +42,28 @@ Both packages follow the same multi-device client pattern:
Key characteristics:
- Thread-safe: Uses `sync.RWMutex` for device map access and `sync.Mutex` for per-device operations
- Auto-login: Automatically authenticates on 401 responses and retries the request
- Explicit login: `Login(ctx, host)` can be called to pre-authenticate
- Concurrent multi-device: `GetAll*` methods use `sync.WaitGroup.Go()` for parallel queries
### API Pattern
Each package exposes:
API pattern for each package:
- `MustNew(ctx, []Config)` - Constructor that accepts multiple device configs
- `Login(ctx, host)` - Explicit authentication (also happens automatically on 401)
- `Add(cfg)` / `Del(host)` - Dynamic device management
- `Get<Resource>(ctx, host)` - Single device query
- `GetAll<Resources>(ctx)` - Parallel query across all devices, returns `map[string]*Resource`
### CLI Architecture (`cmd/`)
The CLI uses cobra with a prerun chain pattern:
- `cmd/cmd/root.go` - Root command with `PersistentPreRunE` hook
- `cmd/cmd/prerun.go` - Prerun functions executed in order: `validateConfigFile``prepareConfig``prepareLogger``setEnvironment``prepareClients`
- `cmd/cmd/client.go` - Shared `fetchDevice()` helper that retrieves clients from context
- `cmd/internal/util/context.go` - Context helpers for config, logger, and clients
- `cmd/internal/config/config.go` - Config loading from YAML/JSON files with env overlay
Context flow: Prerun creates clients based on config and stores them in context. Commands retrieve clients via `util.ToughSwitchClientFromContext(ctx)` / `util.EdgeOSClientFromContext(ctx)`.
### Authentication
- **ToughSwitch**: Token-based via `x-auth-token` header from `/api/v1.0/user/login`