Skip to content

Commit 540eb67

Browse files
committed
fix: refactor parsing agent type
1 parent 672742a commit 540eb67

File tree

1 file changed

+33
-37
lines changed

1 file changed

+33
-37
lines changed

cmd/server/server.go

Lines changed: 33 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"log/slog"
88
"net/http"
99
"os"
10+
"sort"
1011
"strings"
1112

1213
"github.com/spf13/cobra"
@@ -38,45 +39,31 @@ const (
3839
AgentTypeCustom AgentType = msgfmt.AgentTypeCustom
3940
)
4041

42+
// exhaustiveness of this map is by the exhaustive linter
43+
var agentTypeMap = map[AgentType]bool{
44+
AgentTypeClaude: true,
45+
AgentTypeGoose: true,
46+
AgentTypeAider: true,
47+
AgentTypeCodex: true,
48+
AgentTypeGemini: true,
49+
AgentTypeCustom: true,
50+
}
51+
4152
func parseAgentType(firstArg string, agentTypeVar string) (AgentType, error) {
42-
var agentType AgentType
43-
switch agentTypeVar {
44-
case string(AgentTypeClaude):
45-
agentType = AgentTypeClaude
46-
case string(AgentTypeGoose):
47-
agentType = AgentTypeGoose
48-
case string(AgentTypeAider):
49-
agentType = AgentTypeAider
50-
case string(AgentTypeGemini):
51-
agentType = AgentTypeGemini
52-
case string(AgentTypeCustom):
53-
agentType = AgentTypeCustom
54-
case string(AgentTypeCodex):
55-
agentType = AgentTypeCodex
56-
case "":
57-
// do nothing
58-
default:
59-
return "", fmt.Errorf("invalid agent type: %s", agentTypeVar)
53+
// if the agent type is provided, use it
54+
castedAgentType := AgentType(agentTypeVar)
55+
if _, ok := agentTypeMap[castedAgentType]; ok {
56+
return castedAgentType, nil
6057
}
61-
if agentType != "" {
62-
return agentType, nil
58+
if agentTypeVar != "" {
59+
return AgentTypeCustom, fmt.Errorf("invalid agent type: %s", agentTypeVar)
6360
}
64-
65-
switch firstArg {
66-
case string(AgentTypeClaude):
67-
agentType = AgentTypeClaude
68-
case string(AgentTypeGoose):
69-
agentType = AgentTypeGoose
70-
case string(AgentTypeAider):
71-
agentType = AgentTypeAider
72-
case string(AgentTypeCodex):
73-
agentType = AgentTypeCodex
74-
case string(AgentTypeGemini):
75-
agentType = AgentTypeGemini
76-
default:
77-
agentType = AgentTypeCustom
61+
// if the agent type is not provided, guess it from the first argument
62+
castedFirstArg := AgentType(firstArg)
63+
if _, ok := agentTypeMap[castedFirstArg]; ok {
64+
return castedFirstArg, nil
7865
}
79-
return agentType, nil
66+
return AgentTypeCustom, nil
8067
}
8168

8269
func runServer(ctx context.Context, logger *slog.Logger, argsToPass []string) error {
@@ -139,10 +126,19 @@ func runServer(ctx context.Context, logger *slog.Logger, argsToPass []string) er
139126
return nil
140127
}
141128

129+
var agentNames = (func() []string {
130+
names := make([]string, 0, len(agentTypeMap))
131+
for agentType := range agentTypeMap {
132+
names = append(names, string(agentType))
133+
}
134+
sort.Strings(names)
135+
return names
136+
})()
137+
142138
var ServerCmd = &cobra.Command{
143139
Use: "server [agent]",
144140
Short: "Run the server",
145-
Long: `Run the server with the specified agent (claude, goose, aider, gemini, codex)`,
141+
Long: fmt.Sprintf("Run the server with the specified agent (one of: %s)", strings.Join(agentNames, ", ")),
146142
Args: cobra.MinimumNArgs(1),
147143
Run: func(cmd *cobra.Command, args []string) {
148144
logger := slog.New(slog.NewTextHandler(os.Stdout, nil))
@@ -155,7 +151,7 @@ var ServerCmd = &cobra.Command{
155151
}
156152

157153
func init() {
158-
ServerCmd.Flags().StringVarP(&agentTypeVar, "type", "t", "", "Override the agent type (one of: claude, goose, aider, custom)")
154+
ServerCmd.Flags().StringVarP(&agentTypeVar, "type", "t", "", fmt.Sprintf("Override the agent type (one of: %s, custom)", strings.Join(agentNames, ", ")))
159155
ServerCmd.Flags().IntVarP(&port, "port", "p", 3284, "Port to run the server on")
160156
ServerCmd.Flags().BoolVarP(&printOpenAPI, "print-openapi", "P", false, "Print the OpenAPI schema to stdout and exit")
161157
ServerCmd.Flags().StringVarP(&chatBasePath, "chat-base-path", "c", "/chat", "Base path for assets and routes used in the static files of the chat interface")

0 commit comments

Comments
 (0)