Skip to content

Commit

Permalink
Make gcs-configure-text-options based on HTTP APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
piroor committed Oct 17, 2012
1 parent 53a38fc commit 2803400
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 58 deletions.
131 changes: 79 additions & 52 deletions bin/gcs-configure-text-options
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#!/usr/bin/env node

var CLI = require(__dirname + '/../lib/command-line').CommandLineInterface;
var commandLine = new CLI();
var Client = require(__dirname + '/../lib/client').Client;
var fs = require('fs');

var commandLine = new CLI();
commandLine
.option('-d, --domain-name <domain name>',
'The name of the domain that you are querying or configuring. Required.',
Expand Down Expand Up @@ -40,63 +41,89 @@ commandLine
'List the domain\'s synonyms.')
.parse();

commandLine.assertHaveDomainName();
commandLine.assertDomainExists();

function strip(string) {
return string.replace(/^\s+|\s+$/g, '');
}

var printOptions = ['printStems', 'printStopwords', 'printSynonyms'];
var loadOptions = ['stems', 'stopwords', 'synonyms'];
var client = new Client(commandLine);

if (commandLine.hasTooManyExclusiveOptions(printOptions)) {
console.log('Too many options. ' +
'You must specify just one option from ' +
'--print-stems, --print-stopwords, or --print-synonyms.');
process.exit(1);
return;
}
client.assertHaveDomainName();
client.getDomainStatus(function(error, domain) {
if (error)
client.raiseFatalError(error);

if (printOptions.some(function(option) {
return commandLine.options[option];
}) &&
loadOptions.some(function(option) {
return commandLine.options[option];
})) {
console.log('Too many options. ' +
'You cannot do print and load on the same time.');
process.exit(1);
return;
}
var printOptions = ['printStems', 'printStopwords', 'printSynonyms'];
var loadOptions = ['stems', 'stopwords', 'synonyms'];

if (commandLine.options.printStems) {
} else if (commandLine.options.printStopwords) {
} else if (commandLine.options.printSynonyms) {
var synonyms = commandLine.domain.getSynonymsSync();
Object.keys(synonyms).forEach(function(term) {
console.log([term].concat(synonyms[term]).join(','));
});
} else {
if (commandLine.options.stems) {
}
if (commandLine.options.stopwords) {
}
if (commandLine.options.synonyms) {
var synonymsFile = CLI.resolve(commandLine.options.synonyms);
var synonymsCSV = fs.readFileSync(synonymsFile, 'UTF-8');
var synonyms = {};
synonymsCSV.split('\n').forEach(function(synonym) {
synonym = strip(synonym);
if (!synonym) return;
var terms = synonym.split(',');
var key = strip(terms.shift());
var previousTerms = synonyms[key] || [];
synonyms[key] = previousTerms.concat(terms.map(strip));
});
commandLine.domain.updateSynonymsSync(synonyms);
console.log('%s synonyms are loaded.', Object.keys(synonyms).length);
}
}
if (commandLine.hasTooManyExclusiveOptions(printOptions))
client.raiseFatalError('Too many options. ' +
'You must specify just one option from ' +
'--print-stems, --print-stopwords, or --print-synonyms.');

if (printOptions.some(function(option) {
return commandLine.options[option];
}) &&
loadOptions.some(function(option) {
return commandLine.options[option];
}))
client.raiseFatalError('Too many options. ' +
'You cannot do print and load on the same time.');

if (commandLine.options.printStems) {
} else if (commandLine.options.printStopwords) {
} else if (commandLine.options.printSynonyms) {
client.configurationAPI.DescribeSynonymOptions(
{ DomainName: client.domainName },
function(error, response) {
if (error)
client.raiseFatalError(error);
try {
var synonyms = response.Body.DescribeSynonymOptionsResponse
.DescribeSynonymOptionsResult
.Synonyms
.Options;
synonyms = JSON.parse(synonyms);
Object.keys(synonyms).forEach(function(term) {
console.log([term].concat(synonyms[term]).join(','));
});
process.exit(0);
} catch(error) {
client.raiseFatalError(error);
}
}
});
} else {
if (commandLine.options.stems) {
}
if (commandLine.options.stopwords) {
}
if (commandLine.options.synonyms) {
var synonymsFile = CLI.resolve(commandLine.options.synonyms);
var synonymsCSV = fs.readFileSync(synonymsFile, 'UTF-8');
var synonyms = {};
synonymsCSV.split('\n').forEach(function(synonym) {
synonym = strip(synonym);
if (!synonym) return;
var terms = synonym.split(',');
var key = strip(terms.shift());
var previousTerms = synonyms[key] || [];
synonyms[key] = previousTerms.concat(terms.map(strip));
});
client.configurationAPI.UpdateSynonymOptions(
{
DomainName: client.domainName,
Synonyms: JSON.stringify({ synonyms: synonyms })
},
function(error, response) {
if (error)
client.raiseFatalError(error);
try {
console.log('%s synonyms are loaded.', Object.keys(synonyms).length);
} catch(error) {
client.raiseFatalError(error);
}
}
});
}
}
});
18 changes: 12 additions & 6 deletions test/gcs-commands.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,8 @@ suite('gcs-configure-text-options', function() {
.run('gcs-configure-text-options',
'--domain-name', 'companies',
'--synonyms', path.join(__dirname, 'fixtures', 'synonyms.txt'),
'--database-path', temporaryDatabase.path)
'--port', utils.testPort,
'--base-host', 'localhost:' + utils.testPort)
.next(function(result) {
assert.deepEqual({ code: result.code,
message: result.output.stdout },
Expand All @@ -637,7 +638,8 @@ suite('gcs-configure-text-options', function() {
.run('gcs-configure-text-options',
'--domain-name', 'companies',
'--synonyms', path.join(__dirname, 'fixtures', 'synonyms.txt'),
'--database-path', temporaryDatabase.path)
'--port', utils.testPort,
'--base-host', 'localhost:' + utils.testPort)
.next(function(result) {
assertDomainNotExist(result, 'companies');
done();
Expand All @@ -651,7 +653,8 @@ suite('gcs-configure-text-options', function() {
utils
.run('gcs-configure-text-options',
'--synonyms', path.join(__dirname, 'fixtures', 'synonyms.txt'),
'--database-path', temporaryDatabase.path)
'--port', utils.testPort,
'--base-host', 'localhost:' + utils.testPort)
.next(function(result) {
assertDomainNotSpecified(result);
done();
Expand All @@ -672,7 +675,8 @@ suite('gcs-configure-text-options', function() {
.run('gcs-configure-text-options',
'--domain-name', 'companies',
'--print-synonyms',
'--database-path', temporaryDatabase.path)
'--port', utils.testPort,
'--base-host', 'localhost:' + utils.testPort)
.next(function(result) {
assert.deepEqual({ code: result.code,
message: result.output.stdout },
Expand All @@ -692,7 +696,8 @@ suite('gcs-configure-text-options', function() {
.run('gcs-configure-text-options',
'--domain-name', 'companies',
'--print-synonyms',
'--database-path', temporaryDatabase.path)
'--port', utils.testPort,
'--base-host', 'localhost:' + utils.testPort)
.next(function(result) {
assertDomainNotExist(result, 'companies');
done();
Expand All @@ -706,7 +711,8 @@ suite('gcs-configure-text-options', function() {
utils
.run('gcs-configure-text-options',
'--print-synonyms',
'--database-path', temporaryDatabase.path)
'--port', utils.testPort,
'--base-host', 'localhost:' + utils.testPort)
.next(function(result) {
assertDomainNotSpecified(result);
done();
Expand Down

0 comments on commit 2803400

Please sign in to comment.