YUKI Hiroshi
null+****@clear*****
Wed Oct 17 17:54:42 JST 2012
YUKI Hiroshi 2012-10-17 17:54:42 +0900 (Wed, 17 Oct 2012) New Revision: 2803400b8758ff6eb1d44888d68c5a462d91c05e https://github.com/groonga/gcs/commit/2803400b8758ff6eb1d44888d68c5a462d91c05e Log: Make gcs-configure-text-options based on HTTP APIs Modified files: bin/gcs-configure-text-options test/gcs-commands.test.js Modified: bin/gcs-configure-text-options (+79 -52) =================================================================== --- bin/gcs-configure-text-options 2012-10-17 17:42:08 +0900 (4314440) +++ bin/gcs-configure-text-options 2012-10-17 17:54:42 +0900 (d31ae3c) @@ -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.', @@ -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); + } + } + }); + } + } +}); Modified: test/gcs-commands.test.js (+12 -6) =================================================================== --- test/gcs-commands.test.js 2012-10-17 17:42:08 +0900 (0fe719c) +++ test/gcs-commands.test.js 2012-10-17 17:54:42 +0900 (6001987) @@ -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 }, @@ -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(); @@ -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(); @@ -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 }, @@ -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(); @@ -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(); -------------- next part -------------- HTML����������������������������... ダウンロード