Skip to content

Commit 80ea3dc

Browse files
committed
PR feedback
1 parent f6e8702 commit 80ea3dc

File tree

3 files changed

+45
-24
lines changed

3 files changed

+45
-24
lines changed

cmd/config/plugins/add.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,10 @@ in the category, the command fails with an error. To update an existing plugin,
8585
&c.flows,
8686
flagFlow,
8787
nil,
88-
"Flow to execute plugin in: request, response (can be repeated)",
88+
fmt.Sprintf(
89+
"Flow during which, the plugin should execute (%s) (can be repeated)",
90+
strings.Join(config.OrderedFlowNames(), ", "),
91+
),
8992
)
9093
_ = cobraCmd.MarkFlagRequired(flagFlow)
9194

@@ -128,16 +131,17 @@ func (c *AddCmd) run(cmd *cobra.Command, args []string) error {
128131
)
129132
}
130133

131-
flowsMap := config.ParseFlows(c.flows)
132-
if len(flowsMap) == 0 {
133-
return fmt.Errorf("at least one valid flow is required (must be 'request' or 'response')")
134+
flows := config.ParseFlowsDistinct(c.flows)
135+
if len(flows) == 0 {
136+
return fmt.Errorf(
137+
"at least one valid flow is required (%s)",
138+
strings.Join(config.OrderedFlowNames(), ", "),
139+
)
134140
}
135141

136-
parsedFlows := slices.Sorted(maps.Keys(flowsMap))
137-
138142
entry := config.PluginEntry{
139143
Name: pluginName,
140-
Flows: parsedFlows,
144+
Flows: slices.Sorted(maps.Keys(flows)),
141145
}
142146

143147
// Set optional fields only if they were provided.

internal/config/plugin_config.go

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,10 @@ func (f Flow) IsValid() bool {
9999
return ok
100100
}
101101

102-
// ParseFlows validates and reduces flow strings to a distinct set.
102+
// ParseFlowsDistinct validates and reduces flow strings to a distinct set.
103103
// Flow strings are normalized before validation.
104104
// Invalid flows are silently ignored. Returns an empty map if no valid flows are found.
105-
func ParseFlows(flags []string) map[Flow]struct{} {
105+
func ParseFlowsDistinct(flags []string) map[Flow]struct{} {
106106
valid := make(map[Flow]struct{}, len(flows))
107107

108108
for _, s := range flags {
@@ -244,12 +244,10 @@ func (e *PluginEntry) Validate() error {
244244
} else {
245245
seen := make(map[Flow]struct{})
246246
for _, flow := range e.Flows {
247-
// Check for valid flow values.
248247
if !flow.IsValid() {
249-
validationErrors = append(
250-
validationErrors,
251-
fmt.Errorf("invalid flow '%s', must be '%s' or '%s'", flow, FlowRequest, FlowResponse),
252-
)
248+
allowedFlows := strings.Join(OrderedFlowNames(), ", ")
249+
err := fmt.Errorf("invalid flow '%s' (allowed: %s)", flow, allowedFlows)
250+
validationErrors = append(validationErrors, err)
253251
}
254252

255253
// Check for duplicates.
@@ -526,6 +524,18 @@ func OrderedCategories() Categories {
526524
return slices.Clone(orderedCategories)
527525
}
528526

527+
// OrderedFlowNames returns the names of allowed flows in order.
528+
func OrderedFlowNames() []string {
529+
flows := slices.Sorted(maps.Keys(flows))
530+
531+
flowNames := make([]string, len(flows))
532+
for i, f := range flows {
533+
flowNames[i] = string(f)
534+
}
535+
536+
return flowNames
537+
}
538+
529539
// Set is used by Cobra to set the category value from a string.
530540
// NOTE: This is also required by Cobra as part of implementing flag.Value.
531541
func (c *Category) Set(v string) error {

internal/config/plugin_config_test.go

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@ import (
88
"github.com/mozilla-ai/mcpd/v2/internal/context"
99
)
1010

11+
func testPluginStringPtr(t *testing.T, s string) *string {
12+
t.Helper()
13+
return &s
14+
}
15+
16+
func testPluginBoolPtr(t *testing.T, b bool) *bool {
17+
t.Helper()
18+
return &b
19+
}
20+
1121
func TestPluginEntry_Validate(t *testing.T) {
1222
t.Parallel()
1323

@@ -790,7 +800,7 @@ func TestFlow_IsValid(t *testing.T) {
790800
}
791801
}
792802

793-
func TestParseFlows(t *testing.T) {
803+
func TestParseFlowsDistinct(t *testing.T) {
794804
t.Parallel()
795805

796806
tests := []struct {
@@ -866,18 +876,15 @@ func TestParseFlows(t *testing.T) {
866876
t.Run(tc.name, func(t *testing.T) {
867877
t.Parallel()
868878

869-
result := ParseFlows(tc.input)
879+
result := ParseFlowsDistinct(tc.input)
870880
require.Equal(t, tc.expected, result)
871881
})
872882
}
873883
}
874884

875-
func testPluginStringPtr(t *testing.T, s string) *string {
876-
t.Helper()
877-
return &s
878-
}
879-
880-
func testPluginBoolPtr(t *testing.T, b bool) *bool {
881-
t.Helper()
882-
return &b
885+
func TestAddCmd_OrderedFlowNames(t *testing.T) {
886+
flows := OrderedFlowNames()
887+
require.Len(t, flows, 2)
888+
require.Equal(t, "request", flows[0])
889+
require.Equal(t, "response", flows[1])
883890
}

0 commit comments

Comments
 (0)