cli/internal/cmd/unifi/cmd_clients.go

113 lines
2.5 KiB
Go
Raw Normal View History

feat(release): add Homebrew tap support and fix artifact naming (#325) * feat(release): add Homebrew tap support and fix artifact naming - Fix platform naming: binaries now named core-{os}-{arch} instead of just 'core', preventing collision when artifacts merge - Add tar.gz archives for non-Windows builds (Homebrew requirement) - Add update-tap job to alpha-release workflow that auto-updates host-uk/homebrew-tap with checksums on each alpha release - Add homebrew publisher to .core/release.yaml for formal releases - Update install instructions to include brew install Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * feat(unifi): add UniFi Go SDK integration and CLI commands - Add pkg/unifi SDK wrapping unpoller/unifi with TLS, config resolution, and typed accessors for sites, clients, devices, networks, and routes - Add CLI commands: unifi sites, clients, devices, networks, routes, config - Register unifi commands in full variant build Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * fix(release): set AppVersion ldflags, git config, and tap token - Set -X pkg/cli.AppVersion in ldflags so core --version reports the correct version instead of "dev" - Add git config user.name/email in update-tap job so commit succeeds - Use HOMEBREW_TAP_TOKEN secret instead of GITHUB_TOKEN for cross-repo push to host-uk/homebrew-tap Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * fix(unifi): address CodeRabbit review feedback - Reject conflicting --wired and --wireless flags in clients command - Complete --type flag help text with bgp and ospf route types - URL-escape site name in routes API path - Wrap all command errors with log.E for contextual diagnostics - Set TLS MinVersion to 1.2 on UniFi client - Simplify redundant fmt.Sprintf in Print calls Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-05 01:37:02 +00:00
package unifi
import (
"errors"
"fmt"
"github.com/host-uk/core/pkg/cli"
"github.com/host-uk/core/pkg/log"
uf "github.com/host-uk/core/pkg/unifi"
)
// Clients command flags.
var (
clientsSite string
clientsWired bool
clientsWireless bool
)
// addClientsCommand adds the 'clients' subcommand for listing connected clients.
func addClientsCommand(parent *cli.Command) {
cmd := &cli.Command{
Use: "clients",
Short: "List connected clients",
Long: "List all connected clients on the UniFi network, optionally filtered by site or connection type.",
RunE: func(cmd *cli.Command, args []string) error {
return runClients()
},
}
cmd.Flags().StringVar(&clientsSite, "site", "", "Filter by site name")
cmd.Flags().BoolVar(&clientsWired, "wired", false, "Show only wired clients")
cmd.Flags().BoolVar(&clientsWireless, "wireless", false, "Show only wireless clients")
parent.AddCommand(cmd)
}
func runClients() error {
if clientsWired && clientsWireless {
return log.E("unifi.clients", "conflicting flags", errors.New("--wired and --wireless cannot both be set"))
}
Add User Documentation (User Guide, FAQ, Troubleshooting) (#307) * docs: add user guide, faq, and enhance troubleshooting - Created docs/user-guide.md with key concepts and workflows. - Created docs/faq.md with common questions and answers. - Enhanced docs/troubleshooting.md with AI/Agentic issues. - Updated README.md with CLI Quick Start and Getting Help sections. - Refactored mkdocs.yml to reflect actual file structure and include new docs. * docs: add user documentation and fix mkdocs navigation - Created docs/user-guide.md and docs/faq.md. - Enhanced docs/troubleshooting.md with AI/Agentic issues. - Updated README.md with CLI Quick Start and Help links. - Restored original mkdocs.yml navigation and added new user documentation sections. - Fixed formatting in pkg/io/local/client.go to ensure CI passes. * docs: add user documentation and fix auto-merge workflow - Created docs/user-guide.md and docs/faq.md with user-focused content. - Enhanced docs/troubleshooting.md with AI/Agentic issue solutions. - Updated README.md with CLI Quick Start and organized help links. - Refactored mkdocs.yml to include new documentation while preserving technical sections. - Fixed .github/workflows/auto-merge.yml by inlining the logic and adding git repository context (checkout and -R flag) to resolve CI failures. - Verified that docs/workflows.md is present in the repository. * docs: add user documentation and resolve merge conflict - Created docs/user-guide.md and docs/faq.md. - Enhanced docs/troubleshooting.md with AI/Agentic issue solutions. - Updated README.md with CLI Quick Start and Help sections. - Merged latest base branch changes and resolved conflict in .github/workflows/auto-merge.yml. - Verified and organized mkdocs.yml navigation. * docs: add user documentation and fix UniFi security issue - Created docs/user-guide.md and docs/faq.md. - Enhanced docs/troubleshooting.md. - Updated README.md with CLI Quick Start. - Fixed UniFi security vulnerability (CodeQL alert) by making TLS verification configurable. - Added --insecure flag to UniFi CLI commands. - Verified all documentation links and navigation. * docs: add user documentation and fix formatting/security - Created docs/user-guide.md and docs/faq.md. - Enhanced docs/troubleshooting.md. - Updated README.md with CLI Quick Start. - Fixed UniFi security vulnerability by making TLS verification configurable. - Added --insecure flag to UniFi CLI commands. - Fixed formatting in internal/cmd/unifi/cmd_config.go. - Verified all documentation links and navigation. --------- Co-authored-by: Claude <developers@lethean.io>
2026-02-05 06:55:52 +00:00
client, err := uf.NewFromConfig("", "", "", "", nil)
feat(release): add Homebrew tap support and fix artifact naming (#325) * feat(release): add Homebrew tap support and fix artifact naming - Fix platform naming: binaries now named core-{os}-{arch} instead of just 'core', preventing collision when artifacts merge - Add tar.gz archives for non-Windows builds (Homebrew requirement) - Add update-tap job to alpha-release workflow that auto-updates host-uk/homebrew-tap with checksums on each alpha release - Add homebrew publisher to .core/release.yaml for formal releases - Update install instructions to include brew install Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * feat(unifi): add UniFi Go SDK integration and CLI commands - Add pkg/unifi SDK wrapping unpoller/unifi with TLS, config resolution, and typed accessors for sites, clients, devices, networks, and routes - Add CLI commands: unifi sites, clients, devices, networks, routes, config - Register unifi commands in full variant build Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * fix(release): set AppVersion ldflags, git config, and tap token - Set -X pkg/cli.AppVersion in ldflags so core --version reports the correct version instead of "dev" - Add git config user.name/email in update-tap job so commit succeeds - Use HOMEBREW_TAP_TOKEN secret instead of GITHUB_TOKEN for cross-repo push to host-uk/homebrew-tap Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * fix(unifi): address CodeRabbit review feedback - Reject conflicting --wired and --wireless flags in clients command - Complete --type flag help text with bgp and ospf route types - URL-escape site name in routes API path - Wrap all command errors with log.E for contextual diagnostics - Set TLS MinVersion to 1.2 on UniFi client - Simplify redundant fmt.Sprintf in Print calls Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-05 01:37:02 +00:00
if err != nil {
return log.E("unifi.clients", "failed to initialise client", err)
}
clients, err := client.GetClients(uf.ClientFilter{
Site: clientsSite,
Wired: clientsWired,
Wireless: clientsWireless,
})
if err != nil {
return log.E("unifi.clients", "failed to fetch clients", err)
}
if len(clients) == 0 {
cli.Text("No clients found.")
return nil
}
table := cli.NewTable("Name", "IP", "MAC", "Network", "Type", "Uptime")
for _, cl := range clients {
name := cl.Name
if name == "" {
name = cl.Hostname
}
if name == "" {
name = "(unknown)"
}
connType := cl.Essid
if cl.IsWired.Val {
connType = "wired"
}
table.AddRow(
valueStyle.Render(name),
cl.IP,
dimStyle.Render(cl.Mac),
cl.Network,
dimStyle.Render(connType),
dimStyle.Render(formatUptime(cl.Uptime.Int())),
)
}
cli.Blank()
cli.Print(" %d clients\n\n", len(clients))
table.Render()
return nil
}
// formatUptime converts seconds to a human-readable duration string.
func formatUptime(seconds int) string {
if seconds <= 0 {
return "-"
}
days := seconds / 86400
hours := (seconds % 86400) / 3600
minutes := (seconds % 3600) / 60
switch {
case days > 0:
return fmt.Sprintf("%dd %dh %dm", days, hours, minutes)
case hours > 0:
return fmt.Sprintf("%dh %dm", hours, minutes)
default:
return fmt.Sprintf("%dm", minutes)
}
}