HSAdmin Backend Domains, E-Mail, Datenbanken
Peter Hormanns
2014-09-30 26a849dd268a0a01d7de9102563d46b310f96c1f
support shebang
3 files modified
58 ■■■■ changed files
hsarjcli/src/de/hsadmin/jscli/Main.java 28 ●●●● patch | view | raw | blame | history
hsarjcli/src/de/hsadmin/jscli/ScriptClient.java 26 ●●●●● patch | view | raw | blame | history
hsarjcli/src/de/hsadmin/jscli/conf/CommandlineParser.java 4 ●●●● patch | view | raw | blame | history
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 @@
            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();
                        }
                    }
                }
            }
hsarjcli/src/de/hsadmin/jscli/ScriptClient.java
@@ -20,7 +20,7 @@
    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 @@
        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 @@
        }
        console.codeCompletion(getCodeCompletionStrings());
    }
    public String[] getCodeCompletionStrings() {
        final String[] codeCompletionStrings = new String[completionStrings.size()];
        int idx = 0;
@@ -101,5 +102,26 @@
    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);
        }
    }
    
}
hsarjcli/src/de/hsadmin/jscli/conf/CommandlineParser.java
@@ -63,4 +63,8 @@
        formatter.printHelp("hsscript", opts);
    }
    
    public String[] getArgs() {
        return cmd.getArgs();
    }
}