Skip to content

Commit 9961b65

Browse files
committed
Code shuffling
1 parent be6e7aa commit 9961b65

File tree

4 files changed

+35
-34
lines changed

4 files changed

+35
-34
lines changed

cmd/config/plugins/add.go

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ in the category, the command fails with an error. To update an existing plugin,
8787
nil,
8888
fmt.Sprintf(
8989
"Flow during which, the plugin should execute (%s) (can be repeated)",
90-
strings.Join(OrderedFlowNames(), ", "),
90+
strings.Join(config.OrderedFlowNames(), ", "),
9191
),
9292
)
9393
_ = cobraCmd.MarkFlagRequired(flagFlow)
@@ -133,7 +133,10 @@ func (c *AddCmd) run(cmd *cobra.Command, args []string) error {
133133

134134
flows := config.ParseFlowsDistinct(c.flows)
135135
if len(flows) == 0 {
136-
return fmt.Errorf("at least one valid flow is required (%s)", strings.Join(OrderedFlowNames(), ", "))
136+
return fmt.Errorf(
137+
"at least one valid flow is required (%s)",
138+
strings.Join(config.OrderedFlowNames(), ", "),
139+
)
137140
}
138141

139142
entry := config.PluginEntry{
@@ -163,15 +166,3 @@ func (c *AddCmd) run(cmd *cobra.Command, args []string) error {
163166

164167
return nil
165168
}
166-
167-
// OrderedFlowNames returns the names of allowed flows in order.
168-
func OrderedFlowNames() []string {
169-
flows := slices.Sorted(maps.Keys(config.Flows()))
170-
171-
flowNames := make([]string, len(flows))
172-
for i, f := range flows {
173-
flowNames[i] = string(f)
174-
}
175-
176-
return flowNames
177-
}

cmd/config/plugins/add_test.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -358,10 +358,3 @@ func TestAddCmd_RequiredFalseExplicitlySet(t *testing.T) {
358358
require.NotNil(t, authPlugins[0].Required)
359359
require.False(t, *authPlugins[0].Required)
360360
}
361-
362-
func TestAddCmd_OrderedFlowNames(t *testing.T) {
363-
flows := OrderedFlowNames()
364-
require.Len(t, flows, 2)
365-
require.Equal(t, "request", flows[0])
366-
require.Equal(t, "response", flows[1])
367-
}

internal/config/plugin_config.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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: 15 additions & 8 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

@@ -872,12 +882,9 @@ func TestParseFlows(t *testing.T) {
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)