delete is a reserved word in javascript: use remove instead

interactive mode
This commit is contained in:
Peter Hormanns 2012-07-26 10:59:01 +00:00
parent 6ce4645026
commit e30d26fd77
4 changed files with 36 additions and 6 deletions

View File

@ -19,6 +19,7 @@ public class CommandlineParser {
opts.addOption("r", "runas", true, "specify run-as user"); opts.addOption("r", "runas", true, "specify run-as user");
opts.addOption("e", "expr", true, "expression to execute"); opts.addOption("e", "expr", true, "expression to execute");
opts.addOption("f", "file", true, "script file to execute"); opts.addOption("f", "file", true, "script file to execute");
opts.addOption("i", "interactive", false, "interactive shell");
PosixParser parser = new PosixParser(); PosixParser parser = new PosixParser();
try { try {
if (args.length < 1) { if (args.length < 1) {
@ -51,6 +52,10 @@ public class CommandlineParser {
return cmd.getOptionValue("file", null); return cmd.getOptionValue("file", null);
} }
public boolean isInteractive() {
return cmd.hasOption("interactive");
}
public void printHelp() { public void printHelp() {
HelpFormatter formatter = new HelpFormatter(); HelpFormatter formatter = new HelpFormatter();
formatter.printHelp("hsscript", opts); formatter.printHelp("hsscript", opts);

View File

@ -62,6 +62,7 @@ public class JSONFormatter {
} }
public String format(Object object) { public String format(Object object) {
if (object == null) return "";
if (object instanceof List<?>) { if (object instanceof List<?>) {
return formatList((List<?>) object); return formatList((List<?>) object);
} }

View File

@ -1,5 +1,6 @@
package de.hsadmin.jscli; package de.hsadmin.jscli;
import java.io.Console;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FileReader; import java.io.FileReader;
@ -12,11 +13,8 @@ public class Main {
try { try {
JSONFormatter formatter = new JSONFormatter(); JSONFormatter formatter = new JSONFormatter();
CommandlineParser cmdParser = new CommandlineParser(args); CommandlineParser cmdParser = new CommandlineParser(args);
ScriptClient scriptClient = new ScriptClient(cmdParser.getUser(), cmdParser.getRunAs()); String runAs = cmdParser.getRunAs();
String expr = cmdParser.getExpression(); ScriptClient scriptClient = new ScriptClient(cmdParser.getUser(), runAs);
if (expr != null && expr.length() > 0) {
System.out.println(formatter.format(scriptClient.execute(expr)));
}
String file = cmdParser.getFile(); String file = cmdParser.getFile();
if (file != null && file.length() > 0) { if (file != null && file.length() > 0) {
if ("-".equals(file)) { if ("-".equals(file)) {
@ -30,6 +28,29 @@ public class Main {
} }
} }
} }
String expr = cmdParser.getExpression();
if (expr != null && expr.length() > 0) {
System.out.println(formatter.format(scriptClient.execute(expr)));
}
if (cmdParser.isInteractive()) {
Console console = System.console();
if (console == null) {
throw new JSCliException("fatal error: console not found");
}
String command = console.readLine("%s@hsadmin>", runAs);
while (!("bye".equals(command.trim()) || "exit".equals(command.trim()) || "quit".equals(command.trim()))) {
try {
console.printf("%s\n", formatter.format(scriptClient.execute(command)));
} catch (Exception e) {
console.printf("ERR: %s\n", e.getLocalizedMessage());
}
command = console.readLine("%s@hsadmin>", runAs);
while (command.endsWith("\\")) {
command = command.substring(0, command.length() - 1) +
console.readLine(">");
}
}
}
} catch (JSCliException e) { } catch (JSCliException e) {
System.err.println(e.getMessage()); System.err.println(e.getMessage());
System.exit(-1); System.exit(-1);

View File

@ -51,12 +51,15 @@ public class ScriptClient {
if (parts.length == 2) { if (parts.length == 2) {
String module = parts[0]; String module = parts[0];
String function = parts[1]; String function = parts[1];
if ("delete".equals(function)) {
function = "remove";
}
try { try {
engine.eval("if (typeof " + module + " === 'undefined') " + engine.eval("if (typeof " + module + " === 'undefined') " +
"{ var " + module + " = { }; };\n" + "{ var " + module + " = { }; };\n" +
module + "['" + function + "'] = function(json) { " + module + "['" + function + "'] = function(json) { " +
"var mod = '" + module + "'; " + "var mod = '" + module + "'; " +
"var fct = '" + function + "'; " + ("remove".equals(function) ? "var fct = 'delete'; " : "var fct = '" + function + "'; ") +
"var params = new ArrayList(); " + "var params = new ArrayList(); " +
"params.add(casgrantingticket.getRunAs()); " + "params.add(casgrantingticket.getRunAs()); " +
"params.add(casgrantingticket.getTicket()); " + "params.add(casgrantingticket.getTicket()); " +