Fix bug where you weren't able to use both the -q and -j flags at the same time (#227)
All checks were successful
Stage / build (push) Successful in 9s
Stage / rsync (push) Successful in 6s

- Fix bug where if you supplied both `-q`/`--query-metadata` and `-j`/`--json` the query metadata wasn't added to the output
- Update tests

#181

Reviewed-on: #227
Reviewed-by: VylpesTester <tester@vylpes.com>
Co-authored-by: Ethan Lane <ethan@vylpes.com>
Co-committed-by: Ethan Lane <ethan@vylpes.com>
This commit is contained in:
Ethan Lane 2024-12-07 22:21:43 +00:00 committed by Vylpes
parent 3fe8a76534
commit 3b2da358b3
3 changed files with 70 additions and 39 deletions

View file

@ -5,36 +5,28 @@ export default class OutputHelper {
public static GenerateOutput(response: IReturnResult, options: ICliOptions): string {
const result = response.Result!;
const outputLines: string[] = [];
let outputObject = {};
outputObject = { ...result };
if (options.queryMetadata) {
outputObject = { ...outputObject, ...response.Query }
}
if (options.json) {
return JSON.stringify(result);
return JSON.stringify(outputObject);
}
outputLines.push(`Archived = ${result.Archived}`);
outputLines.push(`Author = ${result.Author}`);
outputLines.push(`Downvotes = ${result.Downs}`);
return this.GetFriendlyObjectText(outputObject);
}
if (result.Gallery.length > 1) {
outputLines.push(`Gallery = ${result.Gallery.join(", ")}`);
private static GetFriendlyObjectText<T>(object: T): string {
const output: string[] = [];
for (const key in object) {
output.push(`${key} = ${object[key]}`);
}
outputLines.push(`Hidden = ${result.Hidden}`);
outputLines.push(`Permalink = ${result.Permalink}`);
outputLines.push(`Subreddit = ${result.Subreddit}`);
outputLines.push(`Subreddit Subscribers = ${result.SubredditSubscribers}`);
outputLines.push(`Title = ${result.Title}`);
outputLines.push(`Upvotes = ${result.Ups}`);
outputLines.push(`Url = ${result.Url}`);
if (options.queryMetadata != null) {
outputLines.push(`Query.Subreddit = ${response.Query.subreddit}`);
outputLines.push(`Query.Sort By = ${response.Query.sortBy}`);
outputLines.push(`Query.Limit = ${response.Query.limit}`);
}
return outputLines.join("\n");
return output.join("\n");
}
}