add create and delete action support, plus some code cleanups
diff --git a/client/lib/repl.js b/client/lib/repl.js
index cd105e6..6df867d 100644
--- a/client/lib/repl.js
+++ b/client/lib/repl.js
@@ -60,6 +60,17 @@
description: "Clean up debugging artifacts",
synchronous: true
};
+var create = {
+ handler: rewriter.create,
+ description: "Create an action",
+ synchronous: true
+};
+var deleteAction = {
+ handler: rewriter.deleteAction,
+ description: "Delete an action",
+ synchronous: true
+};
+
var commandHandlers = {
list: list,
@@ -82,6 +93,9 @@
clean: clean,
c: clean,
+ create: create,
+ delete: deleteAction,
+
help: help,
h: help,
'?': help
diff --git a/client/lib/rewriter.js b/client/lib/rewriter.js
index 1f3ab7b..238ec67 100644
--- a/client/lib/rewriter.js
+++ b/client/lib/rewriter.js
@@ -44,10 +44,21 @@
function errorWhile(inOperation, callback) {
return function(err) {
console.error('Error ' + inOperation);
+ console.error(err);
callback();
};
}
+function ok(next) {
+ return function() {
+ console.log('ok');
+ next();
+ };
+}
+function ok_(next) {
+ ok(next)();
+}
+
/**
*
* @return a new unique name for an entity
@@ -77,16 +88,77 @@
console.log('Available actions:'.blue);
function print(actions) {
- actions.forEach(function(action) {
- console.log(' ', action.name);
- });
- next();
+ actions.forEach(action => console.log(' ', action.name));
+ 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') return 'function main(params) {\n return { message: \'hello\' };\n}\n'
+ else if (response.kind == 'swift') return 'func main(args: [String:Any]) -> [String:Any] {\n return [ "message" : "Hello world" ]\n}\n'
+ else 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(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(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
*
*/
@@ -120,19 +192,15 @@
});
}
- function allDone() {
- console.log('ok'.blue);
- next();
- }
Promise.all([cleanType('action'),
cleanType('trigger'),
cleanType('package')
])
.then(function() {
cleanType('rule')
- .then(allDone,
- errorWhile('cleaning rules', allDone))
- }, errorWhile('cleaning actions and triggers', allDone));
+ .then(ok(next),
+ errorWhile('cleaning rules', next))
+ }, errorWhile('cleaning actions and triggers', next));
};
/**
@@ -164,7 +232,7 @@
.then(function() {
ow.rules
.create({ ruleName: names.ruleName, trigger: names.triggerName, action: names.continuationName })
- .then(function() { next(names); },
+ .then(ok(function() { next(names); }),
errorWhile('attaching to action', next));
});
} catch (e) {
@@ -228,7 +296,7 @@
var counter = entities.length;
function countDown() {
if (--counter <= 0) {
- next();
+ ok_(next);
}
}
entities.forEach(function(otherEntity) {