Skip to content

Commit 37e660e

Browse files
committed
Output yaml in cli by default
1 parent 70a8967 commit 37e660e

File tree

2 files changed

+44
-21
lines changed

2 files changed

+44
-21
lines changed

cli/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,23 @@ pgai issues post_comment <issueId> <content> # Post a comment to an issue
129129
# Options:
130130
# --parent <uuid> Parent comment ID (for replies)
131131
# --debug Enable debug output
132+
# --json Output raw JSON (overrides default YAML)
133+
```
134+
135+
#### Output format for issues commands
136+
137+
By default, issues commands print human-friendly YAML when writing to a terminal. For scripting, you can:
138+
139+
- Use `--json` to force JSON output:
140+
141+
```bash
142+
pgai issues list --json | jq '.[] | {id, title}'
143+
```
144+
145+
- Rely on auto-detection: when stdout is not a TTY (e.g., piped or redirected), output is JSON automatically:
146+
147+
```bash
148+
pgai issues comments <issueId> > comments.json
132149
```
133150

134151
#### Grafana management

cli/bin/postgres-ai.ts

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,24 @@ function getConfig(opts: CliOptions): ConfigResult {
9090
return { apiKey };
9191
}
9292

93+
// Human-friendly output helper: YAML for TTY by default, JSON when --json or non-TTY
94+
function printResult(result: unknown, json?: boolean): void {
95+
if (typeof result === "string") {
96+
process.stdout.write(result);
97+
if (!/\n$/.test(result)) console.log();
98+
return;
99+
}
100+
if (json || !process.stdout.isTTY) {
101+
console.log(JSON.stringify(result, null, 2));
102+
} else {
103+
let text = yaml.dump(result as any);
104+
if (Array.isArray(result)) {
105+
text = text.replace(/\n- /g, "\n\n- ");
106+
}
107+
console.log(text);
108+
}
109+
}
110+
93111
const program = new Command();
94112

95113
program
@@ -1192,7 +1210,8 @@ issues
11921210
.command("list")
11931211
.description("list issues")
11941212
.option("--debug", "enable debug output")
1195-
.action(async (opts: { debug?: boolean }) => {
1213+
.option("--json", "output raw JSON")
1214+
.action(async (opts: { debug?: boolean; json?: boolean }) => {
11961215
try {
11971216
const rootOpts = program.opts<CliOptions>();
11981217
const cfg = config.readConfig();
@@ -1206,12 +1225,7 @@ issues
12061225
const { apiBaseUrl } = resolveBaseUrls(rootOpts, cfg);
12071226

12081227
const result = await fetchIssues({ apiKey, apiBaseUrl, debug: !!opts.debug });
1209-
if (typeof result === "string") {
1210-
process.stdout.write(result);
1211-
if (!/\n$/.test(result)) console.log();
1212-
} else {
1213-
console.log(JSON.stringify(result, null, 2));
1214-
}
1228+
printResult(result, opts.json);
12151229
} catch (err) {
12161230
const message = err instanceof Error ? err.message : String(err);
12171231
console.error(message);
@@ -1223,7 +1237,8 @@ issues
12231237
.command("comments <issueId>")
12241238
.description("list comments for an issue")
12251239
.option("--debug", "enable debug output")
1226-
.action(async (issueId: string, opts: { debug?: boolean }) => {
1240+
.option("--json", "output raw JSON")
1241+
.action(async (issueId: string, opts: { debug?: boolean; json?: boolean }) => {
12271242
try {
12281243
const rootOpts = program.opts<CliOptions>();
12291244
const cfg = config.readConfig();
@@ -1237,12 +1252,7 @@ issues
12371252
const { apiBaseUrl } = resolveBaseUrls(rootOpts, cfg);
12381253

12391254
const result = await fetchIssueComments({ apiKey, apiBaseUrl, issueId, debug: !!opts.debug });
1240-
if (typeof result === "string") {
1241-
process.stdout.write(result);
1242-
if (!/\n$/.test(result)) console.log();
1243-
} else {
1244-
console.log(JSON.stringify(result, null, 2));
1245-
}
1255+
printResult(result, opts.json);
12461256
} catch (err) {
12471257
const message = err instanceof Error ? err.message : String(err);
12481258
console.error(message);
@@ -1255,7 +1265,8 @@ issues
12551265
.description("post a new comment to an issue")
12561266
.option("--parent <uuid>", "parent comment id")
12571267
.option("--debug", "enable debug output")
1258-
.action(async (issueId: string, content: string, opts: { parent?: string; debug?: boolean }) => {
1268+
.option("--json", "output raw JSON")
1269+
.action(async (issueId: string, content: string, opts: { parent?: string; debug?: boolean; json?: boolean }) => {
12591270
try {
12601271
// Interpret escape sequences in content (e.g., \n -> newline)
12611272
if (opts.debug) {
@@ -1287,12 +1298,7 @@ issues
12871298
parentCommentId: opts.parent,
12881299
debug: !!opts.debug,
12891300
});
1290-
if (typeof result === "string") {
1291-
process.stdout.write(result);
1292-
if (!/\n$/.test(result)) console.log();
1293-
} else {
1294-
console.log(JSON.stringify(result, null, 2));
1295-
}
1301+
printResult(result, opts.json);
12961302
} catch (err) {
12971303
const message = err instanceof Error ? err.message : String(err);
12981304
console.error(message);

0 commit comments

Comments
 (0)