-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathremove.go
More file actions
100 lines (84 loc) · 2.78 KB
/
remove.go
File metadata and controls
100 lines (84 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package args
import (
"fmt"
"maps"
"slices"
"strings"
"github.com/spf13/cobra"
"github.com/mozilla-ai/mcpd/internal/cmd"
"github.com/mozilla-ai/mcpd/internal/cmd/options"
"github.com/mozilla-ai/mcpd/internal/config"
"github.com/mozilla-ai/mcpd/internal/context"
"github.com/mozilla-ai/mcpd/internal/flags"
)
type RemoveCmd struct {
*cmd.BaseCmd
ctxLoader context.Loader
}
func NewRemoveCmd(baseCmd *cmd.BaseCmd, opt ...options.CmdOption) (*cobra.Command, error) {
opts, err := options.NewOptions(opt...)
if err != nil {
return nil, err
}
c := &RemoveCmd{
BaseCmd: baseCmd,
ctxLoader: opts.ContextLoader,
}
// mcpd config args remove time -- [arg ...] [--arg ...]
cobraCmd := &cobra.Command{
Use: "remove <server-name> -- [arg ...] [--arg ...]",
Example: "remove time -- --local-timezone",
Short: "Remove arguments from an MCP server's configuration",
Long: "Remove arguments from a specified MCP server in the runtime context " +
"configuration file (e.g. `~/.config/mcpd/secrets.dev.toml`).\n\n" +
"This command removes ALL occurrences of each specified argument, whether they are:\n" +
" - Flags (e.g., --verbose, -v)\n" +
" - Flags with values (e.g., --config=file.json)\n" +
" - Positional arguments (e.g., /path/to/file)\n\n" +
"Matching behavior:\n" +
" - Specifying '--verbose' removes ALL '--verbose' variants (--verbose, --verbose=true, etc.)\n" +
" - Specifying '--port=8080' removes ONLY exact matches of '--port=8080'\n" +
" - If an argument appears multiple times, all instances are removed",
RunE: c.run,
Args: cobra.MinimumNArgs(2), // server-name + args ...
}
return cobraCmd, nil
}
func (c *RemoveCmd) run(cmd *cobra.Command, args []string) error {
serverName := strings.TrimSpace(args[0])
if serverName == "" {
return fmt.Errorf("server-name is required")
}
argVars := args[1:]
argMap := make(map[string]struct{}, len(argVars))
for _, key := range argVars {
key = strings.TrimSpace(key)
argMap[key] = struct{}{}
}
cfg, err := c.ctxLoader.Load(flags.RuntimeFile)
if err != nil {
return fmt.Errorf("failed to load execution context config: %w", err)
}
serverCtx, ok := cfg.Get(serverName)
if !ok {
return fmt.Errorf("server '%s' not found in configuration", serverName)
}
toRemove := slices.Collect(maps.Keys(argMap))
filtered := config.RemoveMatchingFlags(serverCtx.Args, toRemove)
// Update the args, and the server.
serverCtx.Args = filtered
res, err := cfg.Upsert(serverCtx)
if err != nil {
return fmt.Errorf("error removing arguments for server '%s': %w", serverName, err)
}
if _, err := fmt.Fprintf(
cmd.OutOrStdout(),
"✓ Arguments removed for server '%s' (operation: %s): %v\n",
serverName,
string(res),
slices.Collect(maps.Keys(argMap)),
); err != nil {
return err
}
return nil
}