Introduces a new command-line tool for managing mc-proxy via the gRPC admin API over Unix socket. Commands include route and firewall rule CRUD operations, health checks, and status queries. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
114 lines
2.8 KiB
Go
114 lines
2.8 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
func routesCmd() *cobra.Command {
|
|
cmd := &cobra.Command{
|
|
Use: "routes",
|
|
Short: "Manage routes",
|
|
Long: "Manage routes for mc-proxy listeners.",
|
|
}
|
|
|
|
cmd.AddCommand(routesListCmd())
|
|
cmd.AddCommand(routesAddCmd())
|
|
cmd.AddCommand(routesRemoveCmd())
|
|
|
|
return cmd
|
|
}
|
|
|
|
func routesListCmd() *cobra.Command {
|
|
return &cobra.Command{
|
|
Use: "list LISTENER",
|
|
Short: "List routes for a listener",
|
|
Long: "List all routes configured for the specified listener address.",
|
|
Args: cobra.ExactArgs(1),
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
listenerAddr := args[0]
|
|
client := clientFromContext(cmd.Context())
|
|
|
|
ctx, cancel := context.WithTimeout(cmd.Context(), 5*time.Second)
|
|
defer cancel()
|
|
|
|
routes, err := client.ListRoutes(ctx, listenerAddr)
|
|
if err != nil {
|
|
return fmt.Errorf("listing routes: %w", err)
|
|
}
|
|
|
|
if len(routes) == 0 {
|
|
fmt.Printf("No routes for %s\n", listenerAddr)
|
|
return nil
|
|
}
|
|
|
|
// Find max hostname length for alignment
|
|
maxHostLen := 0
|
|
for _, r := range routes {
|
|
if len(r.Hostname) > maxHostLen {
|
|
maxHostLen = len(r.Hostname)
|
|
}
|
|
}
|
|
|
|
fmt.Printf("Routes for %s:\n", listenerAddr)
|
|
for _, r := range routes {
|
|
fmt.Printf(" %-*s -> %s\n", maxHostLen, r.Hostname, r.Backend)
|
|
}
|
|
|
|
return nil
|
|
},
|
|
}
|
|
}
|
|
|
|
func routesAddCmd() *cobra.Command {
|
|
return &cobra.Command{
|
|
Use: "add LISTENER HOSTNAME BACKEND",
|
|
Short: "Add a route",
|
|
Long: "Add a route mapping a hostname to a backend for the specified listener.",
|
|
Args: cobra.ExactArgs(3),
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
listenerAddr := args[0]
|
|
hostname := args[1]
|
|
backend := args[2]
|
|
client := clientFromContext(cmd.Context())
|
|
|
|
ctx, cancel := context.WithTimeout(cmd.Context(), 5*time.Second)
|
|
defer cancel()
|
|
|
|
if err := client.AddRoute(ctx, listenerAddr, hostname, backend); err != nil {
|
|
return fmt.Errorf("adding route: %w", err)
|
|
}
|
|
|
|
fmt.Printf("Added route: %s -> %s on %s\n", hostname, backend, listenerAddr)
|
|
return nil
|
|
},
|
|
}
|
|
}
|
|
|
|
func routesRemoveCmd() *cobra.Command {
|
|
return &cobra.Command{
|
|
Use: "remove LISTENER HOSTNAME",
|
|
Short: "Remove a route",
|
|
Long: "Remove a route for the specified hostname from the listener.",
|
|
Args: cobra.ExactArgs(2),
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
listenerAddr := args[0]
|
|
hostname := args[1]
|
|
client := clientFromContext(cmd.Context())
|
|
|
|
ctx, cancel := context.WithTimeout(cmd.Context(), 5*time.Second)
|
|
defer cancel()
|
|
|
|
if err := client.RemoveRoute(ctx, listenerAddr, hostname); err != nil {
|
|
return fmt.Errorf("removing route: %w", err)
|
|
}
|
|
|
|
fmt.Printf("Removed route: %s from %s\n", hostname, listenerAddr)
|
|
return nil
|
|
},
|
|
}
|
|
}
|