2023-12-08 17:10:00 +00:00
|
|
|
import { Command, Option } from "commander";
|
2023-12-01 18:15:18 +00:00
|
|
|
import randomBunny from "./index";
|
|
|
|
import ICliOptions from "./contracts/ICliOptions";
|
|
|
|
import { exit } from "process";
|
|
|
|
|
2023-10-27 15:53:12 +01:00
|
|
|
const program = new Command();
|
|
|
|
|
|
|
|
program
|
|
|
|
.name('random-bunny')
|
|
|
|
.description('Get a random image url from a subreddit of your choosing')
|
2023-12-01 18:15:18 +00:00
|
|
|
.version('2.2')
|
2023-12-08 17:10:00 +00:00
|
|
|
.option('-s, --subreddit <subreddit>', 'The subreddit to search', 'rabbits')
|
2024-01-19 18:02:16 +00:00
|
|
|
.option('-j, --json', 'Output as JSON')
|
2023-12-08 17:10:00 +00:00
|
|
|
.option('-q, --query-metadata', 'Include query metadata in result')
|
|
|
|
.addOption(new Option('--sort <sort>', 'Sort by').default('hot').choices(['hot', 'new', 'top']));
|
2023-12-01 18:15:18 +00:00
|
|
|
|
|
|
|
program.parse();
|
|
|
|
|
|
|
|
const options: ICliOptions = program.opts();
|
|
|
|
|
2023-12-08 17:10:00 +00:00
|
|
|
randomBunny(options.subreddit, options.sort)
|
2023-12-01 18:15:18 +00:00
|
|
|
.then((response) => {
|
|
|
|
if (response.IsSuccess) {
|
|
|
|
const result = response.Result!;
|
|
|
|
|
|
|
|
const outputLines: string[] = [];
|
2024-01-19 18:02:16 +00:00
|
|
|
|
|
|
|
if (options.json) {
|
|
|
|
console.log(JSON.stringify(result));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-12-01 18:15:18 +00:00
|
|
|
outputLines.push(`Archived = ${result.Archived}`);
|
|
|
|
outputLines.push(`Downvotes = ${result.Downs}`);
|
|
|
|
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}`);
|
|
|
|
|
2023-12-08 17:10:00 +00:00
|
|
|
if (options.queryMetadata != null) {
|
|
|
|
outputLines.push(`Query.Subreddit = ${response.Query.subreddit}`);
|
|
|
|
outputLines.push(`Query.Sort By = ${response.Query.sortBy}`);
|
|
|
|
}
|
|
|
|
|
2023-12-01 18:15:18 +00:00
|
|
|
console.log(outputLines.join("\n"));
|
|
|
|
exit(0);
|
|
|
|
} else {
|
|
|
|
const error = response.Error!;
|
2023-10-27 15:53:12 +01:00
|
|
|
|
2023-12-01 18:15:18 +00:00
|
|
|
console.error(error.Message, error.Code);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
});
|