support shebang
This commit is contained in:
parent
713099eda5
commit
26a849dd26
@ -1,6 +1,6 @@
|
|||||||
package de.hsadmin.jscli;
|
package de.hsadmin.jscli;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.BufferedReader;
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
import java.io.FileReader;
|
import java.io.FileReader;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
@ -19,19 +19,37 @@ public class Main {
|
|||||||
final CommandlineParser cmdParser = new CommandlineParser(args);
|
final CommandlineParser cmdParser = new CommandlineParser(args);
|
||||||
final String runAs = cmdParser.getRunAs();
|
final String runAs = cmdParser.getRunAs();
|
||||||
console.open(runAs + "@hsadmin> ");
|
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();
|
final String file = cmdParser.getFile();
|
||||||
if (file != null && file.length() > 0) {
|
if (file != null && file.length() > 0) {
|
||||||
if ("-".equals(file)) {
|
if ("-".equals(file)) {
|
||||||
scriptClient.execute(new InputStreamReader(System.in));
|
scriptClient.execute(new InputStreamReader(System.in));
|
||||||
console.println(formatter.format(scriptClient.getLastRpcResult()));
|
console.println(formatter.format(scriptClient.getLastRpcResult()));
|
||||||
} else {
|
} else {
|
||||||
File fileHandle = new File(file);
|
BufferedReader bufferedReader = null;
|
||||||
try {
|
try {
|
||||||
scriptClient.execute(new FileReader(fileHandle));
|
bufferedReader = new BufferedReader(new FileReader(file));
|
||||||
console.println(formatter.format(scriptClient.getLastRpcResult()));
|
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) {
|
} catch (FileNotFoundException e) {
|
||||||
System.err.println("File not found: " + file);
|
System.err.println("File not found: " + file);
|
||||||
|
} finally {
|
||||||
|
if (bufferedReader != null) {
|
||||||
|
bufferedReader.close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -20,7 +20,7 @@ public class ScriptClient {
|
|||||||
final private ScriptEngine engine;
|
final private ScriptEngine engine;
|
||||||
final private Set<String> completionStrings;
|
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 CASTicketProvider ticketProvider = new CASTicketProvider(console, user, runAs);
|
||||||
final RpcClient rpcClient = new RpcClient(ticketProvider);
|
final RpcClient rpcClient = new RpcClient(ticketProvider);
|
||||||
final ScriptEngineManager engineManager = new ScriptEngineManager();
|
final ScriptEngineManager engineManager = new ScriptEngineManager();
|
||||||
@ -31,6 +31,7 @@ public class ScriptClient {
|
|||||||
completionStrings = new HashSet<String>();
|
completionStrings = new HashSet<String>();
|
||||||
completionStrings.add("set");
|
completionStrings.add("set");
|
||||||
completionStrings.add("where");
|
completionStrings.add("where");
|
||||||
|
considerArguments(arguments);
|
||||||
try {
|
try {
|
||||||
final InputStream inputResource = getClass().getClassLoader().getResourceAsStream("js/functions.js");
|
final InputStream inputResource = getClass().getClassLoader().getResourceAsStream("js/functions.js");
|
||||||
engine.eval(new InputStreamReader(inputResource));
|
engine.eval(new InputStreamReader(inputResource));
|
||||||
@ -102,4 +103,25 @@ public class ScriptClient {
|
|||||||
return engine.get("xmlrpcLastResult");
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -63,4 +63,8 @@ public class CommandlineParser {
|
|||||||
formatter.printHelp("hsscript", opts);
|
formatter.printHelp("hsscript", opts);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String[] getArgs() {
|
||||||
|
return cmd.getArgs();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user