Skip to content

Commit 13fdbe2

Browse files
authored
464 add group creation command (#1105)
1 parent e1dde79 commit 13fdbe2

File tree

8 files changed

+974
-0
lines changed

8 files changed

+974
-0
lines changed

cmd/thv/app/commands.go

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

5657
// Silence printing the usage on error
5758
rootCmd.SilenceUsage = true

cmd/thv/app/group.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package app
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/spf13/cobra"
7+
8+
"github.com/stacklok/toolhive/pkg/groups"
9+
)
10+
11+
var groupCmd = &cobra.Command{
12+
Use: "group",
13+
Short: "Manage logical groupings of MCP servers",
14+
Long: `The group command provides subcommands to manage logical groupings of MCP servers.`,
15+
}
16+
17+
var groupCreateCmd = &cobra.Command{
18+
Use: "create [group-name]",
19+
Short: "Create a new group of MCP servers",
20+
Long: `Create a new logical group of MCP servers. The group can be used to organize and manage multiple MCP servers together.`,
21+
Args: cobra.ExactArgs(1),
22+
RunE: groupCreateCmdFunc,
23+
}
24+
25+
func groupCreateCmdFunc(cmd *cobra.Command, args []string) error {
26+
groupName := args[0]
27+
ctx := cmd.Context()
28+
29+
manager, err := groups.NewManager()
30+
if err != nil {
31+
return fmt.Errorf("failed to create group manager: %w", err)
32+
}
33+
34+
if err := manager.Create(ctx, groupName); err != nil {
35+
return err
36+
}
37+
38+
fmt.Printf("Group '%s' created successfully.\n", groupName)
39+
return nil
40+
}
41+
42+
func init() {
43+
groupCmd.AddCommand(groupCreateCmd)
44+
}

pkg/groups/group.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Package groups provides functionality for managing logical groupings of MCP servers.
2+
// It includes types and interfaces for creating, retrieving, listing, and deleting groups.
3+
package groups
4+
5+
import (
6+
"context"
7+
"encoding/json"
8+
"os"
9+
)
10+
11+
// Group represents a logical grouping of MCP servers.
12+
type Group struct {
13+
Name string `json:"name"`
14+
}
15+
16+
// WriteJSON serializes the Group to JSON and writes it to the provided writer
17+
func (g *Group) WriteJSON(w *os.File) error {
18+
encoder := json.NewEncoder(w)
19+
encoder.SetIndent("", " ")
20+
return encoder.Encode(g)
21+
}
22+
23+
// Manager defines the interface for managing groups of MCP servers.
24+
// It provides methods for creating, retrieving, listing, and deleting groups.
25+
type Manager interface {
26+
// Create creates a new group with the specified name.
27+
// Returns an error if a group with the same name already exists.
28+
Create(ctx context.Context, name string) error
29+
30+
// Get retrieves a group by name.
31+
// Returns an error if the group does not exist.
32+
Get(ctx context.Context, name string) (*Group, error)
33+
34+
// List returns all groups.
35+
List(ctx context.Context) ([]*Group, error)
36+
37+
// Delete removes a group by name.
38+
// Returns an error if the group does not exist.
39+
Delete(ctx context.Context, name string) error
40+
41+
// Exists checks if a group with the specified name exists.
42+
Exists(ctx context.Context, name string) (bool, error)
43+
}

0 commit comments

Comments
 (0)