Navigation Menu

Skip to content

Commit

Permalink
Refactor codes around command line interface
Browse files Browse the repository at this point in the history
  • Loading branch information
piroor committed Aug 6, 2012
1 parent b663e72 commit 686d916
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 70 deletions.
22 changes: 7 additions & 15 deletions bin/cs-configure-fields
@@ -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)
Expand All @@ -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()) {
Expand Down
22 changes: 9 additions & 13 deletions bin/cs-create-domain
@@ -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)
Expand All @@ -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.');
28 changes: 10 additions & 18 deletions bin/cs-delete-domain
@@ -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)
Expand All @@ -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 {
Expand Down
13 changes: 6 additions & 7 deletions bin/cs-describe-domain
@@ -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)
Expand All @@ -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) {
Expand All @@ -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);
Expand Down
20 changes: 9 additions & 11 deletions bin/gcs
Expand Up @@ -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)
Expand All @@ -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);
Expand Down
43 changes: 37 additions & 6 deletions lib/command-line.js
@@ -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;

0 comments on commit 686d916

Please sign in to comment.