2.0 KiB
2.0 KiB
CLAUDE.md
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
Build and Test Commands
# Build library packages
go build ./pkg/...
# Build CLI tool
go build ./cmd/...
# Run all tests
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
go mod tidy
Architecture
This repository provides Go client libraries for interacting with Ubiquiti network devices via reverse-engineered REST APIs.
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)
Client Design Pattern
Both packages follow the same multi-device client pattern:
- Client - Top-level struct holding a map of
deviceClientinstances keyed by host - deviceClient - Per-device HTTP client with authentication state (token for ToughSwitch, cookies for EdgeOS)
- Config - Device connection settings (host, credentials, TLS options, timeout)
Key characteristics:
- Thread-safe: Uses
sync.RWMutexfor device map access andsync.Mutexfor per-device operations - Auto-login: Automatically authenticates on 401 responses and retries the request
- Concurrent multi-device:
GetAll*methods usesync.WaitGroup.Go()for parallel queries
API Pattern
Each package exposes:
MustNew(ctx, []Config)- Constructor that accepts multiple device configsAdd(cfg)/Del(host)- Dynamic device managementGet<Resource>(ctx, host)- Single device queryGetAll<Resources>(ctx)- Parallel query across all devices, returnsmap[string]*Resource
Authentication
- ToughSwitch: Token-based via
x-auth-tokenheader from/api/v1.0/user/login - EdgeOS: Cookie-based from
/api/login2endpoint
Testing
Tests use mockTransport implementing http.RoundTripper to mock HTTP responses without network calls.