1
1
package app
2
2
3
3
import (
4
+ "context"
4
5
"encoding/json"
5
6
"fmt"
6
7
"os"
7
8
"text/tabwriter"
8
9
9
10
"github.com/spf13/cobra"
10
11
12
+ "github.com/stacklok/toolhive/pkg/groups"
11
13
"github.com/stacklok/toolhive/pkg/logger"
12
14
"github.com/stacklok/toolhive/pkg/workloads"
13
15
)
@@ -23,12 +25,15 @@ var (
23
25
listAll bool
24
26
listFormat string
25
27
listLabelFilter []string
28
+ listGroupFilter string
26
29
)
27
30
28
31
func init () {
29
32
listCmd .Flags ().BoolVarP (& listAll , "all" , "a" , false , "Show all workloads (default shows just running)" )
30
33
listCmd .Flags ().StringVar (& listFormat , "format" , FormatText , "Output format (json, text, or mcpservers)" )
31
34
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")
32
37
}
33
38
34
39
func listCmdFunc (cmd * cobra.Command , _ []string ) error {
@@ -45,8 +50,20 @@ func listCmdFunc(cmd *cobra.Command, _ []string) error {
45
50
return fmt .Errorf ("failed to list workloads: %v" , err )
46
51
}
47
52
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
+
48
61
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
+ }
50
67
return nil
51
68
}
52
69
@@ -62,6 +79,47 @@ func listCmdFunc(cmd *cobra.Command, _ []string) error {
62
79
}
63
80
}
64
81
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
+
65
123
// printJSONOutput prints workload information in JSON format
66
124
func printJSONOutput (workloadList []workloads.Workload ) error {
67
125
// Marshal to JSON
0 commit comments