From 9b0f9569347d9d0ef523732dc3988fd61d59e6c3 Mon Sep 17 00:00:00 2001 From: Bryan Ashby Date: Mon, 13 Feb 2017 22:51:20 -0700 Subject: [PATCH] * Start work on new oputil format: oputil * Add auto tagging for oputil scan --- core/file_base_area.js | 17 ++++++--- oputil.js | 78 ++++++++++++++++++++++++++++++------------ 2 files changed, 70 insertions(+), 25 deletions(-) diff --git a/core/file_base_area.js b/core/file_base_area.js index 69e4d4cf..0e05b497 100644 --- a/core/file_base_area.js +++ b/core/file_base_area.js @@ -584,10 +584,14 @@ function scanFile(filePath, options, iterator, cb) { ); } -function scanFileAreaForChanges(areaInfo, iterator, cb) { - if(!cb && _.isFunction(iterator)) { - cb = iterator; - iterator = null; +function scanFileAreaForChanges(areaInfo, options, iterator, cb) { + if(3 === arguments.length && _.isFunction(iterator)) { + cb = iterator; + iterator = null; + } else if(2 === arguments.length && _.isFunction(options)) { + cb = options; + iterator = null; + options = {}; } const storageLocations = getAreaStorageLocations(areaInfo); @@ -632,6 +636,11 @@ function scanFileAreaForChanges(areaInfo, iterator, cb) { if(dupeEntries.length > 0) { // :TODO: Handle duplidates -- what to do here??? } else { + if(Array.isArray(options.tags)) { + options.tags.forEach(tag => { + fileEntry.hashTags.add(tag); + }); + } addNewFileEntry(fileEntry, fullPath, err => { // pass along error; we failed to insert a record in our DB or something else bad return nextFile(err); diff --git a/oputil.js b/oputil.js index c93ccc0c..c14f04fb 100755 --- a/oputil.js +++ b/oputil.js @@ -35,10 +35,11 @@ const USAGE_HELP = { global args: --config PATH : specify config path (${getDefaultConfigPath()}) -commands: +where is one of: user : user utilities config : config file management - file-base : file base management + file-base + fb : file base management `, User : @@ -59,10 +60,14 @@ valid args: --new : generate a new/initial configuration `, FileBase : -`usage: oputil.js file-base +`usage: oputil.js file-base [] [] -valid args: - --scan AREA_TAG : (re)scan area specified by AREA_TAG for new files +where is one of: + scan AREA_TAG : (re)scan area specified by AREA_TAG for new files + multiple area tags can be specified in form of AREA_TAG1 AREA_TAG2 ... + +scan args: + --tags TAG1,TAG2,... : specify tag(s) to assign to discovered entries ` }; @@ -375,7 +380,7 @@ function askNewConfigQuestions(cb) { config.messageConferences.another_sample_conf = { name : 'Another Sample Conference', desc : 'Another conf sample. Change me!', - + areas : { another_sample_area : { name : 'Another Sample Area', @@ -438,7 +443,41 @@ function handleConfigCommand() { } } +function scanFileBaseArea(areaTag, options, iterator, cb) { + async.waterfall( + [ + function getFileArea(callback) { + const fileAreaMod = require('./core/file_base_area.js'); + + const areaInfo = fileAreaMod.getFileAreaByTag(areaTag); + if(!areaInfo) { + return callback(new Error(`Invalid file base area tag: ${areaTag}`)); + } + + return callback(null, fileAreaMod, areaInfo); + }, + function performScan(fileAreaMod, areaInfo, callback) { + fileAreaMod.scanFileAreaForChanges(areaInfo, options, iterator, err => { + return callback(err); + }); + } + ], + err => { + return cb(err); + } + ); +} + function fileAreaScan() { + const options = {}; + + const tags = argv.tags; + if(tags) { + options.tags = tags.split(','); + } + + const areaTags = argv._.slice(2); + function scanFileIterator(stepInfo, nextScanStep) { if('start' === stepInfo.step) { console.info(`Scanning ${stepInfo.filePath}...`); @@ -447,23 +486,17 @@ function fileAreaScan() { // :TODO: add 'finished' step when avail } - async.waterfall( + async.series( [ function init(callback) { return initConfigAndDatabases(callback); }, - function getFileArea(callback) { - const fileAreaMod = require('./core/file_base_area.js'); - - const areaInfo = fileAreaMod.getFileAreaByTag(argv.scan); - if(!areaInfo) { - return callback(new Error('Invalid file area')); - } - - return callback(null, fileAreaMod, areaInfo); - }, - function performScan(fileAreaMod, areaInfo, callback) { - fileAreaMod.scanFileAreaForChanges(areaInfo, scanFileIterator, err => { + function scanAreas(callback) { + async.eachSeries(areaTags, (areaTag, nextAreaTag) => { + scanFileBaseArea(areaTag, options, scanFileIterator, err => { + return nextAreaTag(err); + }); + }, err => { return callback(err); }); } @@ -482,8 +515,10 @@ function handleFileBaseCommand() { return printUsageAndSetExitCode('FileBase', ExitCodes.ERROR); } - if(argv.scan) { - return fileAreaScan(argv.scan); + const action = argv._[1]; + + switch(action) { + case 'scan' : return fileAreaScan(); } } @@ -511,6 +546,7 @@ function main() { break; case 'file-base' : + case 'fb' : handleFileBaseCommand(); break;