YUKI Hiroshi
null+****@clear*****
Wed Oct 17 16:46:51 JST 2012
YUKI Hiroshi 2012-10-17 16:46:51 +0900 (Wed, 17 Oct 2012) New Revision: ac5b52bed0c48df5d3d054d08e924371dce3068a https://github.com/groonga/gcs/commit/ac5b52bed0c48df5d3d054d08e924371dce3068a Log: Reimmpleemnt bin/gcs-configure-fields command with HTTP APIs Modified files: bin/gcs-configure-fields test/gcs-commands.test.js Modified: bin/gcs-configure-fields (+101 -50) =================================================================== --- bin/gcs-configure-fields 2012-10-17 15:44:04 +0900 (d91a403) +++ bin/gcs-configure-fields 2012-10-17 16:46:51 +0900 (2163705) @@ -1,8 +1,9 @@ #!/usr/bin/env node var CLI = require(__dirname + '/../lib/command-line').CommandLineInterface; -var commandLine = new CLI(); +var Client = require(__dirname + '/../lib/client').Client; +var commandLine = new CLI(); commandLine .usage('--name <field name> --type <field type> [options]') .option('--name <field name>', @@ -29,76 +30,126 @@ commandLine 'Delete the domain without prompting for confirmation.') .parse(); -commandLine.assertHaveDomainName(); -commandLine.assertDomainExists(); +var client = new Client(commandLine); -if (!commandLine.options.name) { - console.log('You must specify the field name.'); - return process.exit(1); -} +client.assertHaveDomainName(); -var field = commandLine.domain.getIndexField(commandLine.options.name); +var fieldName = commandLine.options.name; +if (!fieldName) + client.raiseFatalError('You must specify the field name.'); var option = commandLine.options.option; if (typeof option != 'string') option = null; -if (commandLine.options.delete) { - if (!field.exists()) { - console.log('You must specify an existing field.'); - return process.exit(1); +function doDelete(field) { + function sendDeleteRequest() { + client.configurationAPI.DeleteIndexField( + { + DomainName: client.domainName, + 'IndexField.IndexFieldName': fieldName + }, + function(error, response) { + if (error) + client.raiseFatalError(error); + console.log('Updated 1 Index Field:'); + process.exit(0); + } + ); } - var doDelete = function() { - field.deleteSync(); - console.log('Updated 1 Index Field:'); - }; + + if (!field) + client.raiseFatalError('You must specify an existing field.'); + if (commandLine.options.force) { - doDelete(); + sendDeleteRequest(); } else { - commandLine.confirm('Really delete? [' + field.name + '] (y/N) ', function(ok){ + commandLine.confirm('Really delete? [' + fieldName + '] (y/N) ', function(ok){ if (ok) { - doDelete(); - process.exit(0); + sendDeleteRequest(); } else { process.exit(1); } }); } -} else { - if (!field.exists()) { - if (!commandLine.options.type) { - console.log('You must specify the field type.'); - return process.exit(1); - } - field.type = commandLine.options.type; +} + +client.getIndexFieldStatus(fieldName, function(error, field) { + if (commandLine.options.delete) + return doDelete(field); + + var type = commandLine.options.type; + if (type && field) + type = field.Options.IndexFieldType; + + if (!field) { + if (!type) + client.raiseFatalErrlr('You must specify the field type.'); } else if (!option) { - console.log('You must specify the configuring option.'); - return process.exit(1); + client.raiseFatalError('You must specify the configuring option.'); + } + + switch (type) { + case 'text': + case 'uint': + case 'literal': + break; + default: + client.raiseFatalError('invalid field type ' + type); } + var params = { + domainName: client.domainName, + IndexField: { + IndexFieldName: fieldName, + IndexFieldType: type + } + }; + if (option) { - try { - switch (option) { - case 'search': field.searchEnabled = true; break; - case 'nosearch': field.searchEnabled = false; break; - case 'facet': field.facetEnabled = true; break; - case 'nofacet': field.facetEnabled = false; break; - case 'result': field.resultEnabled = true; break; - case 'noresult': field.resultEnabled = false; break; - default: - throw new Error('invalid field option ' + option); - } - } catch(error) { - console.log((error.message || error).toString()); - return process.exit(1); + var options = {}; + switch (option) { + case 'search': options.SearchEnabled = 'true'; break; + case 'nosearch': options.SearchEnabled = 'false'; break; + case 'facet': options.FacetEnabled = 'true'; break; + case 'nofacet': options.FacetEnabled = 'false'; break; + case 'result': options.ResultEnabled = 'true'; break; + case 'noresult': options.ResultEnabled = 'false'; break; + default: + client.raiseFatalError('invalid field option ' + option); + } + switch (type) { + case 'text': + if (option == 'nosearch') + client.raiseFatalError('searchable option cannot be configured for the type text.'); + params.IndexField.TextOptions = options; + break; + + case 'uint': + if (option == 'facet') + client.raiseFatalError('facet option cannot be configured for the type uint.'); + if (option == 'noresult') + client.raiseFatalError('returnable option cannot be configured for the type uint.'); + if (option == 'nosearch') + client.raiseFatalError('searchable option cannot be configured for the type uint.'); + params.IndexField.UIntOptions = options; + break; + + case 'literal': + params.IndexField.LiteralOptions = options; + break; } } - if (!field.exists()) - field.createSync(); - else - field.saveOptionsSync(); + client.configurationAPI.DefineIndexField(params, function(error, response) { + if (error) + client.raiseFatalError(error); - console.log('Updated 1 Index Field:'); - console.log(field.summary); -} + console.log('Updated 1 Index Field:'); + + var field = response.Body.DefineIndexFieldResponse.DefineIndexFieldResult.IndexField; + console.log(client.summarizeIndexFieldStatus(field)); + + process.exit(0); + }); +}); Modified: test/gcs-commands.test.js (+26 -13) =================================================================== --- test/gcs-commands.test.js 2012-10-17 15:44:04 +0900 (3eb8291) +++ test/gcs-commands.test.js 2012-10-17 16:46:51 +0900 (6d2570d) @@ -299,12 +299,14 @@ suite('gcs-configure-fields', function() { utils .run('gcs-create-domain', '--domain-name', 'companies', - '--database-path', temporaryDatabase.path) + '--port', utils.testPort, + '--base-host', 'localhost:' + utils.testPort) .run('gcs-configure-fields', '--domain-name', 'companies', '--name', name, '--type', type, - '--database-path', temporaryDatabase.path) + '--port', utils.testPort, + '--base-host', 'localhost:' + utils.testPort) .next(function(result) { assertSuccess(result, name, type, options); @@ -342,7 +344,8 @@ suite('gcs-configure-fields', function() { '--name', name, '--delete', '--force', - '--database-path', temporaryDatabase.path) + '--port', utils.testPort, + '--base-host', 'localhost:' + utils.testPort) .next(function(result) { assert.deepEqual({ code: result.code, message: result.output.stdout }, @@ -382,7 +385,8 @@ suite('gcs-configure-fields', function() { '--domain-name', 'companies', '--name', name, '--type', type, - '--database-path', temporaryDatabase.path) + '--port', utils.testPort, + '--base-host', 'localhost:' + utils.testPort) .next(function(result) { assert.deepEqual({ code: result.code, message: result.output.stdout }, @@ -414,7 +418,8 @@ suite('gcs-configure-fields', function() { '--name', 'name', '--delete', '--force', - '--database-path', temporaryDatabase.path) + '--port', utils.testPort, + '--base-host', 'localhost:' + utils.testPort) .next(function(result) { assert.deepEqual({ code: result.code, message: result.output.stdout }, @@ -434,7 +439,8 @@ suite('gcs-configure-fields', function() { .run('gcs-configure-fields', '--domain-name', 'companies', '--name', 'name', - '--database-path', temporaryDatabase.path) + '--port', utils.testPort, + '--base-host', 'localhost:' + utils.testPort) .next(function(result) { assert.deepEqual({ code: result.code, message: result.output.stdout }, @@ -452,7 +458,8 @@ suite('gcs-configure-fields', function() { utils .run('gcs-configure-fields', '--name', 'name', - '--database-path', temporaryDatabase.path) + '--port', utils.testPort, + '--base-host', 'localhost:' + utils.testPort) .next(function(result) { assertDomainNotSpecified(result); done(); @@ -493,7 +500,8 @@ suite('gcs-configure-fields', function() { '--domain-name', 'companies', '--name', name, '--option', 'search', - '--database-path', temporaryDatabase.path) + '--port', utils.testPort, + '--base-host', 'localhost:' + utils.testPort) .next(function(result) { if (results.search == 'error') assertOptionNotConfigurable(result, 'search', type); @@ -504,7 +512,8 @@ suite('gcs-configure-fields', function() { '--domain-name', 'companies', '--name', name, '--option', 'nosearch', - '--database-path', temporaryDatabase.path) + '--port', utils.testPort, + '--base-host', 'localhost:' + utils.testPort) .next(function(result) { if (results.nosearch == 'error') assertOptionNotConfigurable(result, 'nosearch', type); @@ -515,7 +524,8 @@ suite('gcs-configure-fields', function() { '--domain-name', 'companies', '--name', name, '--option', 'result', - '--database-path', temporaryDatabase.path) + '--port', utils.testPort, + '--base-host', 'localhost:' + utils.testPort) .next(function(result) { if (results.result == 'error') assertOptionNotConfigurable(result, 'result', type); @@ -526,7 +536,8 @@ suite('gcs-configure-fields', function() { '--domain-name', 'companies', '--name', name, '--option', 'noresult', - '--database-path', temporaryDatabase.path) + '--port', utils.testPort, + '--base-host', 'localhost:' + utils.testPort) .next(function(result) { if (results.noresult == 'error') assertOptionNotConfigurable(result, 'noresult', type); @@ -537,7 +548,8 @@ suite('gcs-configure-fields', function() { '--domain-name', 'companies', '--name', name, '--option', 'facet', - '--database-path', temporaryDatabase.path) + '--port', utils.testPort, + '--base-host', 'localhost:' + utils.testPort) .next(function(result) { if (results.facet == 'error') assertOptionNotConfigurable(result, 'facet', type); @@ -548,7 +560,8 @@ suite('gcs-configure-fields', function() { '--domain-name', 'companies', '--name', name, '--option', 'nofacet', - '--database-path', temporaryDatabase.path) + '--port', utils.testPort, + '--base-host', 'localhost:' + utils.testPort) .next(function(result) { if (results.nofacet == 'error') assertOptionNotConfigurable(result, 'nofacet', type); -------------- next part -------------- HTML����������������������������...Download