Skip to content

Commit

Permalink
headers got fixed, modules command added
Browse files Browse the repository at this point in the history
  • Loading branch information
openmason committed Mar 6, 2013
1 parent 416d899 commit c2c8a80
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 20 deletions.
3 changes: 2 additions & 1 deletion History.md
@@ -1,4 +1,5 @@
# release 0.2.6
# release 0.2.7
* headers had issues and got fixed, module switching added
* user/password can also be specified in rc file
* fixed alias display, checking for version enabled
* list is now executed in series (also respects any input statements in the list)
Expand Down
15 changes: 15 additions & 0 deletions README.md
Expand Up @@ -141,3 +141,18 @@ Use mustache syntax within string for subtitutions
.. > b
would issue 'get students/abc'


### Pluggable modules
nosh comes with pluggable modules and by default ships with a demo and rest module.
In order to list the modules available, issue

.. > .modules
rest
demo

To switch between modules, issue

.. > .switch demo
context switched to: demo


12 changes: 8 additions & 4 deletions lib/modules/rest.js
Expand Up @@ -106,9 +106,12 @@ RestCli.prototype.prompt = function() {

RestCli.prototype.printHeaders = function (headers) {
Object.keys(this.headers).forEach(function (k) {
/*
var key = k.replace(/\b([a-z])/g, function (_, m) {
return m.toUpperCase();
}).bold;
*/
var key = k.bold;
util.puts(key + ': ' + headers[k]);
});
};
Expand Down Expand Up @@ -234,7 +237,8 @@ RestCli.prototype.dispatch = function(command, nosh, done) {
options.data = JSON.stringify(self.requestObj);
}
this.setCookies(this.headers);
options = handy.deepMerge(this.headers, options);
if(!options.headers) options.headers={};
options.headers = handy.deepMerge(this.headers, options.headers);
if(this.verbose) {
util.puts((method+' '+path).grey);
this.printHeaders(options);
Expand Down Expand Up @@ -263,7 +267,7 @@ RestCli.prototype.dispatch = function(command, nosh, done) {
util.puts('filter set to ' + self.filter);
} else {
self.filter=[];
console.log('filter reset to none');
util.puts('filter reset to none');
}
done();
} else if(/^(OUTPUT)/i.test(command)) {
Expand All @@ -273,7 +277,7 @@ RestCli.prototype.dispatch = function(command, nosh, done) {
} else {
self.outputFormat='json';
}
console.log("output format set to "+self.outputFormat);
util.puts("output format set to "+self.outputFormat);
done();
} else if(/^(READ)/i.test(command)) {
// read the file name
Expand All @@ -287,7 +291,7 @@ RestCli.prototype.dispatch = function(command, nosh, done) {
util.puts(JSON.stringify(self.requestObj));
}
} else {
console.log('missing filename'.red);
util.puts('missing filename'.red);
}
done();
} else if(/^(REQUEST)/i.test(command)) {
Expand Down
56 changes: 42 additions & 14 deletions lib/nosh.js
Expand Up @@ -34,10 +34,12 @@ var PROMPT = '> ';
var help = [
'.alias ' + 'display aliases.'.grey,
'.history ' + 'display last 10 commands.'.grey,
'.modules ' + 'display available modules.'.grey,
'.switch ' + 'switch to another module.'.grey,
'.help ' + 'display this message.'.grey,
'.quit ' + 'exit console.'.grey
].join('\n');
var commands = '.alias .history .help .quit'.split(' ');
var commands = '.alias .modules .switch .history .help .quit'.split(' ');

Nosh.prototype.version = handy.getVersion();

Expand Down Expand Up @@ -131,11 +133,16 @@ function Nosh(config, module) {
this.completions = [];
var loadableModules = (config.application && config.application.modules) || [];
for(var i=0;i<loadableModules.length;i++) {
this.loadPlugin(loadableModules[i], config[loadableModules[i]]);
var m = loadableModules[i];
this.loadPlugin(m, config[m]);
// instantiate an object if its not present already
if(!this.objects[m]) {
this.objects[m]=new this.modules[m](config[m]);
}
}

// setup headers
this.start(config, module);
this.initApp(config, module);
}

Nosh.prototype.error = function (errstr) {
Expand Down Expand Up @@ -165,18 +172,11 @@ Nosh.prototype.loadPlugin = function(module) {
};

/*
* Start/switch to a given module
* load and init nosh app settings
*/
Nosh.prototype.start = function(config, module) {
Nosh.prototype.initApp = function(config, module) {
if(module) {
// instantiate an object if its not present already
if(!this.objects[module]) {
this.objects[module]=new this.modules[module](config[module]);
}
// point to the correct module
this.curr = this.objects[module];
this.currentModule = module;
this.completions = commands.concat(this.curr.commands() || []);
this.switchToModule(module);
}
// lets load the init list
var initList = (config.application && config.application.init) || [];
Expand All @@ -186,6 +186,21 @@ Nosh.prototype.start = function(config, module) {
this.prompt();
};

/*
* switch to a given module
*/
Nosh.prototype.switchToModule = function(module) {
if(!module || !this.objects.hasOwnProperty(module)) {
util.puts('unable to switch to module: ' + module.red);
return;
}
// point to the correct module
this.curr = this.objects[module];
this.currentModule = module;
this.completions = commands.concat(this.curr.commands() || []);
util.puts('context switched to: ' + module.green);
};

Nosh.prototype.exec = function(cmd) {
cmd = cmd.trim();
var self=this;
Expand Down Expand Up @@ -308,7 +323,20 @@ Nosh.prototype.dispatch = function(command, done) {
} else if(/^(.HISTORY)/i.test(command)) {
var h = this.rl.history;
for(var i=0;i<h.length;i++) {
console.log(i+1, h[i]);
util.puts(i+1, h[i]);
}
done();
} else if(/^(.MODULES)/i.test(command)) {
var modules = Object.keys(this.modules);
for(i=0;i<modules.length;i++) {
util.puts(modules[i].cyan);
}
done();
} else if(/^(.SWITCH)/i.test(command)) {
argv = cli.parse(self.extractWords(command));
if(argv._.length==2) {
var m = argv._.slice(1)[0];
self.switchToModule(m);
}
done();
} else if(/^(.ALIAS)/i.test(command)) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -2,7 +2,7 @@
"name": "nosh",
"description": "Node.js Shell",
"homepage": "https://github.com/openmason/nosh",
"version": "0.2.6",
"version": "0.2.7",
"author": "el aras <openmason@gmail.com>",

"dependencies": {
Expand Down

0 comments on commit c2c8a80

Please sign in to comment.