refactor(auto-reply): share slash set/unset command parsing

This commit is contained in:
Peter Steinberger
2026-02-18 17:29:24 +00:00
parent fedebc245e
commit f46bcbe16d
4 changed files with 126 additions and 61 deletions

View File

@@ -1,5 +1,4 @@
import { parseSetUnsetCommandAction } from "./commands-setunset.js";
import { parseSlashCommandOrNull } from "./commands-slash-parse.js";
import { parseSlashCommandWithSetUnset } from "./commands-setunset.js";
export type ConfigCommand =
| { action: "show"; path?: string }
@@ -8,37 +7,19 @@ export type ConfigCommand =
| { action: "error"; message: string };
export function parseConfigCommand(raw: string): ConfigCommand | null {
const parsed = parseSlashCommandOrNull(raw, "/config", {
invalidMessage: "Invalid /config syntax.",
});
if (!parsed) {
return null;
}
if (!parsed.ok) {
return { action: "error", message: parsed.message };
}
const { action, args } = parsed;
const setUnset = parseSetUnsetCommandAction<ConfigCommand>({
return parseSlashCommandWithSetUnset<ConfigCommand>({
raw,
slash: "/config",
action,
args,
invalidMessage: "Invalid /config syntax.",
usageMessage: "Usage: /config show|set|unset",
onKnownAction: (action, args) => {
if (action === "show" || action === "get") {
return { action: "show", path: args || undefined };
}
return undefined;
},
onSet: (path, value) => ({ action: "set", path, value }),
onUnset: (path) => ({ action: "unset", path }),
onError: (message) => ({ action: "error", message }),
});
if (setUnset) {
return setUnset;
}
switch (action) {
case "show":
return { action: "show", path: args || undefined };
case "get":
return { action: "show", path: args || undefined };
default:
return {
action: "error",
message: "Usage: /config show|set|unset",
};
}
}