diff --git a/package-lock.json b/package-lock.json index ea152093..36aeaed9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -347,9 +347,9 @@ "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==" }, "commander": { - "version": "5.1.0", - "resolved": "https://registry.npmjs.org/commander/-/commander-5.1.0.tgz", - "integrity": "sha512-P0CysNDQ7rtVw4QIQtm+MRxV66vKFSvlsQvGYXZWR3qFU0jlMKHZZZgw8e+8DSah4UDKMqnknRDQz+xuQXQ/Zg==" + "version": "7.0.0", + "resolved": "https://registry.npmjs.org/commander/-/commander-7.0.0.tgz", + "integrity": "sha512-ovx/7NkTrnPuIV8sqk/GjUIIM1+iUQeqA3ye2VNpq9sVoiZsooObWlQy+OPWGI17GDaEoybuAGJm6U8yC077BA==" }, "compare-versions": { "version": "3.6.0", diff --git a/package.json b/package.json index 6d451dad..ed35f89e 100644 --- a/package.json +++ b/package.json @@ -21,7 +21,7 @@ "dependencies": { "bencode": "^2.0.1", "chalk": "^4.1.0", - "commander": "^5.1.0", + "commander": "^7.0.0", "parse-torrent": "^7.1.3", "simple-get": "^4.0.0", "xmlrpc": "^1.3.2" diff --git a/src/cmd.js b/src/cmd.js index 89e5a0cf..46fb7e29 100755 --- a/src/cmd.js +++ b/src/cmd.js @@ -1,6 +1,6 @@ #!/usr/bin/env node -const { program, Command } = require("commander"); +const { program, Command, Option } = require("commander"); const chalk = require("chalk"); const packageDotJson = require("../package.json"); const { main } = require("./index"); @@ -12,16 +12,27 @@ const logger = require("./logger"); require("./signalHandlers"); const { ACTIONS } = require("./constants"); -function fallback(...args) { - for (const arg of args) { - if (arg !== undefined) return arg; - } - return undefined; -} - async function run() { const fileConfig = getFileConfig(); + function fallback(...args) { + for (const arg of args) { + if (arg !== undefined) return arg; + } + return undefined; + } + + function processOptions(options) { + options.trackers = options.trackers.split(",").filter((e) => e !== ""); + if (options.action === "inject" && !options.rtorrentRpcUrl) { + logger.error( + "You need to specify --rtorrent-rpc-url when using '-A inject'." + ); + process.exit(1); + } + return options; + } + function addSharedOptions() { return this.requiredOption( "-u, --jackett-server-url ", @@ -56,12 +67,15 @@ async function run() { "Search for all torrents regardless of their contents", fallback(fileConfig.searchAll, false) ) - .requiredOption("-v, --verbose", "Log verbose output", false) - .requiredOption( - "-A, --action ", - "If set to 'inject', cross-seed will attempt to add the found torrents to your torrent client.", - fallback(fileConfig.action, ACTIONS.SAVE) + .addOption( + new Option( + "-A, --action ", + "If set to 'inject', cross-seed will attempt to add the found torrents to your torrent client." + ) + .default(fallback(fileConfig.action, ACTIONS.SAVE)) + .choices(Object.values(ACTIONS)) + .makeOptionMandatory() ) .option( "--rtorrent-rpc-url ", @@ -102,12 +116,8 @@ async function run() { .command("daemon") .description("Start the cross-serve daemon") .addSharedOptions() - .action(async (command) => { - const options = command.opts(); - options.trackers = options.trackers - .split(",") - .filter((e) => e !== ""); - setRuntimeConfig(options); + .action(async (options, _command) => { + setRuntimeConfig(processOptions(options)); try { if (process.env.DOCKER_ENV === "true") { generateConfig({ docker: true }); @@ -149,12 +159,8 @@ async function run() { "Exclude torrents which have been searched more recently than x minutes ago. Overrides the -a flag.", (n) => parseInt(n) ) - .action(async (command) => { - const options = command.opts(); - options.trackers = options.trackers - .split(",") - .filter((e) => e !== ""); - setRuntimeConfig(options); + .action(async (options, _command) => { + setRuntimeConfig(processOptions(options)); try { await main(); } catch (e) { diff --git a/src/constants.js b/src/constants.js index 84072dee..9338e1ab 100644 --- a/src/constants.js +++ b/src/constants.js @@ -10,8 +10,8 @@ const README_URL = "https://github.com/mmgoodnow/cross-seed"; const DAEMON_MODE_URL_HASH = "#daemon-mode-rtorrent-only-docker-recommended"; const ACTIONS = { - SAVE: "SAVE", - INJECT: "INJECT", + SAVE: "save", + INJECT: "inject", }; // because I'm sick of intellij whining at me