Skip to content

Commit 81ef131

Browse files
authored
Add plugin list command (#215)
Implement 'mcpd config plugins list' command to display configured plugins. Supports filtering by category or showing all categories with output in text, JSON, or YAML formats.
1 parent 144ec22 commit 81ef131

File tree

12 files changed

+1505
-114
lines changed

12 files changed

+1505
-114
lines changed

cmd/config/plugins/list.go

Lines changed: 96 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,107 @@ import (
55

66
"github.com/spf13/cobra"
77

8-
"github.com/mozilla-ai/mcpd/v2/internal/cmd"
9-
"github.com/mozilla-ai/mcpd/v2/internal/cmd/options"
8+
internalcmd "github.com/mozilla-ai/mcpd/v2/internal/cmd"
9+
cmdopts "github.com/mozilla-ai/mcpd/v2/internal/cmd/options"
10+
"github.com/mozilla-ai/mcpd/v2/internal/cmd/output"
11+
"github.com/mozilla-ai/mcpd/v2/internal/config"
12+
"github.com/mozilla-ai/mcpd/v2/internal/printer"
1013
)
1114

12-
// NewListCmd creates the list command for plugins.
13-
// TODO: Implement in a future PR.
14-
func NewListCmd(baseCmd *cmd.BaseCmd, opt ...options.CmdOption) (*cobra.Command, error) {
15+
// ListCmd represents the command for listing configured plugins.
16+
// Use NewListCmd to create instances of ListCmd.
17+
type ListCmd struct {
18+
*internalcmd.BaseCmd
19+
20+
// cfgLoader is used to load the configuration.
21+
cfgLoader config.Loader
22+
23+
// printer is used to output configured plugins.
24+
printer output.Printer[printer.PluginListResult]
25+
26+
// format stores the format flag when specified.
27+
format internalcmd.OutputFormat
28+
29+
// category stores the category flag when specified.
30+
category config.Category
31+
}
32+
33+
// NewListCmd creates a new list command for displaying configured plugins.
34+
func NewListCmd(baseCmd *internalcmd.BaseCmd, opt ...cmdopts.CmdOption) (*cobra.Command, error) {
35+
opts, err := cmdopts.NewOptions(opt...)
36+
if err != nil {
37+
return nil, err
38+
}
39+
40+
c := &ListCmd{
41+
BaseCmd: baseCmd,
42+
cfgLoader: opts.ConfigLoader,
43+
printer: &printer.PluginListPrinter{},
44+
format: internalcmd.FormatText, // Default to plain text
45+
}
46+
1547
cobraCmd := &cobra.Command{
1648
Use: "list",
17-
Short: "List plugin entries",
18-
Long: "List plugin entries in a specific category or across all categories",
19-
RunE: func(cmd *cobra.Command, args []string) error {
20-
return fmt.Errorf("not yet implemented")
21-
},
49+
Short: "List configured plugin entries",
50+
Long: "List configured plugin entries in a specific category or across all categories",
51+
Example: ` # List plugins in authentication category
52+
mcpd config plugins list --category=authentication
53+
54+
# List all plugins across all categories
55+
mcpd config plugins list
56+
57+
# List with JSON output
58+
mcpd config plugins list --category=observability --format=json`,
59+
RunE: c.run,
60+
Args: cobra.NoArgs,
2261
}
2362

63+
allowedOutputFormats := internalcmd.AllowedOutputFormats()
64+
cobraCmd.Flags().Var(
65+
&c.format,
66+
"format",
67+
fmt.Sprintf("Specify the output format (one of: %s)", allowedOutputFormats.String()),
68+
)
69+
70+
allowedCategories := config.OrderedCategories()
71+
cobraCmd.Flags().Var(
72+
&c.category,
73+
"category",
74+
fmt.Sprintf("Specify the category (one of: %s)", allowedCategories.String()),
75+
)
76+
2477
return cobraCmd, nil
2578
}
79+
80+
func (c *ListCmd) run(cmd *cobra.Command, _ []string) error {
81+
handler, err := internalcmd.FormatHandler(cmd.OutOrStdout(), c.format, c.printer)
82+
if err != nil {
83+
return err
84+
}
85+
86+
cfg, err := c.LoadConfig(c.cfgLoader)
87+
if err != nil {
88+
return handler.HandleError(err)
89+
}
90+
91+
if cfg.Plugins == nil {
92+
result := printer.NewPluginListResult(nil)
93+
return handler.HandleResult(result)
94+
}
95+
96+
var categories map[config.Category][]config.PluginEntry
97+
98+
if c.category != "" {
99+
// List single category.
100+
categories = map[config.Category][]config.PluginEntry{
101+
c.category: cfg.Plugins.ListPlugins(c.category),
102+
}
103+
} else {
104+
// List all categories.
105+
categories = cfg.Plugins.AllCategories()
106+
}
107+
108+
result := printer.NewPluginListResult(categories)
109+
110+
return handler.HandleResult(result)
111+
}

0 commit comments

Comments
 (0)