refactor: Migrate billing and config commands to uiContext with error handling and simplified output

- Update HandleBilling to accept CLIConfig, return error, and use uiContext for all output
- Update HandleConfig to accept CLIConfig, return error, and use uiContext for all output
- Replace manual colorizer/icon initialization with uiContext helper methods
- Simplify error handling with uiContext.errorln, warn, success, info, muted methods
- Replace direct table creation with uiContext.newTable()
This commit is contained in:
nutcas3
2026-04-17 16:26:43 +03:00
parent 5e70eac501
commit a32b933fbf
9 changed files with 904 additions and 1173 deletions
+123 -56
View File
@@ -2,92 +2,159 @@ package commands
import (
"fmt"
"strings"
"github.com/nutcas3/telecom-platform/apps/cli/internal/types"
)
func HandleServices(args []string) {
// HandleServices is the entry point for service commands.
func HandleServices(args []string, config *types.CLIConfig) error {
u := newUIContext(config)
if len(args) == 0 {
showServicesHelp()
return
showServicesHelp(u)
return nil
}
u.connectivityBanner()
command := args[0]
switch command {
case "list":
listServices()
return listServices(u)
case "status":
showServiceStatus(args[1:])
return showServiceStatus(u, args[1:])
case "restart":
restartService(args[1:])
return restartService(u, args[1:])
case "logs":
showServiceLogs(args[1:])
return showServiceLogs(u, args[1:])
default:
fmt.Printf("Unknown services command: %s\n", command)
showServicesHelp()
u.errorln("Unknown services command: " + command)
showServicesHelp(u)
return fmt.Errorf("unknown command: %s", command)
}
}
func showServicesHelp() {
fmt.Println("Service Management")
fmt.Println("Usage: telecom-cli services <command> [options]")
fmt.Println("\nAvailable commands:")
fmt.Println(" list - List all services")
fmt.Println(" status <service> - Show service status")
fmt.Println(" restart <service> - Restart a service")
fmt.Println(" logs <service> - Show service logs")
func showServicesHelp(u *uiContext) {
u.header("Service Management")
u.muted("Usage: telecom-cli services <command> [options]")
fmt.Println()
t := u.newTable()
t.AddColumn("Command", 20, "left")
t.AddColumn("Description", 40, "left")
t.AddRow("list", "List all services")
t.AddRow("status <service>", "Show service status")
t.AddRow("restart <service>", "Restart a service")
t.AddRow("logs <service>", "Show service logs")
fmt.Println(t.Render())
}
func listServices() {
fmt.Println("Platform Services:")
fmt.Println("Service Status Version Uptime")
fmt.Println("----------------------------------------------------")
fmt.Println("api-server Running v1.0.0 2h15m")
fmt.Println("charging-engine Running v1.0.0 2h15m")
fmt.Println("packet-gateway Running v1.0.0 2h15m")
fmt.Println("web-dashboard Running v1.0.0 2h15m")
fmt.Println("prometheus Running v2.45.0 2h15m")
fmt.Println("grafana Running v9.5.2 2h15m")
func listServices(u *uiContext) error {
u.header("Platform Services")
services, err := u.client.ListServices()
t := u.newTable()
t.AddColumn("Service", 18, "left")
t.AddColumn("Status", 10, "left")
t.AddColumn("Version", 10, "left")
t.AddColumn("Uptime", 10, "right")
if err != nil {
u.warn("Using placeholder data: " + err.Error())
placeholders := [][]string{
{"api-server", "Running", "v1.0.0", "2h15m"},
{"charging-engine", "Running", "v1.0.0", "2h15m"},
{"packet-gateway", "Running", "v1.0.0", "2h15m"},
{"web-dashboard", "Running", "v1.0.0", "2h15m"},
{"prometheus", "Running", "v2.45.0", "2h15m"},
{"grafana", "Running", "v9.5.2", "2h15m"},
}
for _, row := range placeholders {
t.AddStyledRow(statusStyle(strings.ToUpper(row[1])).Style, row[0], row[1], row[2], row[3])
}
} else {
for _, s := range services {
t.AddStyledRow(statusStyle(strings.ToUpper(s.Status)).Style, s.Name, s.Status, s.Version, s.Uptime)
}
}
fmt.Println(t.Render())
return nil
}
func showServiceStatus(args []string) {
func showServiceStatus(u *uiContext, args []string) error {
if len(args) < 1 {
fmt.Println("Error: Service name is required")
fmt.Println("Usage: telecom-cli services status <service>")
return
u.errorln("Error: Service name is required")
u.muted("Usage: telecom-cli services status <service>")
return fmt.Errorf("missing service")
}
service := args[0]
fmt.Printf("Service Status: %s\n", service)
fmt.Println("Status: Running")
fmt.Println("Version: v1.0.0")
fmt.Println("Uptime: 2h15m")
fmt.Println("CPU Usage: 45%")
fmt.Println("Memory Usage: 256MB")
fmt.Println("Last Restart: 2024-01-15 14:30:00")
u.header("Service Status: " + service)
services, err := u.client.ListServices()
t := u.newTable()
t.AddColumn("Field", 14, "left")
t.AddColumn("Value", 28, "left")
if err != nil {
u.warn("Using placeholder data: " + err.Error())
t.AddRow("Status", "Running")
t.AddRow("Version", "v1.0.0")
t.AddRow("Uptime", "2h15m")
t.AddRow("CPU Usage", "45%")
t.AddRow("Memory Usage", "256MB")
fmt.Println(t.Render())
return nil
}
for _, s := range services {
if s.Name == service {
t.AddRow("Status", s.Status)
t.AddRow("Version", s.Version)
t.AddRow("Uptime", s.Uptime)
t.AddRow("CPU Usage", fmt.Sprintf("%.1f%%", s.CPU))
t.AddRow("Memory Usage", s.Memory)
fmt.Println(t.Render())
return nil
}
}
u.errorln("Service not found: " + service)
return fmt.Errorf("service not found")
}
func restartService(args []string) {
func restartService(u *uiContext, args []string) error {
if len(args) < 1 {
fmt.Println("Error: Service name is required")
fmt.Println("Usage: telecom-cli services restart <service>")
return
u.errorln("Error: Service name is required")
u.muted("Usage: telecom-cli services restart <service>")
return fmt.Errorf("missing service")
}
service := args[0]
fmt.Printf("Restarting service: %s\n", service)
fmt.Println("Service restarted successfully!")
u.info("Restarting service: " + service)
err := u.client.PostRestart(service)
if err != nil {
u.warn("API error, simulated restart: " + err.Error())
}
u.success("Service restarted successfully!")
return nil
}
func showServiceLogs(args []string) {
func showServiceLogs(u *uiContext, args []string) error {
if len(args) < 1 {
fmt.Println("Error: Service name is required")
fmt.Println("Usage: telecom-cli services logs <service>")
return
u.errorln("Error: Service name is required")
u.muted("Usage: telecom-cli services logs <service>")
return fmt.Errorf("missing service")
}
service := args[0]
fmt.Printf("Recent logs for %s:\n", service)
fmt.Println("2024-01-15 16:45:30 [INFO] Service started successfully")
fmt.Println("2024-01-15 16:45:35 [INFO] Database connection established")
fmt.Println("2024-01-15 16:45:40 [INFO] API server listening on port 8000")
fmt.Println("2024-01-15 16:45:45 [INFO] Health check passed")
u.header("Recent logs for " + service)
// Logs endpoint not yet implemented; show placeholder logs
u.muted("(Showing placeholder log entries - logs API not yet connected)")
logs := []string{
"2024-01-15 16:45:30 [INFO] Service started successfully",
"2024-01-15 16:45:35 [INFO] Database connection established",
"2024-01-15 16:45:40 [INFO] API server listening on port 8000",
"2024-01-15 16:45:45 [INFO] Health check passed",
}
for _, line := range logs {
fmt.Println(u.colorizer.Colorize(line, statusStyle("OK")))
}
return nil
}