trustgraph/ts/packages/cli/src/commands/config.ts

117 lines
3.7 KiB
TypeScript
Raw Normal View History

2026-04-05 21:09:33 -05:00
/**
* Config CLI commands.
*
* Python reference: trustgraph-cli/trustgraph/cli/show_config.py etc.
*/
import type { Command } from "commander";
2026-06-02 00:22:04 -05:00
import { Effect } from "effect";
import { cliCommandError, withSocket, writeJson } from "./util.js";
2026-04-05 21:09:33 -05:00
export function registerConfigCommands(program: Command): void {
const config = program
.command("config")
.description("Configuration management");
config
.command("show")
.description("Show current configuration")
2026-06-02 00:22:04 -05:00
.action((_opts, cmd) =>
Effect.runPromise(withSocket(cmd, (socket) =>
Effect.gen(function* () {
2026-04-05 22:44:45 -05:00
const cfg = socket.config();
2026-06-02 00:22:04 -05:00
const resp = yield* Effect.tryPromise({
try: () => cfg.getConfigAll(),
catch: (error) => cliCommandError("config.show", error),
});
yield* writeJson(resp);
}),
)),
);
2026-04-05 21:09:33 -05:00
config
.command("get")
.description("Get a configuration value")
2026-04-05 22:44:45 -05:00
.argument("<key>", "Config key (format: type/key)")
2026-06-02 00:22:04 -05:00
.action((key: string, _opts, cmd) =>
Effect.runPromise(withSocket(cmd, (socket) =>
Effect.gen(function* () {
2026-04-05 22:44:45 -05:00
const cfg = socket.config();
// Support "type/key" format; fall back to using the whole string as key
const parts = key.split("/");
const configKey =
parts.length >= 2
? { type: parts[0], key: parts.slice(1).join("/") }
: { type: "config", key };
2026-06-02 00:22:04 -05:00
const resp = yield* Effect.tryPromise({
try: () => cfg.getConfig([configKey]),
catch: (error) => cliCommandError("config.get", error),
});
yield* writeJson(resp);
}),
)),
);
2026-04-05 21:09:33 -05:00
config
.command("set")
.description("Set a configuration value")
2026-04-05 22:44:45 -05:00
.argument("<key>", "Config key (format: type/key)")
2026-04-05 21:09:33 -05:00
.argument("<value>", "Config value (JSON)")
2026-06-02 00:22:04 -05:00
.action((key: string, value: string, _opts, cmd) =>
Effect.runPromise(withSocket(cmd, (socket) =>
Effect.gen(function* () {
2026-04-05 22:44:45 -05:00
const cfg = socket.config();
const parts = key.split("/");
const configEntry =
parts.length >= 2
? { type: parts[0], key: parts.slice(1).join("/"), value }
: { type: "config", key, value };
2026-06-02 00:22:04 -05:00
const resp = yield* Effect.tryPromise({
try: () => cfg.putConfig([configEntry]),
catch: (error) => cliCommandError("config.set", error),
});
yield* writeJson(resp);
}),
)),
);
2026-04-05 21:09:33 -05:00
config
.command("list")
2026-04-05 22:44:45 -05:00
.description("List configuration keys for a type")
.argument("[type]", "Config type to list", "config")
2026-06-02 00:22:04 -05:00
.action((type: string, _opts, cmd) =>
Effect.runPromise(withSocket(cmd, (socket) =>
Effect.gen(function* () {
2026-04-05 22:44:45 -05:00
const cfg = socket.config();
2026-06-02 00:22:04 -05:00
const resp = yield* Effect.tryPromise({
try: () => cfg.list(type),
catch: (error) => cliCommandError("config.list", error),
});
yield* writeJson(resp);
}),
)),
);
2026-04-05 22:44:45 -05:00
config
.command("delete")
.description("Delete a configuration entry")
.argument("<key>", "Config key (format: type/key)")
2026-06-02 00:22:04 -05:00
.action((key: string, _opts, cmd) =>
Effect.runPromise(withSocket(cmd, (socket) =>
Effect.gen(function* () {
2026-04-05 22:44:45 -05:00
const cfg = socket.config();
const parts = key.split("/");
const configKey =
parts.length >= 2
? { type: parts[0], key: parts.slice(1).join("/") }
: { type: "config", key };
2026-06-02 00:22:04 -05:00
const resp = yield* Effect.tryPromise({
try: () => cfg.deleteConfig(configKey),
catch: (error) => cliCommandError("config.delete", error),
});
yield* writeJson(resp);
}),
)),
);
2026-04-05 21:09:33 -05:00
}