blob: b9311457fb419e40e9566087436713a133867c6c [file] [log] [blame]
// Generated by CoffeeScript 1.10.0
/*
Please note the javascript is being fully generated from coffeescript.
So make your changes in the .coffee file.
Thatcher Peskens
_
,_(')<
\___)
*/
(function() {
(this.myTerminal = function() {
var EMULATOR_VERSION, bash, cat, parseInput, util_slow_lines, wait, wsk, wsk_action_invoke_blocking_hello, wsk_action_invoke_hello, wsk_activation_list, wsk_activation_result, wsk_cat_helloWorld, wsk_create_action_hello, wsk_create_action_hello_v, wsk_create_action_python, wsk_create_action_sequence, wsk_help, wsk_invalid_choice, wsk_list_action_hello, wsk_no_args, wsk_package_created, wsk_package_get, wsk_rule_created, wsk_trigger_created, wsk_trigger_deleted, wsk_unrecognized_arguments;
EMULATOR_VERSION = "0.1.3";
this.basesettings = {
prompt: 'you@tutorial:~$ ',
greetings: "Welcome to the interactive OpenWhisk CLI tutorial"
};
/*
Callback definitions. These can be overridden by functions anywhere else
*/
this.preventDefaultCallback = false;
this.immediateCallback = function(command) {
console.debug("immediate callback from " + command);
};
this.finishedCallback = function(command) {
console.debug("finished callback from " + command);
};
this.intermediateResults = function(string) {
console.debug("sent " + string);
};
/*
Base interpreter
*/
this.interpreter = function(input, term) {
var command, inputs;
inputs = input.split(" ");
command = inputs[0];
if (command === 'hi') {
term.echo('hi there! What is your name??');
term.push(function(command, term) {
return term.echo(command + ' is a pretty name');
});
} else if (command === 'shell') {
term.push(function(command, term) {
if (command === 'cd') {
return bash(term, inputs);
}
}, {
prompt: '> $ '
});
} else if (command === 'r') {
location.reload('forceGet');
} else if (command === '#') {
term.echo('which question?');
} else if (command === 'test') {
term.echo('I have to keep testing myself.');
} else if (command === 'cd') {
bash(term, inputs);
} else if (command === "wsk") {
wsk(term, inputs);
} else if (command === "help") {
term.echo(help);
} else if (command === "ls") {
term.echo("This is an emulator, not a shell. Try following the instructions.");
} else if (command === "pull") {
term.echo('[[b;#fff;]some text]');
wait(term, 5000, true);
alert(term.get_output());
return;
} else if (command) {
term.echo(inputs[0] + ": command not found");
}
return immediateCallback(inputs);
};
/* =======================================
Common utils
=======================================
*/
String.prototype.beginsWith = function(string) {
/*
Check if 'this' string starts with the inputstring.
*/
return this.indexOf(string) === 0;
};
Array.prototype.containsAllOfThese = function(inputArr) {
/*
This function compares all of the elements in the inputArr
and checks them one by one if they exist in 'this'. When it
finds an element to not exist, it returns false.
*/
var me, valid;
me = this;
valid = false;
if (inputArr) {
valid = inputArr.every(function(value) {
if (me.indexOf(value) === -1) {
return false;
} else {
return true;
}
});
}
return valid;
};
Array.prototype.containsAllOfTheseParts = function(inputArr) {
/*
This function is like containsAllofThese, but also matches partial strings.
*/
var me, valid;
me = this;
if (inputArr) {
valid = inputArr.every(function(value) {
var item, k, len;
for (k = 0, len = me.length; k < len; k++) {
item = me[k];
if (item.match(value)) {
return true;
}
}
return false;
});
}
return valid;
};
parseInput = function(inputs) {
var command, commands, imagename, input, j, k, len, parsed_input, switchArg, switchArgs, switches;
command = inputs[1];
switches = [];
switchArg = false;
switchArgs = [];
imagename = "";
commands = [];
j = 0;
for (k = 0, len = inputs.length; k < len; k++) {
input = inputs[k];
if (input.startsWith('-') && imagename === "") {
switches.push(input);
if (switches.length > 0) {
if (!['-i', '-t', '-d'].containsAllOfThese([input])) {
switchArg = true;
}
}
} else if (switchArg === true) {
switchArg = false;
switchArgs.push(input);
} else if (j > 1 && imagename === "") {
imagename = input;
} else if (imagename !== "") {
commands.push(input);
} else {
}
j++;
}
parsed_input = {
'switches': switches.sortBy(),
'switchArgs': switchArgs,
'imageName': imagename,
'commands': commands
};
return parsed_input;
};
util_slow_lines = function(term, paragraph, keyword, finishedCallback) {
var foo, i, lines;
if (keyword) {
lines = paragraph(keyword).split("\n");
} else {
lines = paragraph.split("\n");
}
term.pause();
i = 0;
foo = function(lines) {
return self.setTimeout((function() {
if (lines[i]) {
term.echo(lines[i]);
i++;
return foo(lines);
} else {
term.resume();
return finishedCallback();
}
}), 1000);
};
return foo(lines);
};
wait = function(term, time, dots) {
var interval_id;
term.echo("starting to wait");
interval_id = self.setInterval((function() {
return dots != null ? dots : term.insert('.');
}), 500);
return self.setTimeout((function() {
var output;
self.clearInterval(interval_id);
output = term.get_command();
term.echo(output);
return term.echo("done ");
}), time);
};
/*
Bash program
*/
bash = function(term, inputs) {
var argument, echo, insert;
echo = term.echo;
insert = term.insert;
if (!inputs[1]) {
return console.log("none");
} else {
argument = inputs[1];
if (argument.beginsWith('..')) {
return echo("-bash: cd: " + argument + ": Permission denied");
} else {
return echo("-bash: cd: " + argument + ": No such file or directory");
}
}
};
cat = function(term, inputs) {
var echo;
echo = term.echo;
if (inputs[1] === "hello.js") {
echo(wsk_cat_helloWorld);
}
};
wsk = function(term, inputs) {
var callback, command, echo, insert;
echo = term.echo;
insert = term.insert;
callback = function() {
return this.finishedCallback(inputs);
};
command = inputs[1];
if (!inputs[1]) {
console.debug("no args");
echo(wsk_no_args);
} else if (inputs[1] === "--help" || inputs[1] === "-h") {
echo(wsk_help);
} else if (inputs[1] === "action") {
if (inputs[2] === "create") {
if (inputs[3] === "hello") {
if (inputs[4] === "hello.js") {
echo(wsk_create_action_hello);
}
}
if (inputs[3] === "sequenceOfActions") {
if (inputs[4] === "--sequence") {
if (inputs[5] === "/whisk.system/util/cat,/whisk.system/util/sort") {
intermediateResults(0);
echo(wsk_create_action_sequence);
} else {
echo(wsk_unrecognized_arguments);
}
}
}
if (inputs[3] === "helloPython") {
if (inputs[4] === "hello.py") {
echo(wsk_create_action_python);
}
}
} else if (inputs[2] === "list") {
echo(wsk_list_action_hello);
} else if (inputs[2] === "invoke") {
if (inputs[3] === "hello") {
echo(wsk_action_invoke_hello);
} else if (inputs[3] === "--blocking") {
if (inputs[4] === "hello") {
echo(wsk_action_invoke_blocking_hello);
}
} else {
echo(wsk_no_args);
}
}
} else if (inputs[1] === "trigger") {
if (inputs[2] === "create") {
if (inputs[3] === "myTrigger") {
intermediateResults(0);
echo(wsk_trigger_created);
}
}
if (inputs[2] === "delete") {
if (inputs[3] === "myTrigger") {
intermediateResults(0);
echo(wsk_trigger_deleted);
}
}
} else if (inputs[1] === "rule") {
if (inputs[2] === "create") {
if (inputs[3] === "myRule") {
if (inputs[4] === "myTrigger") {
if (inputs[5] === "hello") {
intermediateResults(0);
echo(wsk_rule_created);
}
}
}
}
} else if (inputs[1] === "package") {
if (inputs[2] === "create") {
if (inputs[3] === "myPackage") {
intermediateResults(0);
echo(wsk_package_created);
}
}
} else if (inputs[1] === "activation") {
if (inputs[2] === "result") {
if (inputs[3] === "6bf1f670ee614a7eb5af3c9fde813043") {
echo(wsk_activation_result);
}
} else if (inputs[2] === "list") {
echo(wsk_activation_list);
}
} else if (inputs[1] === "-v") {
if (inputs[2] === "action") {
if (inputs[3] === "create") {
if (inputs[4] === "hello") {
if (inputs[5] === "hello.js") {
echo(wsk_create_action_hello_v);
}
}
}
}
} else if (inputs[1] === "images") {
echo(currentCloudImages);
}
};
wsk_help = "Usage:\n wsk [command]\n\nAvailable Commands:\n action work with actions\n activation work with activations\n package work with packages\n rule work with rules\n trigger work with triggers\n sdk work with the sdk\n property work with whisk properties\n namespace work with namespaces\n list list available namespaces\n\nFlags:\n --apihost HOST whisk API HOST\n --apiversion VERSION whisk API VERSION\n -u, --auth KEY authorization KEY\n -d, --debug debug level output\n -h, --help help for wsk\n -i, --insecure bypass certificate checking\n -v, --verbose verbose output\n\nUse \"wsk [command] --help\" for more information about a command.";
wsk_invalid_choice = "usage: wsk [-h] [-v] [--apihost hostname] [--apiversion version]\n {action,activation,namespace,package,rule,trigger,sdk,property,list}\n ...\nwsk: error: argument cmd: invalid choice: (choose from 'action', 'activation', 'namespace', 'package', 'rule', 'trigger', 'sdk', 'property', 'list')";
wsk_trigger_created = "ok: created trigger myTrigger";
wsk_trigger_deleted = "ok deleted myTrigger";
wsk_rule_created = "ok: created rule myRule";
wsk_package_created = "ok: create package myPackage";
wsk_cat_helloWorld = "function main(params) {\n return {payload: 'Hello world'};\n}";
wsk_create_action_hello = "ok: created action hello";
wsk_create_action_python = "ok: created action helloPython";
wsk_create_action_hello_v = "{'apihost': 'openwhisk.ng.bluemix.net', 'namespace': 'jstart', 'clibuild': '2016-03-03T09:55:47-06:00', 'apiversion': 'v1'}\n========\nREQUEST:\nPUT https://openwhisk.ng.bluemix.net/api/v1/namespaces/jstart/actions/hello\nHeaders sent:\n{\n \"Authorization\": \"Basic \n UyLWJJkYu65JKhu7YjM0ZDcwODhlNzBiOmlFS3RWMHl0UWdIT1SxUGNrMUFJRHUzSF2VlFSV53hDUnZlVXhyMGJpbTBGeH827=\",\n \"Content-Type\": \"application/json\"\n}\nBody sent:\n{\"exec\": {\"kind\": \"nodejs\", \"code\": \"function main(params) {\n return {payload: 'Hello, ' + params.name + ' from ' + params.place};\n}\n\n\"}}\n--------\nRESPONSE:\nGot response with code 200\nBody received:\n{\n \"name\": \"hello\",\n \"publish\": false,\n \"annotations\": [],\n \"version\": \"0.0.1\",\n \"exec\": {\n \"kind\": \"nodejs\",\n \"code\": \"function main(params) {\n return {payload: 'Hello, ' + params.name + ' from ' + params.place};\n}\n\n\"\n },\n \"parameters\": [],\n \"limits\": {\n \"timeout\": 60000,\n \"memory\": 256\n },\n \"namespace\": \"jstart\"\n}\n========\nok: created action hello";
wsk_list_action_hello = "actions\nhello private";
wsk_action_invoke_hello = "ok: invoked hello with id 6bf1f670ee614a7eb5af3c9fde813043";
wsk_action_invoke_blocking_hello = "ok: invoked hello with id 44794bd6aab74415b4e42a308d880e5b\nresponse:\n{\n \"result\": {\n \"payload\": \"Hello world\"\n },\n \"status\": \"success\",\n \"success\": true\n}";
wsk_activation_result = "{\n \"payload\" : \"Hello world\"\n}";
wsk_activation_list = "activations\n44794bd6aab74415b4e42a308d880e5b hello\n6bf1f670ee614a7eb5af3c9fde813043 hello";
wsk_no_args = "Usage:\n wsk [command]\n\nAvailable Commands:\n action work with actions\n activation work with activations\n package work with packages\n rule work with rules\n trigger work with triggers\n sdk work with the sdk\n property work with whisk properties\n namespace work with namespaces\n list list available namespaces\n\nFlags:\n --apihost HOST whisk API HOST\n --apiversion VERSION whisk API VERSION\n -u, --auth KEY authorization KEY\n -d, --debug debug level output\n -h, --help help for wsk\n -i, --insecure bypass certificate checking\n -v, --verbose verbose output\n\nUse \"wsk [command] --help\" for more information about a command.";
wsk_create_action_sequence = "ok: created action sequenceOfActions";
wsk_unrecognized_arguments = "Usage:\n wsk [command]\n\nAvailable Commands:\n action work with actions\n activation work with activations\n package work with packages\n rule work with rules\n trigger work with triggers\n sdk work with the sdk\n property work with whisk properties\n namespace work with namespaces\n list list available namespaces\n\nFlags:\n --apihost HOST whisk API HOST\n --apiversion VERSION whisk API VERSION\n -u, --auth KEY authorization KEY\n -d, --debug debug level output\n -h, --help help for wsk\n -i, --insecure bypass certificate checking\n -v, --verbose verbose output\n\nUse \"wsk [command] --help\" for more information about a command.";
return wsk_package_get = "package /whisk.system/util\naction /whisk.system/util/cat: Concatenate array of strings, and split lines into an array\naction /whisk.system/util/head: Filter first K array elements and discard rest\naction /whisk.system/util/date: Get current date and time\naction /whisk.system/util/sort: Sort array";
})();
return this;
}).call(this);