Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 227656e28e | |||
| 63ef233357 | |||
| b4f49be2c6 | |||
| 4964317b6b | |||
| baf321ece0 | |||
| 5e8e7cd41d | |||
| 868ab64bc9 | |||
| edc86feccd |
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
config.y?ml
|
||||
34
CHANGELOG.md
34
CHANGELOG.md
@@ -2,6 +2,40 @@
|
||||
|
||||
All notable changes to this project will be documented in this file.
|
||||
|
||||
## v0.5.0 - 2026-01-19
|
||||
### Added
|
||||
- `ToughSwitch` consolidated type combining Device, System, Interfaces, VLANs, Services, Statistics, and Neighbors
|
||||
- `GetToughSwitch(ctx, host)` method to fetch all data for a single ToughSwitch device in parallel
|
||||
- `GetAllToughSwitches(ctx)` method to fetch consolidated data for all ToughSwitch devices
|
||||
- `EdgeOS` consolidated type combining AuthInfo and Config
|
||||
- `GetEdgeOS(ctx, host)` method to fetch all data for a single EdgeOS device in parallel
|
||||
- `GetAllEdgeOS(ctx)` method to fetch consolidated data for all EdgeOS devices
|
||||
|
||||
### Changed
|
||||
- CLI `get device` and `get devices` commands now return full consolidated data for both device types
|
||||
- EdgeOS CLI output now includes device model, ports, PoE capability, and features from AuthInfo
|
||||
|
||||
### Fixed
|
||||
- Typo in ToughSwitch struct field name (`Statistucs` → `Statistics`)
|
||||
|
||||
## v0.4.0 - 2026-01-18
|
||||
### Added
|
||||
- CLI tool (`cmd/`) for quick device queries using cobra
|
||||
- `get device <name>` - fetch info from a single device
|
||||
- `get devices` - fetch info from all configured devices in parallel
|
||||
- YAML/JSON config file support with environment variable overlay
|
||||
- Pretty print and colorized YAML output options
|
||||
- `Login(ctx, host)` method to both toughswitch and edgeos clients for explicit pre-authentication
|
||||
- Context helpers for storing/retrieving clients (`ToughSwitchClientFromContext`, `EdgeOSClientFromContext`)
|
||||
|
||||
### Changed
|
||||
- CLI uses zerolog's built-in context methods for logger storage
|
||||
- CLI prerun creates shared clients from config and stores in context
|
||||
- Updated documentation (README.md, CLAUDE.md) with CLI usage and new Login method
|
||||
|
||||
### Fixed
|
||||
- Config default flag handling in CLI
|
||||
|
||||
## v0.3.0 - 2026-01-14
|
||||
### Changed
|
||||
- Renamed project from toughswitch to ubiquiti-clients
|
||||
|
||||
74
CLAUDE.md
Normal file
74
CLAUDE.md
Normal file
@@ -0,0 +1,74 @@
|
||||
# CLAUDE.md
|
||||
|
||||
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
|
||||
|
||||
## Build and Test Commands
|
||||
|
||||
```bash
|
||||
# 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 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, 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/` - CLI tool using cobra
|
||||
|
||||
### Library Client Design 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)
|
||||
3. **Config** - Device connection settings (host, credentials, TLS options, timeout)
|
||||
|
||||
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 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`
|
||||
- **EdgeOS**: Cookie-based from `/api/login2` endpoint
|
||||
|
||||
### Testing
|
||||
|
||||
Tests use `mockTransport` implementing `http.RoundTripper` to mock HTTP responses without network calls.
|
||||
143
README.md
143
README.md
@@ -1,6 +1,6 @@
|
||||
# ubiquiti-clients
|
||||
|
||||
Go client libraries for interacting with Ubiquiti network devices via their REST APIs.
|
||||
Go client libraries for interacting with Ubiquiti network devices via their REST APIs, plus a CLI tool.
|
||||
|
||||
**⚠️ Disclaimer: These libraries are based on reverse-engineered API calls. They are not official Ubiquiti products and are subject to change if the device firmware changes.**
|
||||
|
||||
@@ -13,8 +13,9 @@ ToughSwitch POE Pro (TS-8-PRO)) via their internal REST API.
|
||||
|
||||
#### Features
|
||||
|
||||
- **Authentication**: Handles login and session token management automatically.
|
||||
- **Authentication**: Handles login and session token management automatically (or explicitly via `Login`).
|
||||
- **Multi-Device Support**: Manage multiple devices with a single client instance.
|
||||
- **Consolidated Data**: `GetToughSwitch` fetches all device data in a single call (Device, System, Interfaces, VLANs, Services, Statistics, Neighbors).
|
||||
- **Data Retrieval**:
|
||||
- **System Information**: Hostname, uptime, firmware version, etc.
|
||||
- **Interfaces**: Status, POE settings, link speed, statistics.
|
||||
@@ -29,25 +30,79 @@ A client library for interacting with Ubiquiti EdgeOS devices (EdgeRouter, EdgeS
|
||||
|
||||
#### Features
|
||||
|
||||
- **Authentication**: Handles login and session management automatically.
|
||||
- **Authentication**: Handles login and session management automatically (or explicitly via `Login`).
|
||||
- **Multi-Device Support**: Manage multiple devices with a single client instance.
|
||||
- **Consolidated Data**: `GetEdgeOS` fetches all device data in a single call (AuthInfo and Config).
|
||||
- **Data Retrieval**:
|
||||
- **System Configuration**: Hostname, domain name, and other system settings.
|
||||
- **Interface Configuration**: Ethernet and switch interface settings, including PoE.
|
||||
- **VLAN Configuration**: VLAN assignments, PVID, and tagged VLANs.
|
||||
- **Device Information**: Model, ports, PoE capabilities, and features.
|
||||
|
||||
## Installation
|
||||
## CLI Tool
|
||||
|
||||
A command-line tool is included for quick device queries.
|
||||
|
||||
### Installation
|
||||
|
||||
```bash
|
||||
go install gitea.libretechconsulting.com/rmcguire/ubiquiti-clients/cmd@latest
|
||||
```
|
||||
|
||||
### Configuration
|
||||
|
||||
Create a config file (YAML or JSON):
|
||||
|
||||
```yaml
|
||||
logLevel: info
|
||||
logFormat: console
|
||||
|
||||
clients:
|
||||
- name: switch1
|
||||
type: toughswitch
|
||||
host: 192.168.1.1
|
||||
user: ubnt
|
||||
pass: password
|
||||
insecure: true
|
||||
timeout: 10s
|
||||
|
||||
- name: router1
|
||||
type: edgeos
|
||||
host: 192.168.1.2
|
||||
user: ubnt
|
||||
pass: password
|
||||
insecure: true
|
||||
timeout: 10s
|
||||
```
|
||||
|
||||
Environment variables can also be used (and take priority over config file):
|
||||
- `LOG_LEVEL`, `LOG_FORMAT` for top-level settings
|
||||
- `CLIENT_0_NAME`, `CLIENT_0_HOST`, `CLIENT_0_TYPE`, etc. for client array
|
||||
|
||||
### Usage
|
||||
|
||||
```bash
|
||||
# Get info from a single device
|
||||
cmd --config config.yaml get device switch1
|
||||
|
||||
# Get info from all configured devices
|
||||
cmd --config config.yaml get devices
|
||||
|
||||
# With flags
|
||||
cmd --config config.yaml get device switch1 --pretty --color
|
||||
```
|
||||
|
||||
## Library Installation
|
||||
|
||||
```bash
|
||||
# For ToughSwitch
|
||||
go get gitea.libretechconsulting.com/rmcguire/toughswitch-client/pkg/toughswitch
|
||||
go get gitea.libretechconsulting.com/rmcguire/ubiquiti-clients/pkg/toughswitch
|
||||
|
||||
# For EdgeOS
|
||||
go get gitea.libretechconsulting.com/rmcguire/toughswitch-client/pkg/edgeos
|
||||
go get gitea.libretechconsulting.com/rmcguire/ubiquiti-clients/pkg/edgeos
|
||||
```
|
||||
|
||||
## Usage
|
||||
## Library Usage
|
||||
|
||||
### ToughSwitch Basic Example
|
||||
|
||||
@@ -60,7 +115,7 @@ import (
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"gitea.libretechconsulting.com/rmcguire/toughswitch-client/pkg/toughswitch"
|
||||
"gitea.libretechconsulting.com/rmcguire/ubiquiti-clients/pkg/toughswitch"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -80,6 +135,11 @@ func main() {
|
||||
// Initialize the client
|
||||
client := toughswitch.MustNew(ctx, configs)
|
||||
|
||||
// Optionally pre-authenticate (otherwise happens automatically on first request)
|
||||
if err := client.Login(ctx, "192.168.1.1"); err != nil {
|
||||
log.Fatalf("Failed to login: %v", err)
|
||||
}
|
||||
|
||||
// Fetch system information
|
||||
deviceHost := "192.168.1.1"
|
||||
system, err := client.GetSystem(ctx, deviceHost)
|
||||
@@ -116,7 +176,7 @@ import (
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"gitea.libretechconsulting.com/rmcguire/toughswitch-client/pkg/edgeos"
|
||||
"gitea.libretechconsulting.com/rmcguire/ubiquiti-clients/pkg/edgeos"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -136,6 +196,11 @@ func main() {
|
||||
// Initialize the client
|
||||
client := edgeos.MustNew(ctx, configs)
|
||||
|
||||
// Optionally pre-authenticate (otherwise happens automatically on first request)
|
||||
if err := client.Login(ctx, "192.168.1.1"); err != nil {
|
||||
log.Fatalf("Failed to login: %v", err)
|
||||
}
|
||||
|
||||
// Fetch device information
|
||||
deviceHost := "192.168.1.1"
|
||||
authInfo, err := client.GetAuthInfo(ctx, deviceHost)
|
||||
@@ -194,6 +259,34 @@ for _, stat := range stats {
|
||||
}
|
||||
```
|
||||
|
||||
### Fetching All Device Data
|
||||
|
||||
Use the consolidated methods to fetch all device information in a single call:
|
||||
|
||||
```go
|
||||
// ToughSwitch: Get everything at once
|
||||
ts, err := client.GetToughSwitch(ctx, "192.168.1.1")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
fmt.Printf("Model: %s\n", ts.Device.Identification.Model)
|
||||
fmt.Printf("Hostname: %s\n", ts.System.Hostname)
|
||||
fmt.Printf("Interfaces: %d\n", len(ts.Interfaces))
|
||||
fmt.Printf("VLANs: %d\n", len(ts.VLANs.Vlans))
|
||||
fmt.Printf("Neighbors: %d\n", len(ts.Neighbors))
|
||||
|
||||
// EdgeOS: Get everything at once
|
||||
eos, err := edgeClient.GetEdgeOS(ctx, "192.168.2.1")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
fmt.Printf("Model: %s (%s)\n", eos.AuthInfo.ModelName, eos.AuthInfo.Model)
|
||||
fmt.Printf("Ports: %d, PoE: %v\n", eos.AuthInfo.Ports, eos.AuthInfo.PoE)
|
||||
fmt.Printf("Hostname: %s\n", eos.Config.System.HostName)
|
||||
```
|
||||
|
||||
### Working with Multiple Devices
|
||||
|
||||
Both clients are designed to handle multiple devices concurrently.
|
||||
@@ -206,14 +299,14 @@ configs := []toughswitch.Config{
|
||||
}
|
||||
client := toughswitch.MustNew(ctx, configs)
|
||||
|
||||
// Get info for all configured devices in parallel
|
||||
allSystems, err := client.GetAllSystems(ctx)
|
||||
// Get all data for all devices in parallel
|
||||
allSwitches, err := client.GetAllToughSwitches(ctx)
|
||||
if err != nil {
|
||||
log.Printf("Error fetching some systems: %v", err)
|
||||
log.Printf("Error fetching some devices: %v", err)
|
||||
}
|
||||
|
||||
for host, sys := range allSystems {
|
||||
fmt.Printf("[%s] Hostname: %s\n", host, sys.Hostname)
|
||||
for host, ts := range allSwitches {
|
||||
fmt.Printf("[%s] %s - %d interfaces\n", host, ts.System.Hostname, len(ts.Interfaces))
|
||||
}
|
||||
|
||||
// EdgeOS example
|
||||
@@ -223,14 +316,14 @@ edgeConfigs := []edgeos.Config{
|
||||
}
|
||||
edgeClient := edgeos.MustNew(ctx, edgeConfigs)
|
||||
|
||||
// Get config for all configured devices in parallel
|
||||
allConfigs, err := edgeClient.GetAllConfigs(ctx)
|
||||
// Get all data for all devices in parallel
|
||||
allEdge, err := edgeClient.GetAllEdgeOS(ctx)
|
||||
if err != nil {
|
||||
log.Printf("Error fetching some configs: %v", err)
|
||||
log.Printf("Error fetching some devices: %v", err)
|
||||
}
|
||||
|
||||
for host, cfg := range allConfigs {
|
||||
fmt.Printf("[%s] Hostname: %s\n", host, cfg.System.HostName)
|
||||
for host, eos := range allEdge {
|
||||
fmt.Printf("[%s] %s (%d ports)\n", host, eos.AuthInfo.ModelName, eos.AuthInfo.Ports)
|
||||
}
|
||||
```
|
||||
|
||||
@@ -240,26 +333,30 @@ for host, cfg := range allConfigs {
|
||||
|
||||
| Method | Description |
|
||||
|--------|-------------|
|
||||
| `Login` | Explicit authentication (also happens automatically on 401) |
|
||||
| `GetToughSwitch` | All device data combined (Device, System, Interfaces, VLANs, Services, Statistics, Neighbors) |
|
||||
| `GetDevice` | Hardware and capabilities info |
|
||||
| `GetSystem` | General system configuration and status |
|
||||
| `GetInterfaces` | Interface configuration and status |
|
||||
| `GetVLANs` | VLAN and Trunk configuration |
|
||||
| `GetServices` | State of running services (SSH, NTP, etc.) |
|
||||
| `GetStatistics` | Performance metrics |
|
||||
| `GetNeighbors` | Discovered UBNT neighbors |
|
||||
| `GetDevice` | Hardware and capabilities info |
|
||||
|
||||
All methods have corresponding `GetAll*` variants for multi-device operations.
|
||||
All `Get*` methods have corresponding `GetAll*` variants for multi-device operations.
|
||||
|
||||
### EdgeOS
|
||||
|
||||
| Method | Description |
|
||||
|--------|-------------|
|
||||
| `GetConfig` | Complete device configuration |
|
||||
| `Login` | Explicit authentication (also happens automatically on 401) |
|
||||
| `GetEdgeOS` | All device data combined (AuthInfo and Config) |
|
||||
| `GetAuthInfo` | Device authentication and feature information |
|
||||
| `GetConfig` | Complete device configuration |
|
||||
| `GetInterfaces` | Interface configuration (ethernet and switch) |
|
||||
| `GetSystem` | System configuration (hostname, domain) |
|
||||
|
||||
All methods have corresponding `GetAll*` variants for multi-device operations.
|
||||
All `Get*` methods have corresponding `GetAll*` variants for multi-device operations.
|
||||
|
||||
## License
|
||||
|
||||
|
||||
21
cmd/LICENSE
Normal file
21
cmd/LICENSE
Normal file
@@ -0,0 +1,21 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright © 2026 Ryan McGuire <ryan@mcguire.app>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
48
cmd/cmd/client.go
Normal file
48
cmd/cmd/client.go
Normal file
@@ -0,0 +1,48 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"gitea.libretechconsulting.com/rmcguire/ubiquiti-clients/cmd/internal/config"
|
||||
"gitea.libretechconsulting.com/rmcguire/ubiquiti-clients/cmd/internal/util"
|
||||
)
|
||||
|
||||
// fetchDevice retrieves device data using the pre-configured clients from context.
|
||||
// It handles both ToughSwitch and EdgeOS device types.
|
||||
func fetchDevice(ctx context.Context, clientConf *config.ClientConfig) (any, error) {
|
||||
switch clientConf.Type {
|
||||
case config.TypeToughSwitch:
|
||||
return fetchToughSwitchDevice(ctx, clientConf)
|
||||
case config.TypeEdgeOS:
|
||||
return fetchEdgeOSDevice(ctx, clientConf)
|
||||
default:
|
||||
return nil, fmt.Errorf("unknown device type: %s", clientConf.Type)
|
||||
}
|
||||
}
|
||||
|
||||
func fetchToughSwitchDevice(ctx context.Context, clientConf *config.ClientConfig) (any, error) {
|
||||
client := util.ToughSwitchClientFromContext(ctx)
|
||||
if client == nil {
|
||||
return nil, fmt.Errorf("toughswitch client not initialized")
|
||||
}
|
||||
|
||||
if err := client.Login(ctx, clientConf.Host); err != nil {
|
||||
return nil, fmt.Errorf("login failed: %w", err)
|
||||
}
|
||||
|
||||
return client.GetToughSwitch(ctx, clientConf.Host)
|
||||
}
|
||||
|
||||
func fetchEdgeOSDevice(ctx context.Context, clientConf *config.ClientConfig) (any, error) {
|
||||
client := util.EdgeOSClientFromContext(ctx)
|
||||
if client == nil {
|
||||
return nil, fmt.Errorf("edgeos client not initialized")
|
||||
}
|
||||
|
||||
if err := client.Login(ctx, clientConf.Host); err != nil {
|
||||
return nil, fmt.Errorf("login failed: %w", err)
|
||||
}
|
||||
|
||||
return client.GetEdgeOS(ctx, clientConf.Host)
|
||||
}
|
||||
21
cmd/cmd/get.go
Normal file
21
cmd/cmd/get.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"gitea.libretechconsulting.com/rmcguire/ubiquiti-clients/cmd/internal/util"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var getCmd = &cobra.Command{
|
||||
Use: "get",
|
||||
Short: "Get device information",
|
||||
Long: `Get device information from configured Ubiquiti devices.`,
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(getCmd)
|
||||
|
||||
getCmd.PersistentFlags().BoolP(util.FLAG_PRETTY, util.FLAG_PRETTY_P, false,
|
||||
"pretty print output with indentation")
|
||||
getCmd.PersistentFlags().BoolP(util.FLAG_COLOR, util.FLAG_COLOR_P, false,
|
||||
"colorize YAML output")
|
||||
}
|
||||
59
cmd/cmd/get_device.go
Normal file
59
cmd/cmd/get_device.go
Normal file
@@ -0,0 +1,59 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"gitea.libretechconsulting.com/rmcguire/ubiquiti-clients/cmd/internal/util"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var deviceCmd = &cobra.Command{
|
||||
Use: "device <name>",
|
||||
Short: "Get information from a single device",
|
||||
Long: `Get information from a single configured device by name.`,
|
||||
Args: cobra.ExactArgs(1),
|
||||
ValidArgsFunction: completeDeviceName,
|
||||
RunE: runGetDevice,
|
||||
}
|
||||
|
||||
func init() {
|
||||
getCmd.AddCommand(deviceCmd)
|
||||
}
|
||||
|
||||
func completeDeviceName(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
||||
if len(args) != 0 {
|
||||
return nil, cobra.ShellCompDirectiveNoFileComp
|
||||
}
|
||||
|
||||
conf := util.ConfigFromContext(cmd.Context())
|
||||
if conf == nil {
|
||||
return nil, cobra.ShellCompDirectiveNoFileComp
|
||||
}
|
||||
|
||||
return conf.GetClientNames(), cobra.ShellCompDirectiveNoFileComp
|
||||
}
|
||||
|
||||
func runGetDevice(cmd *cobra.Command, args []string) error {
|
||||
ctx := cmd.Context()
|
||||
conf := util.ConfigFromContext(ctx)
|
||||
logger := util.LoggerFromContext(ctx)
|
||||
|
||||
deviceName := args[0]
|
||||
clientConf := conf.GetClientByName(deviceName)
|
||||
if clientConf == nil {
|
||||
return fmt.Errorf("device %q not found in configuration", deviceName)
|
||||
}
|
||||
|
||||
pretty, _ := cmd.Flags().GetBool(util.FLAG_PRETTY)
|
||||
colorize, _ := cmd.Flags().GetBool(util.FLAG_COLOR)
|
||||
|
||||
logger.Debug().Str("device", deviceName).Str("type", string(clientConf.Type)).Msg("fetching device info")
|
||||
|
||||
result, err := fetchDevice(ctx, clientConf)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get device info: %w", err)
|
||||
}
|
||||
|
||||
return util.YAMLOutput(os.Stdout, result, pretty, colorize)
|
||||
}
|
||||
81
cmd/cmd/get_devices.go
Normal file
81
cmd/cmd/get_devices.go
Normal file
@@ -0,0 +1,81 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"sync"
|
||||
|
||||
"gitea.libretechconsulting.com/rmcguire/ubiquiti-clients/cmd/internal/config"
|
||||
"gitea.libretechconsulting.com/rmcguire/ubiquiti-clients/cmd/internal/util"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var devicesCmd = &cobra.Command{
|
||||
Use: "devices",
|
||||
Short: "Get information from all configured devices",
|
||||
Long: `Get information from all configured devices in parallel.`,
|
||||
RunE: runGetDevices,
|
||||
}
|
||||
|
||||
func init() {
|
||||
getCmd.AddCommand(devicesCmd)
|
||||
}
|
||||
|
||||
// DeviceResult holds the result of fetching a single device.
|
||||
type DeviceResult struct {
|
||||
Name string `yaml:"name"`
|
||||
Type string `yaml:"type"`
|
||||
Host string `yaml:"host"`
|
||||
Data any `yaml:"data,omitempty"`
|
||||
Error string `yaml:"error,omitempty"`
|
||||
Failed bool `yaml:"failed,omitempty"`
|
||||
}
|
||||
|
||||
func runGetDevices(cmd *cobra.Command, args []string) error {
|
||||
ctx := cmd.Context()
|
||||
conf := util.ConfigFromContext(ctx)
|
||||
logger := util.LoggerFromContext(ctx)
|
||||
|
||||
pretty, _ := cmd.Flags().GetBool(util.FLAG_PRETTY)
|
||||
colorize, _ := cmd.Flags().GetBool(util.FLAG_COLOR)
|
||||
|
||||
if len(conf.Clients) == 0 {
|
||||
return fmt.Errorf("no devices configured")
|
||||
}
|
||||
|
||||
logger.Debug().Int("count", len(conf.Clients)).Msg("fetching all devices")
|
||||
|
||||
results := make([]DeviceResult, len(conf.Clients))
|
||||
var wg sync.WaitGroup
|
||||
var mu sync.Mutex
|
||||
|
||||
for i, clientConf := range conf.Clients {
|
||||
wg.Add(1)
|
||||
go func(idx int, cc config.ClientConfig) {
|
||||
defer wg.Done()
|
||||
|
||||
result := DeviceResult{
|
||||
Name: cc.Name,
|
||||
Type: string(cc.Type),
|
||||
Host: cc.Host,
|
||||
}
|
||||
|
||||
data, err := fetchDevice(ctx, &cc)
|
||||
if err != nil {
|
||||
result.Error = err.Error()
|
||||
result.Failed = true
|
||||
logger.Error().Err(err).Str("device", cc.Name).Msg("failed to fetch device")
|
||||
} else {
|
||||
result.Data = data
|
||||
}
|
||||
|
||||
mu.Lock()
|
||||
results[idx] = result
|
||||
mu.Unlock()
|
||||
}(i, clientConf)
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
|
||||
return util.YAMLOutput(os.Stdout, results, pretty, colorize)
|
||||
}
|
||||
157
cmd/cmd/prerun.go
Normal file
157
cmd/cmd/prerun.go
Normal file
@@ -0,0 +1,157 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"gitea.libretechconsulting.com/rmcguire/ubiquiti-clients/cmd/internal/config"
|
||||
"gitea.libretechconsulting.com/rmcguire/ubiquiti-clients/cmd/internal/util"
|
||||
"gitea.libretechconsulting.com/rmcguire/ubiquiti-clients/pkg/edgeos"
|
||||
"gitea.libretechconsulting.com/rmcguire/ubiquiti-clients/pkg/toughswitch"
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// preRunFunc is a function that runs before command execution.
|
||||
type preRunFunc func(cmd *cobra.Command, args []string) error
|
||||
|
||||
// preRunFuncs is the list of functions to run before command execution.
|
||||
var preRunFuncs = []preRunFunc{
|
||||
validateConfigFile,
|
||||
prepareConfig,
|
||||
prepareLogger,
|
||||
setEnvironment,
|
||||
prepareClients,
|
||||
}
|
||||
|
||||
// preRun executes all registered pre-run functions in order.
|
||||
func preRun(cmd *cobra.Command, args []string) error {
|
||||
for _, fn := range preRunFuncs {
|
||||
if err := fn(cmd, args); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func setEnvironment(cmd *cobra.Command, _ []string) error {
|
||||
conf := util.ConfigFromContext(cmd.Context())
|
||||
if conf == nil {
|
||||
zerolog.Ctx(cmd.Context()).Fatal().Msg("unconfigured, nothing to do")
|
||||
}
|
||||
|
||||
for _, device := range conf.Clients {
|
||||
if device.Type == config.TypeToughSwitch {
|
||||
zerolog.Ctx(cmd.Context()).Debug().
|
||||
Msg("setting GODEBUG=x509negativeserial=1 for toughswitch devices")
|
||||
os.Setenv("GODEBUG", "x509negativeserial=1")
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func prepareConfig(cmd *cobra.Command, _ []string) error {
|
||||
configFile, _ := cmd.Root().PersistentFlags().GetString(util.FLAG_CONFIG_FILE)
|
||||
conf, err := config.LoadConfig(&configFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
ctx := util.ContextWithConfig(cmd.Context(), conf)
|
||||
cmd.SetContext(ctx)
|
||||
return nil
|
||||
}
|
||||
|
||||
func prepareLogger(cmd *cobra.Command, _ []string) error {
|
||||
conf := util.ConfigFromContext(cmd.Context())
|
||||
level, err := zerolog.ParseLevel(conf.LogLevel)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var writer io.Writer
|
||||
switch conf.LogFormat {
|
||||
case "console":
|
||||
writer = zerolog.NewConsoleWriter()
|
||||
case "json":
|
||||
writer = os.Stderr
|
||||
}
|
||||
|
||||
logger := zerolog.New(writer).Level(level)
|
||||
ctx := util.ContextWithLogger(cmd.Context(), &logger)
|
||||
cmd.SetContext(ctx)
|
||||
|
||||
logger.Trace().Any("config", conf).Msg("config and logging prepared")
|
||||
return nil
|
||||
}
|
||||
|
||||
// validateConfigFile checks that the config file exists if provided.
|
||||
func validateConfigFile(cmd *cobra.Command, args []string) error {
|
||||
configFile, _ := cmd.Root().PersistentFlags().GetString(util.FLAG_CONFIG_FILE)
|
||||
if configFile == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
if !strings.HasSuffix(configFile, ".yaml") && !strings.HasSuffix(configFile, ".json") {
|
||||
return fmt.Errorf("config file must end in .yaml or .json: %s", configFile)
|
||||
}
|
||||
|
||||
if _, err := os.Stat(configFile); os.IsNotExist(err) {
|
||||
return fmt.Errorf("config file does not exist: %s", configFile)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// prepareClients creates toughswitch and edgeos clients based on the configuration.
|
||||
// Clients are only created if there are devices of that type in the config.
|
||||
func prepareClients(cmd *cobra.Command, _ []string) error {
|
||||
ctx := cmd.Context()
|
||||
conf := util.ConfigFromContext(ctx)
|
||||
logger := zerolog.Ctx(ctx)
|
||||
|
||||
var tsConfigs []toughswitch.Config
|
||||
var eosConfigs []edgeos.Config
|
||||
|
||||
for _, clientConf := range conf.Clients {
|
||||
switch clientConf.Type {
|
||||
case config.TypeToughSwitch:
|
||||
tsConfigs = append(tsConfigs, toughswitch.Config{
|
||||
Host: clientConf.Host,
|
||||
Scheme: clientConf.Scheme,
|
||||
Insecure: clientConf.Insecure,
|
||||
Username: clientConf.User,
|
||||
Password: clientConf.Pass,
|
||||
Timeout: clientConf.Timeout,
|
||||
})
|
||||
case config.TypeEdgeOS:
|
||||
eosConfigs = append(eosConfigs, edgeos.Config{
|
||||
Host: clientConf.Host,
|
||||
Scheme: clientConf.Scheme,
|
||||
Insecure: clientConf.Insecure,
|
||||
Username: clientConf.User,
|
||||
Password: clientConf.Pass,
|
||||
Timeout: clientConf.Timeout,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
if len(tsConfigs) > 0 {
|
||||
logger.Debug().Int("count", len(tsConfigs)).Msg("creating toughswitch client")
|
||||
tsClient := toughswitch.MustNew(ctx, tsConfigs)
|
||||
ctx = util.ContextWithToughSwitchClient(ctx, tsClient)
|
||||
}
|
||||
|
||||
if len(eosConfigs) > 0 {
|
||||
logger.Debug().Int("count", len(eosConfigs)).Msg("creating edgeos client")
|
||||
eosClient := edgeos.MustNew(ctx, eosConfigs)
|
||||
ctx = util.ContextWithEdgeOSClient(ctx, eosClient)
|
||||
}
|
||||
|
||||
cmd.SetContext(ctx)
|
||||
return nil
|
||||
}
|
||||
68
cmd/cmd/root.go
Normal file
68
cmd/cmd/root.go
Normal file
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
Package cmd -- ubiquiti-clients test command
|
||||
|
||||
Copyright © 2026 Ryan McGuire <ryan@mcguire.app>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
*/
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
|
||||
"gitea.libretechconsulting.com/rmcguire/ubiquiti-clients/cmd/internal/util"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// rootCmd represents the base command when called without any subcommands
|
||||
var rootCmd = &cobra.Command{
|
||||
Use: "cmd",
|
||||
Short: "Simple CLI tool for using ubiquiti-clients packages",
|
||||
}
|
||||
|
||||
// Execute adds all child commands to the root command and sets flags appropriately.
|
||||
// This is called by main.main(). It only needs to happen once to the rootCmd.
|
||||
func Execute() {
|
||||
ctx, cncl := signal.NotifyContext(context.Background(), syscall.SIGTERM, syscall.SIGINT)
|
||||
defer cncl()
|
||||
|
||||
err := rootCmd.ExecuteContext(ctx)
|
||||
if err != nil {
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.PersistentFlags().StringP(util.FLAG_CONFIG_FILE, util.FLAG_CONFIG_FILE_P, "",
|
||||
"config file for ubiquiti-clients (yaml/json), env takes priority")
|
||||
|
||||
rootCmd.RegisterFlagCompletionFunc(util.FLAG_CONFIG_FILE, completeConfigFile)
|
||||
|
||||
cobra.EnableTraverseRunHooks = true
|
||||
rootCmd.PersistentPreRunE = preRun
|
||||
}
|
||||
|
||||
// completeConfigFile provides shell completion for config file flag,
|
||||
// returning only files ending in .yaml or .json.
|
||||
func completeConfigFile(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
||||
return []string{"yaml", "json"}, cobra.ShellCompDirectiveFilterFileExt
|
||||
}
|
||||
80
cmd/internal/config/config.go
Normal file
80
cmd/internal/config/config.go
Normal file
@@ -0,0 +1,80 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/caarlos0/env/v11"
|
||||
yaml "github.com/oasdiff/yaml3"
|
||||
)
|
||||
|
||||
type ClientsConfig struct {
|
||||
LogLevel string `json:"logLevel" yaml:"logLevel" env:"LOG_LEVEL" default:"warn"`
|
||||
LogFormat string `json:"logFormat" yaml:"logFormat" env:"LOG_FORMAT" default:"console"`
|
||||
|
||||
Clients []ClientConfig `json:"clients" yaml:"clients" envPrefix:"CLIENT_"`
|
||||
}
|
||||
|
||||
type ClientType string
|
||||
|
||||
const (
|
||||
TypeEdgeOS ClientType = "edgeos"
|
||||
TypeToughSwitch ClientType = "toughswitch"
|
||||
)
|
||||
|
||||
type ClientConfig struct {
|
||||
Type ClientType `json:"type" yaml:"type" env:"TYPE"`
|
||||
Name string `json:"name" yaml:"name" env:"NAME"`
|
||||
Host string `json:"host" yaml:"host" env:"HOST"`
|
||||
Scheme string `json:"scheme" yaml:"scheme" env:"SCHEME" default:"https"`
|
||||
User string `json:"user" yaml:"user" env:"USER"`
|
||||
Pass string `json:"pass" yaml:"pass" env:"PASS"`
|
||||
Insecure bool `json:"insecure" yaml:"insecure" env:"INSECURE" default:"false"`
|
||||
Timeout time.Duration `json:"timeout" yaml:"timeout" env:"TIMEOUT" default:"10s"`
|
||||
}
|
||||
|
||||
// LoadConfig will load a file if given, layering env on-top of the config
|
||||
// if present. Environment variables take the form:
|
||||
// - LOG_LEVEL, LOG_FORMAT for top-level settings
|
||||
// - CLIENT_0_NAME, CLIENT_0_HOST, CLIENT_0_TYPE, etc. for client array
|
||||
func LoadConfig(configPath *string) (*ClientsConfig, error) {
|
||||
conf, err := env.ParseAs[ClientsConfig]()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("could not parse env config: %w", err)
|
||||
}
|
||||
|
||||
if configPath != nil && *configPath != "" {
|
||||
file, err := os.Open(*configPath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("could not open config file: %w", err)
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
decoder := yaml.NewDecoder(file)
|
||||
if err := decoder.Decode(&conf); err != nil {
|
||||
return nil, fmt.Errorf("could not decode config file: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
return &conf, nil
|
||||
}
|
||||
|
||||
// GetClientByName returns a client config by its name, or nil if not found.
|
||||
func (c *ClientsConfig) GetClientByName(name string) *ClientConfig {
|
||||
for i := range c.Clients {
|
||||
if c.Clients[i].Name == name {
|
||||
return &c.Clients[i]
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetClientNames returns a list of all configured client names.
|
||||
func (c *ClientsConfig) GetClientNames() []string {
|
||||
names := make([]string, len(c.Clients))
|
||||
for i, client := range c.Clients {
|
||||
names[i] = client.Name
|
||||
}
|
||||
return names
|
||||
}
|
||||
12
cmd/internal/util/constants.go
Normal file
12
cmd/internal/util/constants.go
Normal file
@@ -0,0 +1,12 @@
|
||||
package util
|
||||
|
||||
const (
|
||||
FLAG_CONFIG_FILE = "config"
|
||||
FLAG_CONFIG_FILE_P = "f"
|
||||
|
||||
FLAG_PRETTY = "pretty"
|
||||
FLAG_PRETTY_P = "p"
|
||||
|
||||
FLAG_COLOR = "color"
|
||||
FLAG_COLOR_P = "c"
|
||||
)
|
||||
72
cmd/internal/util/context.go
Normal file
72
cmd/internal/util/context.go
Normal file
@@ -0,0 +1,72 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"gitea.libretechconsulting.com/rmcguire/ubiquiti-clients/cmd/internal/config"
|
||||
"gitea.libretechconsulting.com/rmcguire/ubiquiti-clients/pkg/edgeos"
|
||||
"gitea.libretechconsulting.com/rmcguire/ubiquiti-clients/pkg/toughswitch"
|
||||
"github.com/rs/zerolog"
|
||||
)
|
||||
|
||||
type ctxKey uint8
|
||||
|
||||
const (
|
||||
ctxConfig ctxKey = iota
|
||||
ctxToughSwitchClient
|
||||
ctxEdgeOSClient
|
||||
)
|
||||
|
||||
func ContextWithConfig(baseCtx context.Context, config *config.ClientsConfig) context.Context {
|
||||
return context.WithValue(baseCtx, ctxConfig, config)
|
||||
}
|
||||
|
||||
func ConfigFromContext(ctx context.Context) *config.ClientsConfig {
|
||||
val := ctx.Value(ctxConfig)
|
||||
conf, ok := val.(*config.ClientsConfig)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
|
||||
return conf
|
||||
}
|
||||
|
||||
// ContextWithLogger stores the logger in context using zerolog's built-in method.
|
||||
func ContextWithLogger(baseCtx context.Context, logger *zerolog.Logger) context.Context {
|
||||
return logger.WithContext(baseCtx)
|
||||
}
|
||||
|
||||
// LoggerFromContext retrieves the logger from context using zerolog's built-in method.
|
||||
func LoggerFromContext(ctx context.Context) *zerolog.Logger {
|
||||
return zerolog.Ctx(ctx)
|
||||
}
|
||||
|
||||
// ContextWithToughSwitchClient stores a toughswitch client in the context.
|
||||
func ContextWithToughSwitchClient(baseCtx context.Context, client *toughswitch.Client) context.Context {
|
||||
return context.WithValue(baseCtx, ctxToughSwitchClient, client)
|
||||
}
|
||||
|
||||
// ToughSwitchClientFromContext retrieves the toughswitch client from context.
|
||||
func ToughSwitchClientFromContext(ctx context.Context) *toughswitch.Client {
|
||||
val := ctx.Value(ctxToughSwitchClient)
|
||||
client, ok := val.(*toughswitch.Client)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
return client
|
||||
}
|
||||
|
||||
// ContextWithEdgeOSClient stores an edgeos client in the context.
|
||||
func ContextWithEdgeOSClient(baseCtx context.Context, client *edgeos.Client) context.Context {
|
||||
return context.WithValue(baseCtx, ctxEdgeOSClient, client)
|
||||
}
|
||||
|
||||
// EdgeOSClientFromContext retrieves the edgeos client from context.
|
||||
func EdgeOSClientFromContext(ctx context.Context) *edgeos.Client {
|
||||
val := ctx.Value(ctxEdgeOSClient)
|
||||
client, ok := val.(*edgeos.Client)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
return client
|
||||
}
|
||||
101
cmd/internal/util/output.go
Normal file
101
cmd/internal/util/output.go
Normal file
@@ -0,0 +1,101 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"io"
|
||||
"regexp"
|
||||
|
||||
"github.com/fatih/color"
|
||||
yaml "github.com/oasdiff/yaml3"
|
||||
)
|
||||
|
||||
// YAMLOutput writes data as YAML to the given writer.
|
||||
// If pretty is true, output is indented with 2 spaces.
|
||||
// If colorize is true, YAML syntax is colorized.
|
||||
func YAMLOutput(w io.Writer, data any, pretty, colorize bool) error {
|
||||
encoder := yaml.NewEncoder(w)
|
||||
defer encoder.Close()
|
||||
|
||||
if pretty {
|
||||
encoder.SetIndent(2)
|
||||
}
|
||||
|
||||
if !colorize {
|
||||
return encoder.Encode(data)
|
||||
}
|
||||
|
||||
// For colored output, encode to bytes first, then colorize
|
||||
var buf []byte
|
||||
bufWriter := &byteWriter{buf: &buf}
|
||||
bufEncoder := yaml.NewEncoder(bufWriter)
|
||||
if pretty {
|
||||
bufEncoder.SetIndent(2)
|
||||
}
|
||||
if err := bufEncoder.Encode(data); err != nil {
|
||||
return err
|
||||
}
|
||||
bufEncoder.Close()
|
||||
|
||||
colorized := colorizeYAML(string(buf))
|
||||
_, err := w.Write([]byte(colorized))
|
||||
return err
|
||||
}
|
||||
|
||||
type byteWriter struct {
|
||||
buf *[]byte
|
||||
}
|
||||
|
||||
func (b *byteWriter) Write(p []byte) (n int, err error) {
|
||||
*b.buf = append(*b.buf, p...)
|
||||
return len(p), nil
|
||||
}
|
||||
|
||||
// colorizeYAML applies ANSI colors to YAML output.
|
||||
func colorizeYAML(input string) string {
|
||||
// Color definitions - wrap to match ReplaceAllStringFunc signature
|
||||
keyColor := func(s string) string { return color.New(color.FgCyan).Sprint(s) }
|
||||
stringColor := func(s string) string { return color.New(color.FgGreen).Sprint(s) }
|
||||
numberColor := func(s string) string { return color.New(color.FgYellow).Sprint(s) }
|
||||
boolColor := func(s string) string { return color.New(color.FgMagenta).Sprint(s) }
|
||||
nullColor := func(s string) string { return color.New(color.FgRed).Sprint(s) }
|
||||
|
||||
// Patterns for YAML elements
|
||||
keyPattern := regexp.MustCompile(`(?m)^(\s*)([a-zA-Z_][a-zA-Z0-9_-]*)(:)`)
|
||||
stringPattern := regexp.MustCompile(`:\s*"([^"]*)"`)
|
||||
quotedStringPattern := regexp.MustCompile(`:\s*'([^']*)'`)
|
||||
numberPattern := regexp.MustCompile(`:\s*(-?\d+\.?\d*)\s*$`)
|
||||
boolPattern := regexp.MustCompile(`:\s*(true|false)\s*$`)
|
||||
nullPattern := regexp.MustCompile(`:\s*(null|~)\s*$`)
|
||||
|
||||
result := input
|
||||
|
||||
// Apply colors in order (specific patterns first)
|
||||
result = nullPattern.ReplaceAllStringFunc(result, func(s string) string {
|
||||
return regexp.MustCompile(`(null|~)`).ReplaceAllStringFunc(s, nullColor)
|
||||
})
|
||||
|
||||
result = boolPattern.ReplaceAllStringFunc(result, func(s string) string {
|
||||
return regexp.MustCompile(`(true|false)`).ReplaceAllStringFunc(s, boolColor)
|
||||
})
|
||||
|
||||
result = numberPattern.ReplaceAllStringFunc(result, func(s string) string {
|
||||
return regexp.MustCompile(`(-?\d+\.?\d*)\s*$`).ReplaceAllStringFunc(s, numberColor)
|
||||
})
|
||||
|
||||
result = stringPattern.ReplaceAllStringFunc(result, func(s string) string {
|
||||
return regexp.MustCompile(`"([^"]*)"`).ReplaceAllStringFunc(s, stringColor)
|
||||
})
|
||||
|
||||
result = quotedStringPattern.ReplaceAllStringFunc(result, func(s string) string {
|
||||
return regexp.MustCompile(`'([^']*)'`).ReplaceAllStringFunc(s, stringColor)
|
||||
})
|
||||
|
||||
result = keyPattern.ReplaceAllStringFunc(result, func(s string) string {
|
||||
matches := keyPattern.FindStringSubmatch(s)
|
||||
if len(matches) >= 4 {
|
||||
return matches[1] + keyColor(matches[2]) + matches[3]
|
||||
}
|
||||
return s
|
||||
})
|
||||
|
||||
return result
|
||||
}
|
||||
28
cmd/main.go
Normal file
28
cmd/main.go
Normal file
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
Copyright © 2026 Ryan McGuire <ryan@mcguire.app>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
*/
|
||||
package main
|
||||
|
||||
import "gitea.libretechconsulting.com/rmcguire/ubiquiti-clients/cmd/cmd"
|
||||
|
||||
func main() {
|
||||
cmd.Execute()
|
||||
}
|
||||
16
go.mod
16
go.mod
@@ -1,3 +1,19 @@
|
||||
module gitea.libretechconsulting.com/rmcguire/ubiquiti-clients
|
||||
|
||||
go 1.25.5
|
||||
|
||||
require (
|
||||
github.com/caarlos0/env/v11 v11.3.1
|
||||
github.com/fatih/color v1.18.0
|
||||
github.com/oasdiff/yaml3 v0.0.0-20250309153720-d2182401db90
|
||||
github.com/rs/zerolog v1.34.0
|
||||
github.com/spf13/cobra v1.10.2
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/inconshreveable/mousetrap v1.1.0 // indirect
|
||||
github.com/mattn/go-colorable v0.1.13 // indirect
|
||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||
github.com/spf13/pflag v1.0.9 // indirect
|
||||
golang.org/x/sys v0.25.0 // indirect
|
||||
)
|
||||
|
||||
34
go.sum
Normal file
34
go.sum
Normal file
@@ -0,0 +1,34 @@
|
||||
github.com/caarlos0/env/v11 v11.3.1 h1:cArPWC15hWmEt+gWk7YBi7lEXTXCvpaSdCiZE2X5mCA=
|
||||
github.com/caarlos0/env/v11 v11.3.1/go.mod h1:qupehSf/Y0TUTsxKywqRt/vJjN5nz6vauiYEUUr8P4U=
|
||||
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
|
||||
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/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
|
||||
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
|
||||
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
|
||||
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
|
||||
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
|
||||
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
|
||||
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
||||
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||
github.com/oasdiff/yaml3 v0.0.0-20250309153720-d2182401db90 h1:bQx3WeLcUWy+RletIKwUIt4x3t8n2SxavmoclizMb8c=
|
||||
github.com/oasdiff/yaml3 v0.0.0-20250309153720-d2182401db90/go.mod h1:y5+oSEHCPT/DGrS++Wc/479ERge0zTFxaF8PbGKcg2o=
|
||||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/rs/xid v1.6.0/go.mod h1:7XoLgs4eV+QndskICGsho+ADou8ySMSjJKDIan90Nz0=
|
||||
github.com/rs/zerolog v1.34.0 h1:k43nTLIwcTVQAncfCw4KZ2VY6ukYoZaBPNOE8txlOeY=
|
||||
github.com/rs/zerolog v1.34.0/go.mod h1:bJsvje4Z08ROH4Nhs5iH600c3IkWhwp44iRc54W6wYQ=
|
||||
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
|
||||
github.com/spf13/cobra v1.10.2 h1:DMTTonx5m65Ic0GOoRY2c16WCbHxOOw6xxezuLaBpcU=
|
||||
github.com/spf13/cobra v1.10.2/go.mod h1:7C1pvHqHw5A4vrJfjNwvOdzYu0Gml16OCs2GRiTUUS4=
|
||||
github.com/spf13/pflag v1.0.9 h1:9exaQaMOCwffKiiiYk6/BndUBv+iRViNW+4lEMi0PvY=
|
||||
github.com/spf13/pflag v1.0.9/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
||||
go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg=
|
||||
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.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34=
|
||||
golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
@@ -203,3 +203,79 @@ func (c *Client) GetAllSystems(ctx context.Context) (map[string]*SystemConfig, e
|
||||
wg.Wait()
|
||||
return results, errs
|
||||
}
|
||||
|
||||
// GetEdgeOS retrieves all information for a specific device,
|
||||
// combining AuthInfo and Config.
|
||||
func (c *Client) GetEdgeOS(ctx context.Context, host string) (*EdgeOS, error) {
|
||||
_, err := c.getDeviceByHost(host)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
eos := &EdgeOS{}
|
||||
var (
|
||||
mu sync.Mutex
|
||||
wg sync.WaitGroup
|
||||
errs error
|
||||
)
|
||||
|
||||
// Fetch auth info and config in parallel
|
||||
wg.Go(func() {
|
||||
res, err := c.GetAuthInfo(ctx, host)
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
if err != nil {
|
||||
errs = errors.Join(errs, err)
|
||||
return
|
||||
}
|
||||
eos.AuthInfo = res
|
||||
})
|
||||
|
||||
wg.Go(func() {
|
||||
res, err := c.GetConfig(ctx, host)
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
if err != nil {
|
||||
errs = errors.Join(errs, err)
|
||||
return
|
||||
}
|
||||
eos.Config = res
|
||||
})
|
||||
|
||||
wg.Wait()
|
||||
return eos, errs
|
||||
}
|
||||
|
||||
// GetAllEdgeOS retrieves all information for all devices.
|
||||
func (c *Client) GetAllEdgeOS(ctx context.Context) (map[string]*EdgeOS, error) {
|
||||
results := make(map[string]*EdgeOS)
|
||||
var (
|
||||
mu sync.Mutex
|
||||
wg sync.WaitGroup
|
||||
errs error
|
||||
)
|
||||
|
||||
c.mu.RLock()
|
||||
hosts := make([]string, 0, len(c.devices))
|
||||
for h := range c.devices {
|
||||
hosts = append(hosts, h)
|
||||
}
|
||||
c.mu.RUnlock()
|
||||
|
||||
for _, host := range hosts {
|
||||
wg.Go(func() {
|
||||
res, err := c.GetEdgeOS(ctx, host)
|
||||
if err != nil {
|
||||
mu.Lock()
|
||||
errs = errors.Join(errs, err)
|
||||
mu.Unlock()
|
||||
return
|
||||
}
|
||||
mu.Lock()
|
||||
results[host] = res
|
||||
mu.Unlock()
|
||||
})
|
||||
}
|
||||
wg.Wait()
|
||||
return results, errs
|
||||
}
|
||||
|
||||
@@ -119,6 +119,17 @@ func (c *Client) getDeviceByHost(host string) (*deviceClient, error) {
|
||||
return d, nil
|
||||
}
|
||||
|
||||
// Login authenticates with the device at the given host.
|
||||
// This is called automatically on 401 responses, but can be called explicitly
|
||||
// to pre-authenticate before making requests.
|
||||
func (c *Client) Login(ctx context.Context, host string) error {
|
||||
d, err := c.getDeviceByHost(host)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return d.login(ctx)
|
||||
}
|
||||
|
||||
func (d *deviceClient) login(ctx context.Context) error {
|
||||
d.mu.Lock()
|
||||
defer d.mu.Unlock()
|
||||
|
||||
@@ -1,5 +1,11 @@
|
||||
package edgeos
|
||||
|
||||
// EdgeOS combines all device information into one response.
|
||||
type EdgeOS struct {
|
||||
AuthInfo *AuthResponse `json:"authInfo,omitempty"`
|
||||
Config *ConfigData `json:"config,omitempty"`
|
||||
}
|
||||
|
||||
// AuthResponse represents the authentication response from login2 endpoint.
|
||||
type AuthResponse struct {
|
||||
Username string `json:"username"`
|
||||
|
||||
@@ -348,3 +348,134 @@ func (c *Client) GetAllNeighbors(ctx context.Context) (map[string][]Neighbor, er
|
||||
wg.Wait()
|
||||
return results, errs
|
||||
}
|
||||
|
||||
// GetToughSwitch retrieves all information for a specific device,
|
||||
// combining Device, System, Interfaces, VLANs, Services, Statistics, and Neighbors.
|
||||
func (c *Client) GetToughSwitch(ctx context.Context, host string) (*ToughSwitch, error) {
|
||||
_, err := c.getDeviceByHost(host)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ts := &ToughSwitch{}
|
||||
var (
|
||||
mu sync.Mutex
|
||||
wg sync.WaitGroup
|
||||
errs error
|
||||
)
|
||||
|
||||
// Fetch all resources in parallel
|
||||
wg.Go(func() {
|
||||
res, err := c.GetDevice(ctx, host)
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
if err != nil {
|
||||
errs = errors.Join(errs, err)
|
||||
return
|
||||
}
|
||||
ts.Device = res
|
||||
})
|
||||
|
||||
wg.Go(func() {
|
||||
res, err := c.GetSystem(ctx, host)
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
if err != nil {
|
||||
errs = errors.Join(errs, err)
|
||||
return
|
||||
}
|
||||
ts.System = res
|
||||
})
|
||||
|
||||
wg.Go(func() {
|
||||
res, err := c.GetInterfaces(ctx, host)
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
if err != nil {
|
||||
errs = errors.Join(errs, err)
|
||||
return
|
||||
}
|
||||
ts.Interfaces = res
|
||||
})
|
||||
|
||||
wg.Go(func() {
|
||||
res, err := c.GetVLANs(ctx, host)
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
if err != nil {
|
||||
errs = errors.Join(errs, err)
|
||||
return
|
||||
}
|
||||
ts.VLANs = res
|
||||
})
|
||||
|
||||
wg.Go(func() {
|
||||
res, err := c.GetServices(ctx, host)
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
if err != nil {
|
||||
errs = errors.Join(errs, err)
|
||||
return
|
||||
}
|
||||
ts.Services = res
|
||||
})
|
||||
|
||||
wg.Go(func() {
|
||||
res, err := c.GetStatistics(ctx, host)
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
if err != nil {
|
||||
errs = errors.Join(errs, err)
|
||||
return
|
||||
}
|
||||
ts.Statistics = res
|
||||
})
|
||||
|
||||
wg.Go(func() {
|
||||
res, err := c.GetNeighbors(ctx, host)
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
if err != nil {
|
||||
errs = errors.Join(errs, err)
|
||||
return
|
||||
}
|
||||
ts.Neighbors = res
|
||||
})
|
||||
|
||||
wg.Wait()
|
||||
return ts, errs
|
||||
}
|
||||
|
||||
// GetAllToughSwitches retrieves all information for all devices.
|
||||
func (c *Client) GetAllToughSwitches(ctx context.Context) (map[string]*ToughSwitch, error) {
|
||||
results := make(map[string]*ToughSwitch)
|
||||
var (
|
||||
mu sync.Mutex
|
||||
wg sync.WaitGroup
|
||||
errs error
|
||||
)
|
||||
|
||||
c.mu.RLock()
|
||||
hosts := make([]string, 0, len(c.devices))
|
||||
for h := range c.devices {
|
||||
hosts = append(hosts, h)
|
||||
}
|
||||
c.mu.RUnlock()
|
||||
|
||||
for _, host := range hosts {
|
||||
wg.Go(func() {
|
||||
res, err := c.GetToughSwitch(ctx, host)
|
||||
if err != nil {
|
||||
mu.Lock()
|
||||
errs = errors.Join(errs, err)
|
||||
mu.Unlock()
|
||||
return
|
||||
}
|
||||
mu.Lock()
|
||||
results[host] = res
|
||||
mu.Unlock()
|
||||
})
|
||||
}
|
||||
wg.Wait()
|
||||
return results, errs
|
||||
}
|
||||
|
||||
@@ -120,6 +120,17 @@ func (c *Client) getDeviceByHost(host string) (*deviceClient, error) {
|
||||
return d, nil
|
||||
}
|
||||
|
||||
// Login authenticates with the device at the given host.
|
||||
// This is called automatically on 401 responses, but can be called explicitly
|
||||
// to pre-authenticate before making requests.
|
||||
func (c *Client) Login(ctx context.Context, host string) error {
|
||||
d, err := c.getDeviceByHost(host)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return d.login(ctx)
|
||||
}
|
||||
|
||||
func (d *deviceClient) login(ctx context.Context) error {
|
||||
d.mu.Lock()
|
||||
defer d.mu.Unlock()
|
||||
|
||||
@@ -1,5 +1,16 @@
|
||||
package toughswitch
|
||||
|
||||
// ToughSwitch combines all types into one response.
|
||||
type ToughSwitch struct {
|
||||
Device *Device `json:"device,omitempty"`
|
||||
System *System `json:"system,omitempty"`
|
||||
Interfaces []Interface `json:"interfaces,omitempty"`
|
||||
Neighbors []Neighbor `json:"neighbors,omitempty"`
|
||||
Statistics []Statistics `json:"statistics,omitempty"`
|
||||
Services *Services `json:"services,omitempty"`
|
||||
VLANs *VLANs `json:"vlans,omitempty"`
|
||||
}
|
||||
|
||||
// LoginResponse represents the response from the login endpoint.
|
||||
type LoginResponse struct {
|
||||
StatusCode int `json:"statusCode"`
|
||||
|
||||
Reference in New Issue
Block a user