[Groonga-commit] groonga/gcs [master] Refactor codes around command line interface

Back to archive index

null+****@clear***** null+****@clear*****
2012年 8月 6日 (月) 16:28:39 JST


SHIMODA Hiroshi	2012-08-06 16:28:39 +0900 (Mon, 06 Aug 2012)

  New Revision: 686d91679857bfa15d95f2f1484428c1345376f2
  https://github.com/groonga/gcs/commit/686d91679857bfa15d95f2f1484428c1345376f2

  Log:
    Refactor codes around command line interface

  Modified files:
    bin/cs-configure-fields
    bin/cs-create-domain
    bin/cs-delete-domain
    bin/cs-describe-domain
    bin/gcs
    lib/command-line.js

  Modified: bin/cs-configure-fields (+7 -15)
===================================================================
--- bin/cs-configure-fields    2012-08-06 16:01:26 +0900 (686bc96)
+++ bin/cs-configure-fields    2012-08-06 16:28:39 +0900 (4c0b3b4)
@@ -1,7 +1,8 @@
 #!/usr/bin/env node
 
 var program = require('commander');
-var commandLine = require(__dirname + '/../lib/command-line');
+var CLI = require(__dirname + '/../lib/command-line').CommandLineInterface;
+var commandLine = new CLI(program);
 
 program
   .version(require('../package').version)
@@ -19,29 +20,20 @@ program
   .option('--delete',
           'Delete the field specified by the --name and --type options.')
   .option('--database-path <path>',
-          'database path [' + commandLine.defaultDatabasePath + ']',
+          'database path [' + CLI.defaultDatabasePath + ']',
           String,
-          commandLine.defaultDatabasePath)
+          CLI.defaultDatabasePath)
   .parse(process.argv);
 
-if (!program.domainName) {
-  console.log('You must specify the domain name.');
-  return process.exit(1);
-}
-
-var context = commandLine.getContext(program.databasePath);
-var domain = new commandLine.Domain(program.domainName, context);
-if (!domain.exists()) {
-  console.log('You must specify an existing domain name.');
-  return process.exit(1);
-}
+commandLine.assertHaveDomainName();
+commandLine.assertDomainExists();
 
 if (!program.name) {
   console.log('You must specify the field name.');
   return process.exit(1);
 }
 
-var field = domain.getIndexField(program.name);
+var field = commandLine.domain.getIndexField(program.name);
 
 if (program.delete) {
   if (!field.exists()) {

  Modified: bin/cs-create-domain (+9 -13)
===================================================================
--- bin/cs-create-domain    2012-08-06 16:01:26 +0900 (04c92a4)
+++ bin/cs-create-domain    2012-08-06 16:28:39 +0900 (9681410)
@@ -1,7 +1,8 @@
 #!/usr/bin/env node
 
 var program = require('commander');
-var commandLine = require(__dirname + '/../lib/command-line');
+var CLI = require(__dirname + '/../lib/command-line').CommandLineInterface;
+var commandLine = new CLI(program);
 
 program
   .version(require('../package').version)
@@ -10,26 +11,21 @@ program
           'The name of the domain that you are creating. Required.',
           String)
   .option('--database-path <path>',
-          'database path [' + commandLine.defaultDatabasePath + ']',
+          'database path [' + CLI.defaultDatabasePath + ']',
           String,
-          commandLine.defaultDatabasePath)
+          CLI.defaultDatabasePath)
   .parse(process.argv);
 
-if (!program.domainName) {
-  console.log('You must specify the domain name.');
-  return process.exit(1);
-}
+commandLine.assertHaveDomainName();
 
