Navigation Menu

Skip to content

Commit

Permalink
Move common codes for command line interface to command-line.js
Browse files Browse the repository at this point in the history
  • Loading branch information
piroor committed Aug 6, 2012
1 parent dc2b823 commit 0e8e674
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 65 deletions.
24 changes: 9 additions & 15 deletions bin/cs-configure-fields
@@ -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.',
Expand All @@ -18,40 +16,36 @@ 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);
}
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);
}
if (field.exists()) {
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 + ' ()');
Expand Down
12 changes: 3 additions & 9 deletions bin/cs-create-domain
@@ -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()) {
Expand Down
17 changes: 6 additions & 11 deletions bin/cs-delete-domain
@@ -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 {
Expand Down
17 changes: 6 additions & 11 deletions bin/cs-describe-domain
@@ -1,24 +1,19 @@
#!/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.',
String)
.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);
Expand All @@ -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);
Expand Down
26 changes: 11 additions & 15 deletions bin/gcs
@@ -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);
});
28 changes: 24 additions & 4 deletions lib/command-line.js
@@ -1,3 +1,4 @@
var program = require('commander');
var nroonga = require('./wrapped-nroonga');
var Domain = require('./database/domain').Domain;

Expand All @@ -8,24 +9,43 @@ 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 ||
(this._context = new nroonga.Context(this.databasePath));
},
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);
}
Expand Down

0 comments on commit 0e8e674

Please sign in to comment.