feat: Add persistent config loading, styled help screen, and API connectivity banner to enhanced CLI

- Update NewEnhancedCLI to load persisted config from disk with fallback to defaults
- Add loadPersistedConfig helper for best-effort viper-backed config hydration
- Add Config() method to expose read-only CLI configuration
- Update showHelp to use lipgloss styling with color support and theme awareness
- Add API connectivity banner to help screen showing connection status
- Add help/version command
This commit is contained in:
nutcas3
2026-04-17 16:31:47 +03:00
parent 36e085e246
commit 978918f956
+125 -31
View File
@@ -4,8 +4,12 @@ import (
"fmt" "fmt"
"os" "os"
"github.com/charmbracelet/lipgloss"
tea "github.com/charmbracelet/bubbletea" tea "github.com/charmbracelet/bubbletea"
"github.com/nutcas3/telecom-platform/apps/cli/internal/api"
"github.com/nutcas3/telecom-platform/apps/cli/internal/commands" "github.com/nutcas3/telecom-platform/apps/cli/internal/commands"
"github.com/nutcas3/telecom-platform/apps/cli/internal/config"
"github.com/nutcas3/telecom-platform/apps/cli/internal/types" "github.com/nutcas3/telecom-platform/apps/cli/internal/types"
) )
@@ -13,20 +17,55 @@ type EnhancedCLI struct {
config *types.CLIConfig config *types.CLIConfig
} }
// NewEnhancedCLI builds the CLI with defaults and attempts to load the persisted
// configuration from disk. The returned CLI always has a valid (defaulted)
// config, even if loading failed.
func NewEnhancedCLI() *EnhancedCLI { func NewEnhancedCLI() *EnhancedCLI {
return &EnhancedCLI{ cfg := &types.CLIConfig{
config: &types.CLIConfig{ APIEndpoint: "http://localhost:8000",
APIEndpoint: "http://localhost:8000", APIToken: "",
APIToken: "", Profile: "default",
Profile: "default", Verbose: false,
Verbose: false, NoColor: false,
NoColor: false, Theme: "default",
Theme: "default",
},
} }
// Attempt to hydrate from the viper-backed configuration.
if persisted := loadPersistedConfig(); persisted != nil {
if persisted.GetAPIEndpoint() != "" {
cfg.APIEndpoint = persisted.GetAPIEndpoint()
}
if persisted.GetAPIToken() != "" {
cfg.APIToken = persisted.GetAPIToken()
}
if persisted.Profile != "" {
cfg.Profile = persisted.Profile
}
if persisted.UI.Theme != "" {
cfg.Theme = persisted.UI.Theme
}
if persisted.NoColors() {
cfg.NoColor = true
}
}
return &EnhancedCLI{config: cfg}
}
// loadPersistedConfig is a best-effort load of the CLI configuration; errors
// are swallowed so the CLI still works with sane defaults.
func loadPersistedConfig() *config.Config {
c := config.NewConfig()
if err := c.Load(); err != nil {
return c // still return defaults so callers can use GetAPIEndpoint()
}
return c
} }
func (cli *EnhancedCLI) Run(args []string) error { func (cli *EnhancedCLI) Run(args []string) error {
// Parse any global flags first so subcommands see the updated config.
_ = cli.parseConfig(args)
if len(args) < 2 { if len(args) < 2 {
cli.showHelp() cli.showHelp()
return nil return nil
@@ -54,44 +93,94 @@ func (cli *EnhancedCLI) Run(args []string) error {
return commands.HandlePluginsEnhanced(commandArgs, cli.config) return commands.HandlePluginsEnhanced(commandArgs, cli.config)
case "automation": case "automation":
return commands.HandleAutomationEnhanced(commandArgs, cli.config) return commands.HandleAutomationEnhanced(commandArgs, cli.config)
case "help", "--help", "-h":
cli.showHelp()
return nil
case "version", "--version", "-v":
fmt.Println("telecom-cli v1.0.0")
return nil
default: default:
fmt.Printf("Unknown command: %s\n", command) fmt.Printf("Unknown command: %s\n\n", command)
cli.showHelp() cli.showHelp()
return fmt.Errorf("unknown command: %s", command) return fmt.Errorf("unknown command: %s", command)
} }
} }
func (cli *EnhancedCLI) runDashboard() error { func (cli *EnhancedCLI) runDashboard() error {
p := tea.NewProgram(NewDashboard()) p := tea.NewProgram(NewDashboardWithConfig(cli.config))
if _, err := p.Run(); err != nil { if _, err := p.Run(); err != nil {
return fmt.Errorf("error running dashboard: %w", err) return fmt.Errorf("error running dashboard: %w", err)
} }
return nil return nil
} }
// showHelp renders a lipgloss-styled help screen and an API connectivity banner
// so the user immediately sees whether the CLI can reach the backend.
func (cli *EnhancedCLI) showHelp() { func (cli *EnhancedCLI) showHelp() {
fmt.Println("Telecom Platform CLI - Enhanced") useColor := !cli.config.NoColor
fmt.Println("Usage: telecom-cli <command> [options]")
headerStyle := lipgloss.NewStyle().Bold(true).Foreground(lipgloss.Color("205"))
subHeaderStyle := lipgloss.NewStyle().Bold(true).Foreground(lipgloss.Color("14"))
mutedStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("241"))
cmdStyle := lipgloss.NewStyle().Bold(true).Foreground(lipgloss.Color("12"))
render := func(s lipgloss.Style, text string) string {
if !useColor {
return text
}
return s.Render(text)
}
fmt.Println(render(headerStyle, "Telecom Platform CLI"))
fmt.Println(render(mutedStyle, "Usage: telecom-cli <command> [options]"))
fmt.Println() fmt.Println()
fmt.Println("Available commands:")
fmt.Println(" dashboard - Interactive dashboard for platform overview") // API connectivity banner
fmt.Println(" subscribers - Subscriber management") client := api.NewClient(cli.config)
fmt.Println(" services - Service management") if client.IsConnected() {
fmt.Println(" billing - Billing and invoice management") fmt.Println(render(
fmt.Println(" monitoring - Monitoring and metrics") lipgloss.NewStyle().Foreground(lipgloss.Color("46")).Bold(true),
fmt.Println(" config - Configuration management") "✓ Connected to API: "+cli.config.APIEndpoint,
fmt.Println(" deploy - Deployment management") ))
fmt.Println(" plugins - Plugin management") } else {
fmt.Println(" automation - Automation and scripting") fmt.Println(render(
lipgloss.NewStyle().Foreground(lipgloss.Color("208")).Bold(true),
"⚠ API unreachable at "+cli.config.APIEndpoint+" - commands will show sample data",
))
}
fmt.Println() fmt.Println()
fmt.Println("Global options:")
fmt.Println(" --endpoint <url> API endpoint (default: http://localhost:8000)") fmt.Println(render(subHeaderStyle, "Available commands:"))
fmt.Println(" --token <token> API authentication token") rows := [][2]string{
fmt.Println(" --profile <name> Configuration profile") {"dashboard", "Interactive dashboard for platform overview"},
fmt.Println(" --verbose Enable verbose output") {"subscribers", "Subscriber management (list, show, create, delete, ...)"},
fmt.Println(" --no-color Disable color output") {"services", "Service management (list, status, restart, logs)"},
{"billing", "Billing and invoice management"},
{"monitoring", "Monitoring, alerts, and health checks"},
{"config", "Configuration management (show, get, set, validate)"},
{"deploy", "Deployment management"},
{"plugins", "Plugin management"},
{"automation", "Automation and scripting"},
}
for _, r := range rows {
fmt.Printf(" %-14s %s\n", render(cmdStyle, r[0]), render(mutedStyle, r[1]))
}
fmt.Println() fmt.Println()
fmt.Println("Use 'telecom-cli <command> --help' for more information") fmt.Println(render(subHeaderStyle, "Global options:"))
opts := [][2]string{
{"--endpoint <url>", "API endpoint (default: http://localhost:8000)"},
{"--token <token>", "API authentication token"},
{"--profile <name>", "Configuration profile"},
{"--verbose", "Enable verbose output"},
{"--no-color", "Disable color output"},
}
for _, r := range opts {
fmt.Printf(" %-22s %s\n", render(cmdStyle, r[0]), render(mutedStyle, r[1]))
}
fmt.Println()
fmt.Println(render(mutedStyle, "Use 'telecom-cli <command>' with no args to see its subcommand help."))
} }
func (cli *EnhancedCLI) parseConfig(args []string) error { func (cli *EnhancedCLI) parseConfig(args []string) error {
@@ -121,10 +210,15 @@ func (cli *EnhancedCLI) parseConfig(args []string) error {
return nil return nil
} }
// Config returns a read-only view of the CLI configuration; primarily used by
// tests and by subcommands that need to bypass the command parser.
func (cli *EnhancedCLI) Config() *types.CLIConfig {
return cli.config
}
func Main() { func Main() {
cli := NewEnhancedCLI() cli := NewEnhancedCLI()
// Parse global options
if err := cli.parseConfig(os.Args); err != nil { if err := cli.parseConfig(os.Args); err != nil {
fmt.Fprintf(os.Stderr, "Error parsing options: %v\n", err) fmt.Fprintf(os.Stderr, "Error parsing options: %v\n", err)
os.Exit(1) os.Exit(1)