-var context = commandLine.getContext(program.databasePath);
-var domain = new commandLine.Domain(program.domainName, context);
-if (domain.exists()) {
-  console.log('The domain [' + program.domainName + '] already exists.');
+if (commandLine.domain.exists()) {
+  console.log('The domain [' + commandLine.domain.name + '] already exists.');
   return process.exit(1);
 }
 
-console.log('Creating domain [' + program.domainName + ']');
+console.log('Creating domain [' + commandLine.domain.name + ']');
 
-domain.createSync();
+commandLine.domain.createSync();
 
 console.log('Domain endpoints are currently being created. ' +
             'Use cs-describe-domain to check for endpoints.');

  Modified: bin/cs-delete-domain (+10 -18)
===================================================================
--- bin/cs-delete-domain    2012-08-06 16:01:26 +0900 (1f0d418)
+++ bin/cs-delete-domain    2012-08-06 16:28:39 +0900 (bc5be4a)
@@ -1,7 +1,8 @@
 #!/usr/bin/env node
 
 var program = require('commander');
-var commandLine = require(__dirname + '/../lib/command-line');
+var CLI = require(__dirname + '/../lib/command-line').CommandLineInterface;
+var commandLine = new CLI(program);
 
 program
   .version(require('../package').version)
@@ -12,30 +13,21 @@ program
   .option('-f, --force',
           'Delete the domain without prompting for confirmation.')
   .option('--database-path <path>',
-          'database path [' + commandLine.defaultDatabasePath + ']',
+          'database path [' + CLI.defaultDatabasePath + ']',
           String,
-          commandLine.defaultDatabasePath)
+          CLI.defaultDatabasePath)
   .parse(process.argv);
 
