refactoring to pull basic commands (that don't have to do with rewriting) out of rewriter.js
diff --git a/client/lib/commands/create.js b/client/lib/commands/create.js
new file mode 100644
index 0000000..6732395
--- /dev/null
+++ b/client/lib/commands/create.js
@@ -0,0 +1,53 @@
+var inquirer = require('inquirer'),
+ ok = require('../repl-messages').ok,
+ ok_ = require('../repl-messages').ok_,
+ errorWhile = require('../repl-messages').errorWhile,
+ setupOpenWhisk = require('../util').setupOpenWhisk;
+
+exports.created = {};
+
+/**
+ * Create an action
+ *
+ */
+exports.create = function create(wskprops, next, name) {
+ var questions = [];
+ if (!name) {
+ questions.push({ name: 'name', message: 'Choose a name for your new action' });
+ }
+ questions.push({ name: 'kind', type: 'list',
+ message: 'Which runtime do you want to use?',
+ choices: ['nodejs', 'swift', 'python' ]
+ });
+ questions.push({ name: 'code', type: 'editor',
+ message: 'Please provide the function body for your new action',
+ default: function(response) {
+ if (response.kind === 'nodejs') {
+ // nodejs
+ return 'function main(params) {\n return { message: \'hello\' };\n}\n';
+ } else if (response.kind === 'swift') {
+ // swift
+ return 'func main(args: [String:Any]) -> [String:Any] {\n return [ "message" : "Hello world" ]\n}\n';
+ } else {
+ // python
+ return 'import sys\n\ndef main(dict):\n return { \'message\': \'Hello world\' }\n';
+ }
+ }
+ });
+
+ inquirer
+ .prompt(questions)
+ .then(response => {
+ return setupOpenWhisk(wskprops).actions.create({
+ actionName: name || response.name,
+ action: {
+ exec: {
+ kind: response.kind,
+ code: response.code
+ }
+ }
+ });
+ })
+ .then((action) => exports.created[action.name] = true)
+ .then(ok(next), errorWhile('creating action', next));
+};
diff --git a/client/lib/commands/delete.js b/client/lib/commands/delete.js
new file mode 100644
index 0000000..f735572
--- /dev/null
+++ b/client/lib/commands/delete.js
@@ -0,0 +1,33 @@
+var created = require('./create').created,
+ _list = require('./list')._list,
+ ok = require('../repl-messages').ok,
+ ok_ = require('../repl-messages').ok_,
+ errorWhile = require('../repl-messages').errorWhile,
+ setupOpenWhisk = require('../util').setupOpenWhisk;
+
+/**
+ * Delete an action
+ *
+ */
+exports.deleteAction = function deleteAction(wskprops, next, name) {
+ var ow = setupOpenWhisk(wskprops);
+
+ function doDelete(name) {
+ ow.actions.delete({ actionName: name })
+ .then((action) => delete created[action.name])
+ .then(ok(next), errorWhile('deleting action', next));
+ }
+
+ if (!name) {
+ _list(ow, function(L) {
+ require('inquirer')
+ .prompt([{ name: 'name', type: 'list',
+ message: 'Which action do you wish to delete',
+ choices: L.map(function(action) { return action.name; })
+ }])
+ .then(function(response) { doDelete(response.name); });
+ });
+ } else {
+ doDelete(name);
+ }
+};
diff --git a/client/lib/commands/list.js b/client/lib/commands/list.js
new file mode 100644
index 0000000..1ac2e52
--- /dev/null
+++ b/client/lib/commands/list.js
@@ -0,0 +1,35 @@
+var created = require('./create').created,
+ Namer = require('../namer'),
+ ok = require('../repl-messages').ok,
+ ok_ = require('../repl-messages').ok_,
+ errorWhile = require('../repl-messages').errorWhile,
+ setupOpenWhisk = require('../util').setupOpenWhisk;
+
+exports._list = function _list(ow, callback, type) {
+ ow[type || 'actions']
+ .list({ limit: 200 })
+ .then(function onList(L) { callback(L, ow); },
+ errorWhile('fetching actions', callback));
+};
+
+exports.list = function list(wskprops, callback, type) {
+ var ow = setupOpenWhisk(wskprops);
+ exports._list(ow, callback, type);
+};
+
+exports.listToConsole = function listToConsole(wskprops, options, next) {
+ if (options.help) {
+ return next();
+ }
+
+ console.log('Available actions:'.blue);
+ function print(actions) {
+ actions
+ .filter(action => options && options.full || !Namer.isDebugArtifact(action.name))
+ .forEach(action => console.log(' ', action.name[created[action.name] ? 'green' : 'reset']));
+
+ ok_(next);
+ }
+
+ exports.list(wskprops, print);
+};
diff --git a/client/lib/namer.js b/client/lib/namer.js
new file mode 100644
index 0000000..1212f66
--- /dev/null
+++ b/client/lib/namer.js
@@ -0,0 +1,15 @@
+var uuid = require('uuid');
+
+/**
+ *
+ * @return a new unique name for an entity
+ */
+exports.prefix = '___debug___';
+
+exports.name = function name(extra) {
+ return exports.prefix + (extra ? extra + '-' : '') + uuid.v4();
+};
+
+exports.isDebugArtifact = function isDebugArtifact(name) {
+ return name.indexOf(exports.prefix) === 0;
+};
diff --git a/client/lib/repl-messages.js b/client/lib/repl-messages.js
new file mode 100644
index 0000000..9d44492
--- /dev/null
+++ b/client/lib/repl-messages.js
@@ -0,0 +1,24 @@
+exports.ok = function ok(next) {
+ return function() {
+ console.log('ok');
+ next();
+ };
+};
+
+exports.ok_ = function ok_(next) {
+ exports.ok(next)();
+};
+
+/**
+ * Log an error, and continue
+ *
+ */
+exports.errorWhile = function errorWhile(inOperation, callback) {
+ return function(err) {
+ console.error('Error ' + inOperation);
+ console.error(err);
+ if (callback) {
+ callback();
+ }
+ };
+};
diff --git a/client/lib/repl.js b/client/lib/repl.js
index c48ee81..25b77f2 100644
--- a/client/lib/repl.js
+++ b/client/lib/repl.js
@@ -16,6 +16,7 @@
var argv = require('argv'),
prompt = require('inquirer'),
+ lister = require('./commands/list'),
rewriter = require('./rewriter'),
columnify = require('columnify');
@@ -50,7 +51,7 @@
};
var attach = {
handler: rewriter.attach,
- enumerate: rewriter.list,
+ enumerate: lister.list,
description: 'Attach to an action',
synchronous: true,
options: [{ name: 'action-only', short: 'a', type: 'string', description: 'Instrument just the action, not any rules or sequences in which it takes part' }]
@@ -74,7 +75,7 @@
synchronous: true
};
var list = {
- handler: rewriter.listToConsole,
+ handler: lister.listToConsole,
description: 'List available actions',
synchronous: true,
options: [{ name: 'full', short: 'f', type: 'string', description: 'Show all actions, including debugging artifacts' }]
@@ -85,12 +86,12 @@
synchronous: true
};
var create = {
- handler: rewriter.create,
+ handler: require('./commands/create').create,
description: 'Create an action',
synchronous: true
};
var deleteAction = {
- handler: rewriter.deleteAction,
+ handler: require('./commands/delete').deleteAction,
description: 'Delete an action',
synchronous: true
};
diff --git a/client/lib/rewriter.js b/client/lib/rewriter.js
index 037f5cb..da6b625 100644
--- a/client/lib/rewriter.js
+++ b/client/lib/rewriter.js
@@ -15,7 +15,14 @@
*/
var uuid = require('uuid'),
+ inquirer = require('inquirer'),
openwhisk = require('openwhisk'),
+ setupOpenWhisk = require('./util').setupOpenWhisk,
+ lister = require('./commands/list'),
+ Namer = require('./namer'),
+ ok = require('./repl-messages').ok,
+ ok_ = require('./repl-messages').ok_,
+ errorWhile = require('./repl-messages').errorWhile,
invokerPackageNamespace = 'nickm@us.ibm.com_canary-advisor', // this is currently housed in one of nick's namespace
invokerPackageName = 'owdbg',
invokerActionName = 'invoker',
@@ -27,7 +34,6 @@
/** the dictionary of live attachments to actions */
var attached = {}, chainAttached = {};
-var created = {};
function echoContinuation(entity, entityNamespace) {
return {
@@ -40,160 +46,6 @@
}
/**
- * Initialize a connection mediator to openwhisk
- *
- */
-function setupOpenWhisk(wskprops) {
- var key = wskprops.AUTH;
- var namespace = wskprops.NAMESPACE;
- var ow = openwhisk({
- api: api.host + api.path,
- api_key: key,
- namespace: namespace
- });
- return ow;
-}
-
-/**
- * Log an error, and continue
- *
- */
-function errorWhile(inOperation, callback) {
- return function(err) {
- console.error('Error ' + inOperation);
- console.error(err);
- if (callback) {
- callback();
- }
- };
-}
-
-function ok(next) {
- return function() {
- console.log('ok');
- next();
- };
-}
-function ok_(next) {
- ok(next)();
-}
-
-/**
- *
- * @return a new unique name for an entity
- */
-var Namer = {
- prefix: '___debug___',
- name: function name(extra) {
- return Namer.prefix + (extra ? extra + '-' : '') + uuid.v4();
- },
- isDebugArtifact: function(name) {
- return name.indexOf(Namer.prefix) === 0;
- }
-};
-
-function _list(ow, callback, type) {
- ow[type || 'actions']
- .list({ limit: 200 })
- .then(function onList(L) { callback(L, ow); },
- errorWhile('fetching actions', callback));
-}
-exports.list = function list(wskprops, callback, type) {
- var ow = setupOpenWhisk(wskprops);
- _list(ow, callback, type);
-};
-
-exports.listToConsole = function listToConsole(wskprops, options, next) {
- if (options.help) {
- return next();
- }
-
- console.log('Available actions:'.blue);
- function print(actions) {
- actions
- .filter(action => options && options.full || !Namer.isDebugArtifact(action.name))
- .forEach(action => console.log(' ', action.name[created[action.name] ? 'green' : 'reset']));
-
- ok_(next);
- }
-
- exports.list(wskprops, print);
-};
-
-/**
- * Create an action
- *
- */
-exports.create = function create(wskprops, next, name) {
- var questions = [];
- if (!name) {
- questions.push({ name: 'name', message: 'Choose a name for your new action' });
- }
- questions.push({ name: 'kind', type: 'list',
- message: 'Which runtime do you want to use?',
- choices: ['nodejs', 'swift', 'python' ]
- });
- questions.push({ name: 'code', type: 'editor',
- message: 'Please provide the function body for your new action',
- default: function(response) {
- if (response.kind === 'nodejs') {
- // nodejs
- return 'function main(params) {\n return { message: \'hello\' };\n}\n';
- } else if (response.kind === 'swift') {
- // swift
- return 'func main(args: [String:Any]) -> [String:Any] {\n return [ "message" : "Hello world" ]\n}\n';
- } else {
- // python
- return 'import sys\n\ndef main(dict):\n return { \'message\': \'Hello world\' }\n';
- }
- }
- });
-
- require('inquirer')
- .prompt(questions)
- .then(response => {
- return setupOpenWhisk(wskprops).actions.create({
- actionName: name || response.name,
- action: {
- exec: {
- kind: response.kind,
- code: response.code
- }
- }
- });
- })
- .then((action) => created[action.name] = true)
- .then(ok(next), errorWhile('creating action', next));
-};
-
-/**
- * Delete an action
- *
- */
-exports.deleteAction = function deleteAction(wskprops, next, name) {
- var ow = setupOpenWhisk(wskprops);
-
- function doDelete(name) {
- ow.actions.delete({ actionName: name })
- .then((action) => delete created[action.name])
- .then(ok(next), errorWhile('deleting action', next));
- }
-
- if (!name) {
- _list(ow, function(L) {
- require('inquirer')
- .prompt([{ name: 'name', type: 'list',
- message: 'Which action do you wish to delete',
- choices: L.map(function(action) { return action.name; })
- }])
- .then(function(response) { doDelete(response.name); });
- });
- } else {
- doDelete(name);
- }
-};
-
-/**
* Clean up any residual debugging artifacts
*
*/
@@ -203,7 +55,7 @@
// console.log('Cleaning ' + types);
return new Promise(function(resolve, reject) {
- exports.list(wskprops, function onList(entities, ow) {
+ lister.list(wskprops, function onList(entities, ow) {
var toClean = entities.filter(function(entity) {
return Namer.isDebugArtifact(entity.name);
});
@@ -415,7 +267,7 @@
//
return next();
}
- _list(ow, function onList(entities) {
+ lister._list(ow, function onList(entities) {
var counter = entities.length;
function countDown(names) {
if (--counter <= 0) {
@@ -533,7 +385,7 @@
console.error('No attached actions detected');
next();
} else {
- require('inquirer')
+ inquirer
.prompt([{ name: 'name', type: 'list',
message: 'From which action do you wish to detach',
choices: L
diff --git a/client/lib/util.js b/client/lib/util.js
new file mode 100644
index 0000000..eb49961
--- /dev/null
+++ b/client/lib/util.js
@@ -0,0 +1,20 @@
+var openwhisk = require('openwhisk'),
+ api = {
+ host: 'https://openwhisk.ng.bluemix.net',
+ path: '/api/v1'
+ };
+
+/**
+ * Initialize a connection mediator to openwhisk
+ *
+ */
+exports.setupOpenWhisk = function setupOpenWhisk(wskprops) {
+ var key = wskprops.AUTH;
+ var namespace = wskprops.NAMESPACE;
+ var ow = openwhisk({
+ api: api.host + api.path,
+ api_key: key,
+ namespace: namespace
+ });
+ return ow;
+};
diff --git a/client/package.json b/client/package.json
index 55096ae..26b1367 100644
--- a/client/package.json
+++ b/client/package.json
@@ -5,7 +5,7 @@
"repository": "https://github.ibm.com/nickm/owdbg",
"main": "client.js",
"scripts": {
- "test": "./node_modules/.bin/jshint wskdb.js lib/*.js && ava --verbose"
+ "test": "./node_modules/.bin/jshint wskdb.js lib/*.js lib/commands/*.js && ava --verbose"
},
"author": "",
"license": "ISC",