Skip to content

Commit

Permalink
expose all commands via CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
andrenarchy committed May 27, 2016
1 parent 10ff66e commit 3bd7038
Showing 1 changed file with 74 additions and 16 deletions.
90 changes: 74 additions & 16 deletions bin/oai-pmh
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,85 @@ const pkg = require('../package.json');
program
.version(pkg.version);

function run(fun) {
co(fun).then(
() => process.exit(0),
(err) => {
process.stderr.write(`${err.toS()}\n`);
process.exit(1);
}
);
}

function printJson(obj) {
process.stdout.write(`${JSON.stringify(obj)}\n`);
}

function* printList(promiseGenerator) {
for (const itemPromise of promiseGenerator) {
const item = yield itemPromise;
printJson(item);
}
}

program
.command('get-record <baseUrl>')
.option('-i, --identifier <id>')
.option('-p, --metadata-prefix <prefix>')
.action((baseUrl, options) => run(function* listSets() {
const oaiPmh = new oaiPmhModule.OaiPmh(baseUrl);
const result =
yield oaiPmh.getRecord(options.identifier, options.metadataPrefix);
printJson(result);
}));

program
.command('identify <baseUrl>')
.action(baseUrl => run(function* identify() {
const oaiPmh = new oaiPmhModule.OaiPmh(baseUrl);
const result = yield oaiPmh.identify();
printJson(result);
}));

program
.command('list-identifiers <baseUrl>')
.option('-p, --metadata-prefix <prefix>')
.option('-f, --from <DATE>', 'from date YYYY-MM-DD or ISO8601')
.option('-u, --until <DATE>', 'from date YYYY-MM-DD or ISO8601')
.option('-s, --set <SETSPEC>', 'set specifier, e.g., "math"')
.action((baseUrl, _options) => {
try {
co(function* _listIdentifiers() {
const options = _.pick(_options, 'metadataPrefix', 'from', 'until', 'set');
const oaiPmh = new oaiPmhModule.OaiPmh(baseUrl);

for (const identifierPromise of oaiPmh.listIdentifiers(options)) {
const identifier = yield identifierPromise;
process.stdout.write(`${JSON.stringify(identifier)}\n`);
}
});
} catch (err) {
process.stderr.write(`${err.message}\n`);
process.exit(1);
}
});
.action((baseUrl, _options) => run(function* listIdentifiers() {
const options = _.pick(_options, 'metadataPrefix', 'from', 'until', 'set');
const oaiPmh = new oaiPmhModule.OaiPmh(baseUrl);
yield printList(oaiPmh.listIdentifiers(options));
}));

program
.command('list-metadata-formats <baseUrl>')
.option('-i, --identifier <id>')
.action((baseUrl, _options) => run(function* listIdentifiers() {
const options = _.pick(_options, 'identifier');
const oaiPmh = new oaiPmhModule.OaiPmh(baseUrl);
const result = yield oaiPmh.listMetadataFormats(options);
printJson(result);
}));

program
.command('list-records <baseUrl>')
.option('-p, --metadata-prefix <prefix>')
.option('-f, --from <DATE>', 'from date YYYY-MM-DD or ISO8601')
.option('-u, --until <DATE>', 'from date YYYY-MM-DD or ISO8601')
.option('-s, --set <SETSPEC>', 'set specifier, e.g., "math"')
.action((baseUrl, _options) => run(function* listIdentifiers() {
const options = _.pick(_options, 'metadataPrefix', 'from', 'until', 'set');
const oaiPmh = new oaiPmhModule.OaiPmh(baseUrl);
yield printList(oaiPmh.listIdentifiers(options));
}));

program
.command('list-sets <baseUrl>')
.action(baseUrl => run(function* listSets() {
const oaiPmh = new oaiPmhModule.OaiPmh(baseUrl);
yield printList(oaiPmh.listSets());
}));

program.parse(process.argv);

0 comments on commit 3bd7038

Please sign in to comment.