-if (!program.domainName) {
-  console.log('You must specify the domain name.');
-  return process.exit(1);
-}
-
-var context = commandLine.getContext(program.databasePath);
-var domain = new commandLine.Domain(program.domainName, context);
-if (!domain.exists()) {
-  console.log('You must specify an existing domain name.');
-  return process.exit(1);
-}
+commandLine.assertHaveDomainName();
+commandLine.assertDomainExists();
 
 if (program.force) {
-  domain.deleteSync();
-  console.log('Domain [' + domain.name + '] has been deleted successfully.');
+  commandLine.domain.deleteSync();
+  console.log('Domain [' + commandLine.domain.name + '] has been deleted successfully.');
 } else {
-  program.confirm('Really delete? [' + domain.name + '] (y/N)', function(ok){
+  program.confirm('Really delete? [' + commandLine.domain.name + '] (y/N)', function(ok){
     if (ok) {
-      domain.deleteSync();
+      commandLine.domain.deleteSync();
       console.log('Successfully deleted.');
       process.exit(0);
     } else {

  Modified: bin/cs-describe-domain (+6 -7)
===================================================================
--- bin/cs-describe-domain    2012-08-06 16:01:26 +0900 (7db72e6)
+++ bin/cs-describe-domain    2012-08-06 16:28:39 +0900 (1d4c700)
@@ -1,7 +1,8 @@
 #!/usr/bin/env node
 
 var program = require('commander');
-var commandLine = require(__dirname + '/../lib/command-line');
+var CLI = require(__dirname + '/../lib/command-line').CommandLineInterface;
+var commandLine = new CLI(program);
 
 program
   .version(require('../package').version)
@@ -14,9 +15,9 @@ program
              + 'including configured fields.',
           String)
   .option('--database-path <path>',
-          'database path [' + commandLine.defaultDatabasePath + ']',
+          'database path [' + CLI.defaultDatabasePath + ']',
           String,
-          commandLine.defaultDatabasePath)
+          CLI.defaultDatabasePath)
   .parse(process.argv);
 
 function report(domain) {
@@ -31,12 +32,10 @@ function report(domain) {
   console.log('SearchInstanceType        %s', domain.searchInstanceType);
 }
 
-var context = commandLine.getContext(program.databasePath);
 if (program.domainName) {
-  var domain = new commandLine.Domain(program.domainName, context);
-  report(domain);
+  report(commandLine.domain);
 } else {
-  var domains = commandLine.Domain.getAll(context);
+  var domains = commandLine.Domain.getAll(commandLine.context);
   domains.forEach(function(domain, index) {
     if (index) console.log('========================================');
     report(domain);

  Modified: bin/gcs (+9 -11)
===================================================================
--- bin/gcs    2012-08-06 16:01:26 +0900 (cf09ea1)
+++ bin/gcs    2012-08-06 16:28:39 +0900 (bc91531)
@@ -2,7 +2,7 @@
 
 var gcsServer = require(__dirname + '/../lib/server');
 var program = require('commander');
-var commandLine = require(__dirname + '/../lib/command-line');
+var CLI = require(__dirname + '/../lib/command-line').CommandLineInterface;
 
 program
   .version(require('../package').version)
@@ -12,22 +12,20 @@ program
           Number,
           7575)
   .option('--database-path <path>',
-          'database path [' + commandLine.defaultDatabasePath + ']',
+          'database path [' + CLI.defaultDatabasePath + ']',
           String,
-          commandLine.defaultDatabasePath)
+          CLI.defaultDatabasePath)
   .option('--privilege <ip range>',
           'list of IP ranges for privileged client '+
-            '[' + commandLine.defaultPrivilegedRanges + ']',
+            '[' + CLI.defaultPrivilegedRanges + ']',
           String,
-          commandLine.defaultPrivilegedRanges)
+          CLI.defaultPrivilegedRanges)
   .parse(process.argv);
 
-var server;
-
-server = gcsServer.createServer({
-  databasePath:     program.databasePath,
-  privilegedRanges: program.privilege
-});
+var server = gcsServer.createServer({
+      databasePath:     program.databasePath,
+      privilegedRanges: program.privilege
+    });
 
 server.listen(program.port, function() {
   console.log('gcs listening at %d', program.port);

  Modified: lib/command-line.js (+37 -6)
===================================================================
--- lib/command-line.js    2012-08-06 16:01:26 +0900 (98b617c)
+++ lib/command-line.js    2012-08-06 16:28:39 +0900 (dcb5c39)
@@ -1,11 +1,42 @@
 var nroonga = require('./wrapped-nroonga');
-var context;
 
-exports.defaultDatabasePath = process.env.HOME + '/.gcs/database/gcs';
-exports.defaultPrivilegedRanges = '127.0.0.0/8';
-exports.getContext = function(databasePath) {
-  return context ||
-         (context = new nroonga.Context(databasePath || exports.defaultDatabasePath));
+var defaultDatabasePath =
+      exports.defaultDatabasePath =
+      CommandLineInterface.defaultDatabasePath = process.env.HOME + '/.gcs/database/gcs';
+var defaultPrivilegedRanges =
+      exports.defaultPrivilegedRanges =
+      CommandLineInterface.defaultPrivilegedRanges = '127.0.0.0/8';
+
+function CommandLineInterface(program) {
+  this.program = program;
+}
+CommandLineInterface.prototype = {
+  get databasePath() {
+    return this.program.databasePath || defaultDatabasePath;
+  },
+  get context() {
+    return this._context ||
+           (this._context = new nroonga.Context(this.databasePath));
+  },
+  get domain() {
+    return this._domain ||
+           (this._domain = new Domain(this.program.domainName, this.context));
+  },
+
+  assertHaveDomainName: function() {
+    if (!this.program.domainName) {
+      console.log('You must specify the domain name.');
+      process.exit(1);
+    }
+  },
+
+  assertDomainExists: function() {
+    if (!this.domain.exists()) {
+      console.log('You must specify an existing domain name.');
+      process.exit(1);
+    }
+  },
 };
+exports.CommandLineInterface = CommandLineInterface;
 
 exports.Domain = require('./database/domain').Domain;
-------------- next part --------------
HTML$B$NE:IU%U%!%$%k$rJ]4I$7$^$7$?(B...
Download 



Groonga-commit メーリングリストの案内
Back to archive index