2020-03-23 00:32:57 +00:00
|
|
|
#!/usr/bin/env node
|
2020-03-22 22:09:33 +00:00
|
|
|
import Telme from './telme';
|
|
|
|
import { ArgParser, ERunMode, ISimpleMessageOptions, ITaskOptions, IInitProfileOptions } from './utils';
|
2020-03-25 22:40:18 +00:00
|
|
|
import Config, { ITaskConfig } from './config/config';
|
2020-03-22 22:09:33 +00:00
|
|
|
import Init from './flows/init'
|
|
|
|
const { version } = require('../package.json');
|
|
|
|
|
|
|
|
async function main() {
|
|
|
|
const parsed = ArgParser.parse(process.argv);
|
2020-03-25 22:40:18 +00:00
|
|
|
Config.cliInit();
|
|
|
|
let config: ITaskConfig;
|
2020-03-22 22:09:33 +00:00
|
|
|
let options: any;
|
|
|
|
switch (parsed.mode) {
|
|
|
|
case ERunMode.VERSION:
|
|
|
|
console.log(`[${Config.APP_NAME}] version ${version}`);
|
|
|
|
break;
|
|
|
|
case ERunMode.HELP:
|
|
|
|
printHelp();
|
|
|
|
break;
|
|
|
|
case ERunMode.INIT:
|
|
|
|
await Init.init(parsed.mode_data);
|
|
|
|
break;
|
|
|
|
case ERunMode.SIMPLE_MESSAGE:
|
|
|
|
config = await Config.getConfig(parsed.mode_data.profileName);
|
|
|
|
options = parsed.mode_data as ISimpleMessageOptions;
|
2020-03-25 22:40:18 +00:00
|
|
|
await Telme.sendMessage(config, options.message);
|
2020-03-22 22:09:33 +00:00
|
|
|
console.log(`[${Config.APP_NAME}]: Told Ya!`);
|
|
|
|
break;
|
|
|
|
case ERunMode.TASK_MESSAGE:
|
|
|
|
config = await Config.getConfig(parsed.mode_data.profileName);
|
|
|
|
options = parsed.mode_data as ITaskOptions;
|
2020-03-25 22:40:18 +00:00
|
|
|
await Telme.runTask(config, options);
|
2020-03-22 22:09:33 +00:00
|
|
|
console.log(`[${Config.APP_NAME}]: Told Ya!`);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function printHelp() {
|
|
|
|
const cliFlags = ArgParser.CLI_OPTIONS;
|
|
|
|
const message =
|
|
|
|
`${Config.APP_NAME} v${version} - A CLI Telegram message tool
|
|
|
|
|
|
|
|
[Usage]: $ ${Config.APP_NAME} <telme_options> <?command> <arguments>
|
|
|
|
|
|
|
|
Options:
|
|
|
|
\t ${cliFlags.versionFlags.join(', ')} \t\t Print ${Config.APP_NAME} version.
|
|
|
|
\t ${cliFlags.helpFlags.join(', ')} \t\t This help page.
|
|
|
|
\t ${cliFlags.profileFlags.join(', ')} \t\t Specify a profile to use. This is optional. defaults to 'default' profile.
|
|
|
|
\t ${cliFlags.initFlags.join(', ')} \t\t Will generate a config file for a given profile (defaults to 'default' profile).
|
|
|
|
\t ${cliFlags.messageFlags.join(', ')} \t\t Send a simple message.
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
init:
|
2020-03-25 22:40:18 +00:00
|
|
|
\t '${Config.APP_NAME} --init' - init a default profile
|
|
|
|
\t '${Config.APP_NAME} -i -p <profile-name>' - init a 'named' profile
|
2020-03-22 22:09:33 +00:00
|
|
|
tasks:
|
2020-03-25 22:40:18 +00:00
|
|
|
\t '${Config.APP_NAME} docker-compose pull' - Send a message to default profile once the command 'docker-compose pull' is done
|
|
|
|
\t '${Config.APP_NAME} -p <profile-name> docker-compose pull' - Send a message to <profile-name> profile once the command 'docker-compose pull' is done
|
2020-03-22 22:09:33 +00:00
|
|
|
|
|
|
|
messages:
|
2020-03-25 22:40:18 +00:00
|
|
|
\t '${Config.APP_NAME} -m "text to send"' - Send a message to default profile
|
|
|
|
\t '${Config.APP_NAME} -p <profile-name> -m "text to send"' - Send message to <profile-name> profile
|
2020-03-22 22:09:33 +00:00
|
|
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
console.log(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
main().then(_ => {
|
|
|
|
process.exit(0);
|
|
|
|
}).catch(error => {
|
|
|
|
const exitCode = error.exitCode || 1;
|
|
|
|
console.error(`[${Config.APP_NAME}] ERROR: ${error.message}`);
|
|
|
|
console.log(`[${Config.APP_NAME}] For help run '$ ${Config.APP_NAME} -h'`);
|
|
|
|
process.exit(exitCode);
|
|
|
|
});
|