[Groonga-commit] groonga/gcs [master] Move common codes for command line interface to command-line.js

Back to archive index

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


SHIMODA Hiroshi	2012-08-06 16:58:56 +0900 (Mon, 06 Aug 2012)

  New Revision: 0e8e6742b7d35f1249b0e0c45ffd8c88e2af77e1
  https://github.com/groonga/gcs/commit/0e8e6742b7d35f1249b0e0c45ffd8c88e2af77e1

  Log:
    Move common codes for command line interface to command-line.js

  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 (+9 -15)
===================================================================
--- bin/cs-configure-fields    2012-08-06 16:41:48 +0900 (4c0b3b4)
+++ bin/cs-configure-fields    2012-08-06 16:58:56 +0900 (9230d45)
@@ -1,11 +1,9 @@
 #!/usr/bin/env node
 
-var program = require('commander');
 var CLI = require(__dirname + '/../lib/command-line').CommandLineInterface;
-var commandLine = new CLI(program);
+var commandLine = new CLI();
 
-program
-  .version(require('../package').version)
+commandLine
   .usage('--name <field name> --type <field type> [options]')
   .option('--name <field name>',
           'The name of the field you are configuring or deleting. Required.',
@@ -18,24 +16,20 @@ program
           'The name of the domain that you are configuring. Required.',
           String)
   .option('--delete',
-          'Delete the field specified by the --name and --type options.')
-  .option('--database-path <path>',
-          'database path [' + CLI.defaultDatabasePath + ']',
-          String,
-          CLI.defaultDatabasePath)
-  .parse(process.argv);
+          'Delete the field specified by the --name and --type options.');
 
+commandLine.parse();
 commandLine.assertHaveDomainName();
 commandLine.assertDomainExists();
 
