Skip to content

Commit 91667ad

Browse files
committed
fix(cli): handle heredoc stdin input for cip query
1 parent 700cf87 commit 91667ad

File tree

2 files changed

+24
-13
lines changed

2 files changed

+24
-13
lines changed

packages/b2c-cli/src/commands/cip/query.ts

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -80,22 +80,27 @@ export default class CipQuery extends CipCommand<typeof CipQuery> {
8080

8181
private resolveSql(): string {
8282
const positionalSql = this.args.sql;
83-
const sources = [Boolean(positionalSql), Boolean(this.flags.file), Boolean(this.flags.stdin)].filter(
84-
Boolean,
85-
).length;
83+
const hasPositional = Boolean(positionalSql);
84+
const hasFile = Boolean(this.flags.file);
85+
const hasStdin = this.flags.stdin === true;
8686

87-
if (sources === 0) {
88-
this.error('No SQL provided. Pass SQL as an argument, or use --file / --stdin.');
87+
if (hasStdin && hasFile) {
88+
this.error('Use either --stdin or --file, not both.');
89+
}
90+
91+
if (!hasStdin && hasFile && hasPositional) {
92+
this.error('Use either a positional SQL argument or --file, not both.');
8993
}
9094

91-
if (sources > 1) {
92-
this.error('Provide SQL from exactly one source: positional argument, --file, or --stdin.');
95+
if (!hasStdin && !hasFile && !hasPositional) {
96+
this.error('No SQL provided. Pass SQL as an argument, or use --file / --stdin.');
9397
}
9498

95-
const rawSql =
96-
positionalSql ??
97-
(this.flags.file ? fs.readFileSync(this.flags.file, 'utf8') : undefined) ??
98-
(this.flags.stdin ? fs.readFileSync(0, 'utf8') : undefined);
99+
const rawSql = hasStdin
100+
? fs.readFileSync(0, 'utf8')
101+
: hasFile
102+
? fs.readFileSync(this.flags.file as string, 'utf8')
103+
: positionalSql;
99104

100105
if (!rawSql || rawSql.trim().length === 0) {
101106
this.error('SQL input is empty.');

packages/b2c-cli/test/commands/cip/query.test.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,15 @@ describe('cip query', () => {
2626
expect(error?.message).to.include('No SQL provided');
2727
});
2828

29-
it('rejects multiple SQL sources', async () => {
29+
it('rejects conflicting file and stdin sources', async () => {
30+
const {error} = await runCommand('cip query --stdin --file ./query.sql');
31+
expect(error).to.not.be.undefined;
32+
expect(error?.message).to.include('either --stdin or --file');
33+
});
34+
35+
it('prioritizes --stdin over positional SQL source', async () => {
3036
const {error} = await runCommand('cip query "SELECT 1" --stdin');
3137
expect(error).to.not.be.undefined;
32-
expect(error?.message).to.include('exactly one source');
38+
expect(error?.message).to.include('SQL input is empty');
3339
});
3440
});

0 commit comments

Comments
 (0)