diff --git a/hsarjcli/src/de/hsadmin/jscli/Main.java b/hsarjcli/src/de/hsadmin/jscli/Main.java index 26a4f93..f2f98e6 100644 --- a/hsarjcli/src/de/hsadmin/jscli/Main.java +++ b/hsarjcli/src/de/hsadmin/jscli/Main.java @@ -1,6 +1,6 @@ package de.hsadmin.jscli; -import java.io.File; +import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.InputStreamReader; @@ -19,19 +19,37 @@ public class Main { final CommandlineParser cmdParser = new CommandlineParser(args); final String runAs = cmdParser.getRunAs(); console.open(runAs + "@hsadmin> "); - final ScriptClient scriptClient = new ScriptClient(console, cmdParser.getUser(), runAs); + final ScriptClient scriptClient = new ScriptClient(console, cmdParser.getUser(), runAs, cmdParser.getArgs()); final String file = cmdParser.getFile(); if (file != null && file.length() > 0) { if ("-".equals(file)) { scriptClient.execute(new InputStreamReader(System.in)); console.println(formatter.format(scriptClient.getLastRpcResult())); } else { - File fileHandle = new File(file); + BufferedReader bufferedReader = null; try { - scriptClient.execute(new FileReader(fileHandle)); - console.println(formatter.format(scriptClient.getLastRpcResult())); + bufferedReader = new BufferedReader(new FileReader(file)); + String inputLine = bufferedReader.readLine(); + boolean isFirstLine = true; + final StringBuffer scriptFromFile = new StringBuffer(); + while (inputLine != null) { + if (isFirstLine && inputLine.startsWith("#!")) { + scriptFromFile.append("//"); + scriptFromFile.append(inputLine.substring(2)); + } else { + scriptFromFile.append(inputLine); + } + scriptFromFile.append("\n"); + isFirstLine = false; + inputLine = bufferedReader.readLine(); + } + scriptClient.execute(scriptFromFile.toString()); } catch (FileNotFoundException e) { System.err.println("File not found: " + file); + } finally { + if (bufferedReader != null) { + bufferedReader.close(); + } } } } diff --git a/hsarjcli/src/de/hsadmin/jscli/ScriptClient.java b/hsarjcli/src/de/hsadmin/jscli/ScriptClient.java index 9322ff9..b4c7870 100644 --- a/hsarjcli/src/de/hsadmin/jscli/ScriptClient.java +++ b/hsarjcli/src/de/hsadmin/jscli/ScriptClient.java @@ -20,7 +20,7 @@ public class ScriptClient { final private ScriptEngine engine; final private Set completionStrings; - public ScriptClient(final ConsoleWrapper console, final String user, final String runAs) throws JSCliException { + public ScriptClient(final ConsoleWrapper console, final String user, final String runAs, final String... arguments) throws JSCliException { final CASTicketProvider ticketProvider = new CASTicketProvider(console, user, runAs); final RpcClient rpcClient = new RpcClient(ticketProvider); final ScriptEngineManager engineManager = new ScriptEngineManager(); @@ -31,6 +31,7 @@ public class ScriptClient { completionStrings = new HashSet(); completionStrings.add("set"); completionStrings.add("where"); + considerArguments(arguments); try { final InputStream inputResource = getClass().getClassLoader().getResourceAsStream("js/functions.js"); engine.eval(new InputStreamReader(inputResource)); @@ -69,7 +70,7 @@ public class ScriptClient { } console.codeCompletion(getCodeCompletionStrings()); } - + public String[] getCodeCompletionStrings() { final String[] codeCompletionStrings = new String[completionStrings.size()]; int idx = 0; @@ -101,5 +102,26 @@ public class ScriptClient { public Object getLastRpcResult() { return engine.get("xmlrpcLastResult"); } + + private void considerArguments(final String... arguments) + throws JSCliException { + final StringBuilder argsBuilder = new StringBuilder("var arguments = [ "); + boolean isFirstArg = true; + for (final String arg : arguments) { + if (!isFirstArg) { + argsBuilder.append(", "); + } + argsBuilder.append('\''); + argsBuilder.append(arg); + argsBuilder.append('\''); + isFirstArg = false; + } + argsBuilder.append(" ];"); + try { + engine.eval(argsBuilder.toString()); + } catch (ScriptException e) { + throw new JSCliException(e); + } + } } diff --git a/hsarjcli/src/de/hsadmin/jscli/conf/CommandlineParser.java b/hsarjcli/src/de/hsadmin/jscli/conf/CommandlineParser.java index 2fe573f..3de7095 100644 --- a/hsarjcli/src/de/hsadmin/jscli/conf/CommandlineParser.java +++ b/hsarjcli/src/de/hsadmin/jscli/conf/CommandlineParser.java @@ -63,4 +63,8 @@ public class CommandlineParser { formatter.printHelp("hsscript", opts); } + public String[] getArgs() { + return cmd.getArgs(); + } + }