Skip to content

Commit 250b078

Browse files
authored
Adds the command to list groups (#1148)
1 parent ffa3a9c commit 250b078

File tree

3 files changed

+354
-10
lines changed

3 files changed

+354
-10
lines changed

cmd/thv/app/group.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ package app
22

33
import (
44
"fmt"
5+
"os"
6+
"sort"
7+
"strings"
8+
"text/tabwriter"
59

610
"github.com/spf13/cobra"
711

@@ -22,6 +26,13 @@ var groupCreateCmd = &cobra.Command{
2226
RunE: groupCreateCmdFunc,
2327
}
2428

29+
var groupListCmd = &cobra.Command{
30+
Use: "list",
31+
Short: "List all groups",
32+
Long: `List all logical groups of MCP servers.`,
33+
RunE: groupListCmdFunc,
34+
}
35+
2536
func groupCreateCmdFunc(cmd *cobra.Command, args []string) error {
2637
groupName := args[0]
2738
ctx := cmd.Context()
@@ -39,6 +50,47 @@ func groupCreateCmdFunc(cmd *cobra.Command, args []string) error {
3950
return nil
4051
}
4152

53+
func groupListCmdFunc(cmd *cobra.Command, _ []string) error {
54+
ctx := cmd.Context()
55+
56+
manager, err := groups.NewManager()
57+
if err != nil {
58+
return fmt.Errorf("failed to create group manager: %w", err)
59+
}
60+
61+
allGroups, err := manager.List(ctx)
62+
if err != nil {
63+
return fmt.Errorf("failed to list groups: %w", err)
64+
}
65+
66+
// Sort groups alphanumerically by name (handles mixed characters, numbers, etc.)
67+
sort.Slice(allGroups, func(i, j int) bool {
68+
return strings.Compare(allGroups[i].Name, allGroups[j].Name) < 0
69+
})
70+
71+
if len(allGroups) == 0 {
72+
fmt.Println("No groups configured.")
73+
return nil
74+
}
75+
76+
// Create a tabwriter for table output
77+
w := tabwriter.NewWriter(os.Stdout, 0, 0, 3, ' ', 0)
78+
fmt.Fprintln(w, "NAME")
79+
80+
// Print group names in table format
81+
for _, group := range allGroups {
82+
fmt.Fprintf(w, "%s\n", group.Name)
83+
}
84+
85+
// Flush the tabwriter
86+
if err := w.Flush(); err != nil {
87+
return fmt.Errorf("failed to flush tabwriter: %w", err)
88+
}
89+
90+
return nil
91+
}
92+
4293
func init() {
4394
groupCmd.AddCommand(groupCreateCmd)
95+
groupCmd.AddCommand(groupListCmd)
4496
}

cmd/thv/app/run.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,11 @@ permission profile. Additional configuration can be provided via flags.`,
4949
},
5050
}
5151

52-
var runConfig RunFlags
52+
var runFlags RunFlags
5353

5454
func init() {
5555
// Add run flags
56-
AddRunFlags(runCmd, &runConfig)
56+
AddRunFlags(runCmd, &runFlags)
5757

5858
// This is used for the K8s operator which wraps the run command, but shouldn't be visible to users.
5959
if err := runCmd.Flags().MarkHidden("k8s-pod-patch"); err != nil {
@@ -81,33 +81,33 @@ func runCmdFunc(cmd *cobra.Command, args []string) error {
8181
debugMode, _ := cmd.Flags().GetBool("debug")
8282

8383
// Build the run configuration
84-
runnerConfig, err := BuildRunnerConfig(ctx, &runConfig, serverOrImage, cmdArgs, debugMode, cmd)
84+
runnerConfig, err := BuildRunnerConfig(ctx, &runFlags, serverOrImage, cmdArgs, debugMode, cmd)
8585
if err != nil {
8686
return err
8787
}
8888

89-
if runConfig.Group != "" {
89+
if runFlags.Group != "" {
9090
groupManager, err := groups.NewManager()
9191
if err != nil {
9292
return fmt.Errorf("failed to create group manager: %v", err)
9393
}
9494

9595
// Check if the workload is already in a group
96-
group, err := groupManager.GetWorkloadGroup(ctx, runConfig.Name)
96+
group, err := groupManager.GetWorkloadGroup(ctx, runFlags.Name)
9797
if err != nil {
9898
return fmt.Errorf("failed to get workload group: %v", err)
9999
}
100-
if group != nil && group.Name != runConfig.Group {
101-
return fmt.Errorf("workload '%s' is already in group '%s'", runConfig.Name, group.Name)
100+
if group != nil && group.Name != runFlags.Group {
101+
return fmt.Errorf("workload '%s' is already in group '%s'", runFlags.Name, group.Name)
102102
}
103103

104104
// Validate that the group specified exists
105-
exists, err := groupManager.Exists(ctx, runConfig.Group)
105+
exists, err := groupManager.Exists(ctx, runFlags.Group)
106106
if err != nil {
107107
return fmt.Errorf("failed to check if group exists: %v", err)
108108
}
109109
if !exists {
110-
return fmt.Errorf("group '%s' does not exist", runConfig.Group)
110+
return fmt.Errorf("group '%s' does not exist", runFlags.Group)
111111
}
112112
}
113113

@@ -118,7 +118,7 @@ func runCmdFunc(cmd *cobra.Command, args []string) error {
118118
}
119119
workloadManager := workloads.NewManagerFromRuntime(rt)
120120

121-
if runConfig.Foreground {
121+
if runFlags.Foreground {
122122
err = workloadManager.RunWorkload(ctx, runnerConfig)
123123
} else {
124124
// Run the workload in detached mode

0 commit comments

Comments
 (0)