Add --update option to fb scan

This commit is contained in:
Bryan Ashby 2017-09-08 23:11:01 -06:00
parent 79e410315c
commit 18461e594a
2 changed files with 65 additions and 21 deletions

View File

@ -34,7 +34,7 @@ exports.handleFileBaseCommand = handleFileBaseCommand;
let fileArea; // required during init let fileArea; // required during init
function finalizeEntryAndPersist(fileEntry, descHandler, cb) { function finalizeEntryAndPersist(isUpdate, fileEntry, descHandler, cb) {
async.series( async.series(
[ [
function getDescFromHandlerIfNeeded(callback) { function getDescFromHandlerIfNeeded(callback) {
@ -53,18 +53,24 @@ function finalizeEntryAndPersist(fileEntry, descHandler, cb) {
return callback(null); return callback(null);
}, },
function getDescFromUserIfNeeded(callback) { function getDescFromUserIfNeeded(callback) {
if(false === argv.prompt || ( fileEntry.desc && fileEntry.desc.length > 0 ) ) { if(fileEntry.desc && fileEntry.desc.length > 0 ) {
return callback(null); return callback(null);
} }
const getDescFromFileName = require('../../core/file_base_area.js').getDescFromFileName; const getDescFromFileName = require('../../core/file_base_area.js').getDescFromFileName;
const descFromFile = getDescFromFileName(fileEntry.fileName);
if(false === argv.prompt) {
fileEntry.desc = descFromFile;
return callback(null);
}
const questions = [ const questions = [
{ {
name : 'desc', name : 'desc',
message : `Description for ${fileEntry.fileName}:`, message : `Description for ${fileEntry.fileName}:`,
type : 'input', type : 'input',
default : getDescFromFileName(fileEntry.fileName), default : descFromFile,
} }
]; ];
@ -74,7 +80,7 @@ function finalizeEntryAndPersist(fileEntry, descHandler, cb) {
}); });
}, },
function persist(callback) { function persist(callback) {
fileEntry.persist( err => { fileEntry.persist(isUpdate, err => {
return callback(err); return callback(err);
}); });
} }
@ -105,6 +111,12 @@ function scanFileAreaForChanges(areaInfo, options, cb) {
}); });
}); });
function updateTags(fe) {
if(Array.isArray(options.tags)) {
fe.hashTags = new Set(options.tags);
}
}
async.eachSeries(storageLocations, (storageLoc, nextLocation) => { async.eachSeries(storageLocations, (storageLoc, nextLocation) => {
async.waterfall( async.waterfall(
[ [
@ -153,27 +165,58 @@ function scanFileAreaForChanges(areaInfo, options, cb) {
}, },
(err, fileEntry, dupeEntries) => { (err, fileEntry, dupeEntries) => {
if(err) { if(err) {
// :TODO: Log me!!!
console.info(`Error: ${err.message}`); console.info(`Error: ${err.message}`);
return nextFile(null); // try next anyway return nextFile(null); // try next anyway
} }
if(dupeEntries.length > 0) { //
// :TODO: Handle duplidates -- what to do here??? // We'll update the entry if the following conditions are met:
console.info('Dupe'); // * We have a single duplicate, and:
// * --update-desc was passed or the existing entry's desc or
// longDesc are blank/empty
//
if(argv['update'] && 1 === dupeEntries.length) {
const FileEntry = require('../../core/file_entry.js');
const existingEntry = new FileEntry();
return existingEntry.load(dupeEntries[0].fileId, err => {
if(err) {
console.info('Dupe (cannot update)');
return nextFile(null); return nextFile(null);
} else {
console.info('Done!');
if(Array.isArray(options.tags)) {
options.tags.forEach(tag => {
fileEntry.hashTags.add(tag);
});
} }
finalizeEntryAndPersist(fileEntry, descHandler, err => { //
// Update only if tags or desc changed
//
const optTags = Array.isArray(options.tags) ? new Set(options.tags) : existingEntry.hashTags;
const tagsEq = _.isEqual(optTags, existingEntry.hashTags);
if( tagsEq &&
fileEntry.desc === existingEntry.desc &&
fileEntry.descLong == existingEntry.descLong)
{
console.info('Dupe');
return nextFile(null);
}
console.info('Dupe (updating)');
updateTags(existingEntry);
finalizeEntryAndPersist(true, existingEntry, descHandler, err => {
return nextFile(err); return nextFile(err);
}); });
});
} else if(dupeEntries.length > 0) {
console.info('Dupe');
return nextFile(null);
} }
console.info('Done!');
updateTags(fileEntry);
finalizeEntryAndPersist(false, fileEntry, descHandler, err => {
return nextFile(err);
});
} }
); );
}); });
@ -518,7 +561,7 @@ function moveFiles() {
function removeFiles() { function removeFiles() {
// //
// REMOVE SHA|FILE_ID [SHA|FILE_ID ...] // REMOVE FILENAME_WC|SHA|FILE_ID [SHA|FILE_ID ...]
} }
function handleFileBaseCommand() { function handleFileBaseCommand() {

View File

@ -65,6 +65,7 @@ scan args:
other sources such as FILE_ID.DIZ. other sources such as FILE_ID.DIZ.
if PATH is specified, use DESCRIPT.ION at PATH instead if PATH is specified, use DESCRIPT.ION at PATH instead
of looking in specific storage locations of looking in specific storage locations
--update attempt to update information for existing entries
info args: info args:
--show-desc display short description, if any --show-desc display short description, if any