-if (!program.name) {
+if (!commandLine.options.name) {
   console.log('You must specify the field name.');
   return process.exit(1);
 }
 
-var field = commandLine.domain.getIndexField(program.name);
+var field = commandLine.domain.getIndexField(commandLine.options.name);
 
-if (program.delete) {
+if (commandLine.options.delete) {
   if (!field.exists()) {
     console.log('You must specify an existing field.');
     return process.exit(1);
@@ -43,7 +37,7 @@ if (program.delete) {
   field.deleteSync();
   console.log('Updated 1 Index Field:');
 } else {
-  if (!program.type) {
+  if (!commandLine.options.type) {
     console.log('You must specify the field type.');
     return process.exit(1);
   }
@@ -51,7 +45,7 @@ if (program.delete) {
     console.log('You must specify not-existing field name.');
     return process.exit(1);
   }
-  field.type = program.type;
+  field.type = commandLine.options.type;
   field.createSync();
   console.log('Updated 1 Index Field:');
   console.log(field.name + ' ' + field.state + ' ' + field.type + ' ()');

  Modified: bin/cs-create-domain (+3 -9)
===================================================================
--- bin/cs-create-domain    2012-08-06 16:41:48 +0900 (9681410)
+++ bin/cs-create-domain    2012-08-06 16:58:56 +0900 (dbd424b)
@@ -1,21 +1,15 @@
 #!/usr/bin/env node
 
-var program = require('commander');
 var CLI = require(__dirname + '/../lib/command-line').CommandLineInterface;
-var commandLine = new CLI(program);
+var commandLine = new CLI();
 
 program
-  .version(require('../package').version)
   .usage('--domain-name <domain name> [options]')
   .option('-d, --domain-name <domain name>',
           'The name of the domain that you are creating. Required.',
-          String)
-  .option('--database-path <path>',
-          'database path [' + CLI.defaultDatabasePath + ']',
-          String,
-          CLI.defaultDatabasePath)
-  .parse(process.argv);
+          String);
 
+commandLine.parse();
 commandLine.assertHaveDomainName();
 
 if (commandLine.domain.exists()) {

  Modified: bin/cs-delete-domain (+6 -11)
===================================================================
--- bin/cs-delete-domain    2012-08-06 16:41:48 +0900 (bc5be4a)
+++ bin/cs-delete-domain    2012-08-06 16:58:56 +0900 (c7155dc)
@@ -1,27 +1,22 @@
 #!/usr/bin/env node
 
-var program = require('commander');
 var CLI = require(__dirname + '/../lib/command-line').CommandLineInterface;
-var commandLine = new CLI(program);
+var commandLine = new CLI();
 
-program
-  .version(require('../package').version)
+commandLine
   .usage('--domain-name <domain name> [options]')
   .option('-d, --domain-name <domain name>',
           'The name of the domain that you are deleting. Required.',
           String)
   .option('-f, --force',
-          'Delete the domain without prompting for confirmation.')
-  .option('--database-path <path>',
-          'database path [' + CLI.defaultDatabasePath + ']',
-          String,
-          CLI.defaultDatabasePath)
-  .parse(process.argv);
+          'Delete the domain without prompting for confirmation.');
+
+commandLine.parse();
 
 commandLine.assertHaveDomainName();
 commandLine.assertDomainExists();
 
-if (program.force) {
+if (commandLine.options.force) {
   commandLine.domain.deleteSync();
   console.log('Domain [' + commandLine.domain.name + '] has been deleted successfully.');
 } else {

  Modified: bin/cs-describe-domain (+6 -11)
===================================================================
--- bin/cs-describe-domain    2012-08-06 16:41:48 +0900 (3eb37ba)
+++ bin/cs-describe-domain    2012-08-06 16:58:56 +0900 (0a2e0a4)
@@ -1,11 +1,9 @@
 #!/usr/bin/env node
 
-var program = require('commander');
 var CLI = require(__dirname + '/../lib/command-line').CommandLineInterface;
-var commandLine = new CLI(program);
+var commandLine = new CLI();
 
-program
-  .version(require('../package').version)
+commandLine
   .usage('[options]')
   .option('-d, --domain-name <domain name>',
           'The name of the domain that you are creating. Required.',
@@ -13,12 +11,9 @@ program
   .option('-all, --show-all',
           'Display all available information for the domain, '
              + 'including configured fields.',
-          String)
-  .option('--database-path <path>',
-          'database path [' + CLI.defaultDatabasePath + ']',
-          String,
-          CLI.defaultDatabasePath)
-  .parse(process.argv);
+          String);
+
+commandLine.parse();
 
 function report(domain) {
   console.log('Domain Name               %s', domain.name);
@@ -32,7 +27,7 @@ function report(domain) {
   console.log('SearchInstanceType        %s', domain.searchInstanceType);
 }
 
-if (program.domainName) {
+if (commandLine.options.domainName) {
   report(commandLine.domain);
 } else {
   var domains = CLI.Domain.getAll(commandLine.context);

  Modified: bin/gcs (+11 -15)
===================================================================
--- bin/gcs    2012-08-06 16:41:48 +0900 (bc91531)
+++ bin/gcs    2012-08-06 16:58:56 +0900 (9ef21f7)
@@ -1,34 +1,30 @@
 #!/usr/bin/env node
 
 var gcsServer = require(__dirname + '/../lib/server');
-var program = require('commander');
 var CLI = require(__dirname + '/../lib/command-line').CommandLineInterface;
+var commandLine = new CLI();
 
-program
-  .version(require('../package').version)
+commandLine
   .usage('[options]')
   .option('-p, --port <port>',
           'specify port [7575]',
           Number,
           7575)
-  .option('--database-path <path>',
-          'database path [' + CLI.defaultDatabasePath + ']',
-          String,
-          CLI.defaultDatabasePath)
   .option('--privilege <ip range>',
           'list of IP ranges for privileged client '+
             '[' + CLI.defaultPrivilegedRanges + ']',
           String,
-          CLI.defaultPrivilegedRanges)
-  .parse(process.argv);
+          CLI.defaultPrivilegedRanges);
+
+commandLine.parse();
 
 var server = gcsServer.createServer({
-      databasePath:     program.databasePath,
-      privilegedRanges: program.privilege
+      databasePath:     commandLine.options.databasePath,
+      privilegedRanges: commandLine.options.privilege
     });
 
-server.listen(program.port, function() {
-  console.log('gcs listening at %d', program.port);
-  console.log('database is at %s', program.databasePath);
-  console.log('privileged IP ranges are %s', program.privilege);
+server.listen(commandLine.options.port, function() {
+  console.log('gcs listening at %d', commandLine.options.port);
+  console.log('database is at %s', commandLine.options.databasePath);
+  console.log('privileged IP ranges are %s', commandLine.options.privilege);
 });

  Modified: lib/command-line.js (+24 -4)
===================================================================
--- lib/command-line.js    2012-08-06 16:41:48 +0900 (d1b8414)
+++ lib/command-line.js    2012-08-06 16:58:56 +0900 (58400d8)
@@ -1,3 +1,4 @@
+var program = require('commander');
 var nroonga = require('./wrapped-nroonga');
 var Domain = require('./database/domain').Domain;
 
@@ -8,12 +9,18 @@ var defaultPrivilegedRanges =
       exports.defaultPrivilegedRanges =
       CommandLineInterface.defaultPrivilegedRanges = '127.0.0.0/8';
 
-function CommandLineInterface(program) {
+function CommandLineInterface() {
   this.program = program;
+  this.program.
+      .version(require('../package').version)
+      .option('--database-path <path>',
+              'database path [' + defaultDatabasePath + ']',
+              String,
+              defaultDatabasePath)
 }
 CommandLineInterface.prototype = {
   get databasePath() {
-    return this.program.databasePath || defaultDatabasePath;
+    return this.options.databasePath || defaultDatabasePath;
   },
   get context() {
     return this._context ||
@@ -21,11 +28,24 @@ CommandLineInterface.prototype = {
   },
   get domain() {
     return this._domain ||
-           (this._domain = new Domain(this.program.domainName, this.context));
+           (this._domain = new Domain(this.options.domainName, this.context));
+  },
+  get options() {
+    return this.program;
+  },
+
+  parse: function() {
+    this.program.parse(process.argv);
+  },
+  usage: function() {
+    return this.program.option.apply(this.program, arguments);
+  },
+  option: function() {
+    return this.program.option.apply(this.program, arguments);
   },
 
   assertHaveDomainName: function() {
-    if (!this.program.domainName) {
+    if (!this.options.domainName) {
       console.log('You must specify the domain name.');
       process.exit(1);
     }
-------------- next part --------------
HTML$B$NE:IU%U%!%$%k$rJ]4I$7$^$7$?(B...
Download 



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