Skip to content

Commit 73a3707

Browse files
authored
1179 list servers in a group (#1205)
1 parent ef6dc24 commit 73a3707

File tree

3 files changed

+411
-2
lines changed

3 files changed

+411
-2
lines changed

cmd/thv/app/commands.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func NewRootCmd(enableUpdates bool) *cobra.Command {
5353
rootCmd.AddCommand(newSecretCommand())
5454
rootCmd.AddCommand(inspectorCommand())
5555
rootCmd.AddCommand(newMCPCommand())
56-
// rootCmd.AddCommand(groupCmd) // TODO: add back in once we have a working group command, and update the docs
56+
// rootCmd.AddCommand(groupCmd) // TODO: add back in once we have a working group command, and update the docs
5757

5858
// Silence printing the usage on error
5959
rootCmd.SilenceUsage = true

cmd/thv/app/list.go

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package app
22

33
import (
4+
"context"
45
"encoding/json"
56
"fmt"
67
"os"
78
"text/tabwriter"
89

910
"github.com/spf13/cobra"
1011

12+
"github.com/stacklok/toolhive/pkg/groups"
1113
"github.com/stacklok/toolhive/pkg/logger"
1214
"github.com/stacklok/toolhive/pkg/workloads"
1315
)
@@ -23,12 +25,15 @@ var (
2325
listAll bool
2426
listFormat string
2527
listLabelFilter []string
28+
listGroupFilter string
2629
)
2730

2831
func init() {
2932
listCmd.Flags().BoolVarP(&listAll, "all", "a", false, "Show all workloads (default shows just running)")
3033
listCmd.Flags().StringVar(&listFormat, "format", FormatText, "Output format (json, text, or mcpservers)")
3134
listCmd.Flags().StringArrayVarP(&listLabelFilter, "label", "l", []string{}, "Filter workloads by labels (format: key=value)")
35+
// TODO: Re-enable when group functionality is complete
36+
// listCmd.Flags().StringVar(&listGroupFilter, "group", "", "Filter workloads by group")
3237
}
3338

3439
func listCmdFunc(cmd *cobra.Command, _ []string) error {
@@ -45,8 +50,20 @@ func listCmdFunc(cmd *cobra.Command, _ []string) error {
4550
return fmt.Errorf("failed to list workloads: %v", err)
4651
}
4752

53+
// Apply group filtering if specified
54+
if listGroupFilter != "" {
55+
workloadList, err = filterWorkloadsByGroup(ctx, workloadList, listGroupFilter)
56+
if err != nil {
57+
return fmt.Errorf("failed to filter workloads by group: %v", err)
58+
}
59+
}
60+
4861
if len(workloadList) == 0 {
49-
fmt.Println("No MCP servers found")
62+
if listGroupFilter != "" {
63+
fmt.Printf("No MCP servers found in group '%s'\n", listGroupFilter)
64+
} else {
65+
fmt.Println("No MCP servers found")
66+
}
5067
return nil
5168
}
5269

@@ -62,6 +79,47 @@ func listCmdFunc(cmd *cobra.Command, _ []string) error {
6279
}
6380
}
6481

82+
// filterWorkloadsByGroup filters workloads to only include those in the specified group
83+
func filterWorkloadsByGroup(
84+
ctx context.Context, workloadList []workloads.Workload, groupName string,
85+
) ([]workloads.Workload, error) {
86+
// Create group manager
87+
groupManager, err := groups.NewManager()
88+
if err != nil {
89+
return nil, fmt.Errorf("failed to create group manager: %v", err)
90+
}
91+
92+
// Check if the group exists
93+
exists, err := groupManager.Exists(ctx, groupName)
94+
if err != nil {
95+
return nil, fmt.Errorf("failed to check if group exists: %v", err)
96+
}
97+
if !exists {
98+
return nil, fmt.Errorf("group '%s' does not exist", groupName)
99+
}
100+
101+
// Get all workload names in the specified group
102+
groupWorkloadNames, err := groupManager.ListWorkloadsInGroup(ctx, groupName)
103+
if err != nil {
104+
return nil, fmt.Errorf("failed to list workloads in group: %v", err)
105+
}
106+
107+
groupWorkloadMap := make(map[string]struct{})
108+
for _, name := range groupWorkloadNames {
109+
groupWorkloadMap[name] = struct{}{}
110+
}
111+
112+
// Filter workloads that belong to the specified group
113+
var filteredWorkloads []workloads.Workload
114+
for _, workload := range workloadList {
115+
if _, ok := groupWorkloadMap[workload.Name]; ok {
116+
filteredWorkloads = append(filteredWorkloads, workload)
117+
}
118+
}
119+
120+
return filteredWorkloads, nil
121+
}
122+
65123
// printJSONOutput prints workload information in JSON format
66124
func printJSONOutput(workloadList []workloads.Workload) error {
67125
// Marshal to JSON

0 commit comments

Comments
 (0)