support shebang

This commit is contained in:
Peter Hormanns 2014-09-30 18:08:58 +02:00
parent 713099eda5
commit 26a849dd26
3 changed files with 51 additions and 7 deletions

View File

@ -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();
}
}
}
}

View File

@ -20,7 +20,7 @@ public class ScriptClient {
final private ScriptEngine engine;
final private Set<String> 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<String>();
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);
}
}
}

View File

@ -63,4 +63,8 @@ public class CommandlineParser {
formatter.printHelp("hsscript", opts);
}
public String[] getArgs() {
return cmd.getArgs();
}
}