UNPKG

hhurley

Version:

Tool to find lost security patches for Linux distributions.

137 lines (99 loc) 3.74 kB
#!/usr/bin/env node /* Copyright IBM Research Emergent Solutions Jesús Pérez <jesusprubio@gmail.com> This code may only be used under the MIT license found at https://opensource.org/licenses/MIT. */ 'use strict'; const path = require('path'); const program = require('commander'); const Hhurley = require('../'); const utils = require('../lib/utils'); const logger = require('./logger'); const pkgJson = require('../package'); const dbg = utils.dbg(__filename); // Defaults. let forceDown = false; let pathPatches = path.resolve(process.cwd(), '.'); let pathReport = path.resolve(pathPatches, 'report.json'); // TODO: Increment this. let concurrency = 100; const patchesInfo = []; dbg('Starting the CLI ...'); program .version(pkgJson.version) .option('-pp, --pathPatches', `path for the downloaded patches (default: ${pathPatches})`) .option('-pr, --pathReport', `path for the final report (default: ${pathReport})`) .option('-c, --concurrency', 'number of max. simultaneous' + ` file downloads (default: ${concurrency})`) .parse(process.argv); dbg('Checking the parameters ...'); if (program.forceDown) { forceDown = true; } if (program.pathPatches) { pathPatches = path.resolve(process.cwd(), program.pathPatches); } if (program.pathReport) { pathReport = path.resolve(process.cwd(), program.pathReport); } if (program.concurrency) { concurrency = program.concurrency; } dbg('Arguments parsed:', { forceDown, pathPatches, pathReport }); logger.info(`${logger.emoji('computer')} Loading ...\n`); const hhurley = new Hhurley({ forceDown }); logger.title(`\n\tPatch finder ${logger.emoji('eyes')}`); logger.info(`\t(v${hhurley.version})`); dbg('Setting the events, to get partial results ...'); hhurley.on( 'file:downloading', () => logger.info(`\n${logger.emoji('hourglass')} Donwloading the` + 'base file, please wait a bit ...') ); hhurley.on( 'file:downloaded', () => logger.info(`\n${logger.emoji('ok_hand')} Base file downloaded correctly.`) ); hhurley.on('new', (info) => { patchesInfo.push(info); logger.bold(`\n${logger.emoji('punch')} Patch found:`); logger.json(info); }); hhurley.on('end', () => { logger.info(`\n\n${logger.emoji('sparkles')} Finished, patches: ...`); logger.json(patchesInfo); logger.info(`\n\n${logger.emoji('arrow_double_down')} ` + `Downloading the patches to "${pathPatches}" ... `); const mapper = info => utils.downFile(info.patchUrl, path.resolve(pathPatches, `${info.id}.patch`)); utils.pMap(patchesInfo, mapper, { concurrency }) .then(() => { logger.info(`\n${logger.emoji('grinning')} All patches correctly downloaded`); logger.info(`\n${logger.emoji('pencil2')} Writing report to "${pathReport}" ... `); utils.writeFile(pathReport, JSON.stringify(patchesInfo)) .then(() => { logger.info(`\n${logger.emoji('thumbsup')} Report correctly written ...`); logger.bold(`\n${logger.emoji('wave')} Done, see you!\n`); process.exit(0); }) .catch((err) => { logger.error('Writing the report to a file'); logger.error(err); process.exit(1); }); }) .catch((err) => { logger.error('Downloading the files'); logger.error(err); }); }); hhurley.on('error', (err) => { logger.error('Searching'); logger.error(err); process.exit(1); }); logger.bold(`\n${logger.emoji('mag')} Starting the search ...`); hhurley.search(); // Just in case. process.on('uncaughtException', (err) => { logger.error('"uncaughtException" found:'); logger.error(err); }); process.on( 'unhandledRejection', reason => logger.error(`"unhandledRejection : reason : ${reason}`) );