Skip to content
This repository has been archived by the owner on Sep 5, 2024. It is now read-only.

Commit

Permalink
fix: manually read from stdin
Browse files Browse the repository at this point in the history
If you do `flag.allowStdin: true` then oclif will read from stdin for
you if the value is `-`, so there's no way to know if the flag value
came from stdin or the user specified a file path (here we need to read
the file).

I don't like having another specific flag just to read from stdin so I
made this a string flag and handle reading from stdin/file in
`flag.parse`.
  • Loading branch information
cristiand391 committed Jan 26, 2024
1 parent 4c1d3c5 commit e55c119
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions src/commands/org/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { ProxyAgent } from 'proxy-agent';
import { SfCommand, Flags } from '@salesforce/sf-plugins-core';
import { SfError, Org } from '@salesforce/core';
import { Args, ux } from '@oclif/core';
import { fileExists } from '@oclif/core/lib/util/fs.js';
import { readStdin } from '@oclif/core/lib/parser/parse.js';

export class OrgApi extends SfCommand<void> {
public static readonly summary =
Expand Down Expand Up @@ -64,10 +64,19 @@ export class OrgApi extends SfCommand<void> {
summary:
'The file to use as the body for the request (use "-" to read from standard input).',
parse: async (input) => {
if (input === '-') return input;
await fileExists(input);
if (input === '-') {
const body = await readStdin();
if (body) {
return body.trim();
} else {
throw new Error(
'Unable to read body: `-` was provided but STDIN is empty.',
);
}
} else {
return readFile(input, 'utf8');
}
},
allowStdin: true,
helpValue: 'file',
}),
};
Expand Down Expand Up @@ -117,12 +126,7 @@ export class OrgApi extends SfCommand<void> {
}`,
...(flags.header ? OrgApi.getHeaders(flags.header) : {}),
},
body:
flags.method === 'GET'
? undefined
: flags.body
? await readFile(flags.body)
: undefined,
body: flags.method === 'GET' ? undefined : flags.body,
throwHttpErrors: false,
followRedirect: false,
});
Expand Down

0 comments on commit e55c119

Please sign in to comment.