Search in sources :

Example 1 with Process

use of org.apache.felix.service.command.Process in project felix by apache.

the class Builtin method bg.

public void bg(CommandSession session, String[] argv) {
    final String[] usage = { "bg - put job in background", "Usage: bg [OPTIONS] [jobid]", "  -? --help                show help" };
    Process process = Process.Utils.current();
    Options opt = Options.compile(usage).parse(argv);
    if (opt.isSet("help")) {
        opt.usage(process.err());
        return;
    }
    if (opt.args().size() > 1) {
        process.err().println("usage: bg [jobid]");
        process.error(2);
        return;
    }
    List<Job> jobs = new ArrayList<>(session.jobs());
    Collections.reverse(jobs);
    Job current = Job.Utils.current();
    if (argv.length == 0) {
        Job job = jobs.stream().filter(j -> j != current).findFirst().orElse(null);
        if (job != null) {
            job.background();
        } else {
            process.err().println("bg: no current job");
            process.error(1);
        }
    } else {
        Job job = jobs.stream().filter(j -> j != current && argv[0].equals(Integer.toString(j.id()))).findFirst().orElse(null);
        if (job != null) {
            job.background();
        } else {
            process.err().println("bg: job not found: " + argv[0]);
            process.error(1);
        }
    }
}
Also used : Options(org.jline.builtins.Options) ArrayList(java.util.ArrayList) Process(org.apache.felix.service.command.Process) Job(org.apache.felix.service.command.Job)

Example 2 with Process

use of org.apache.felix.service.command.Process in project felix by apache.

the class Builtin method type.

// FIXME: expose API in runtime so type command doesn't have to duplicate the runtime
// command search strategy.
public boolean type(CommandSession session, String[] argv) throws Exception {
    final String[] usage = { "type - show command type", "Usage: type [OPTIONS] [name[:]]", "  -a --all                 show all matches", "  -? --help                show help", "  -q --quiet               don't print anything, just return status", "  -s --scope=NAME          list all commands in named scope", "  -t --types               show full java type names" };
    Process process = Process.Utils.current();
    Options opt = Options.compile(usage).parse(argv);
    List<String> args = opt.args();
    if (opt.isSet("help")) {
        opt.usage(process.err());
        return true;
    }
    boolean all = opt.isSet("all");
    String optScope = null;
    if (opt.isSet("scope")) {
        optScope = opt.get("scope");
    }
    if (args.size() == 1) {
        String arg = args.get(0);
        if (arg.endsWith(":")) {
            optScope = args.remove(0);
        }
    }
    if (optScope != null || (args.isEmpty() && all)) {
        Set<String> snames = new TreeSet<String>();
        for (String sname : (getCommands(session))) {
            if ((optScope == null) || sname.startsWith(optScope)) {
                snames.add(sname);
            }
        }
        for (String sname : snames) {
            process.out().println(sname);
        }
        return true;
    }
    if (args.size() == 0) {
        Map<String, Integer> scopes = new TreeMap<String, Integer>();
        for (String sname : getCommands(session)) {
            int colon = sname.indexOf(':');
            String scope = sname.substring(0, colon);
            Integer count = scopes.get(scope);
            if (count == null) {
                count = 0;
            }
            scopes.put(scope, ++count);
        }
        for (Entry<String, Integer> entry : scopes.entrySet()) {
            process.out().println(entry.getKey() + ":" + entry.getValue());
        }
        return true;
    }
    final String name = args.get(0).toLowerCase();
    final int colon = name.indexOf(':');
    // FIXME: must match Reflective.java
    final String MAIN = "_main";
    StringBuilder buf = new StringBuilder();
    Set<String> cmds = new LinkedHashSet<String>();
    // get all commands
    if ((colon != -1) || (session.get(name) != null)) {
        cmds.add(name);
    } else if (session.get(MAIN) != null) {
        cmds.add(MAIN);
    } else {
        String path = session.get("SCOPE") != null ? session.get("SCOPE").toString() : "*";
        for (String s : path.split(":")) {
            if (s.equals("*")) {
                for (String sname : getCommands(session)) {
                    if (sname.endsWith(":" + name)) {
                        cmds.add(sname);
                        if (!all) {
                            break;
                        }
                    }
                }
            } else {
                String sname = s + ":" + name;
                if (session.get(sname) != null) {
                    cmds.add(sname);
                    if (!all) {
                        break;
                    }
                }
            }
        }
    }
    for (String key : cmds) {
        Object target = session.get(key);
        if (target == null) {
            continue;
        }
        CharSequence source = getClosureSource(session, key);
        if (source != null) {
            buf.append(name);
            buf.append(" is function {");
            buf.append(source);
            buf.append("}");
            continue;
        }
        for (Method m : getMethods(session, key)) {
            StringBuilder params = new StringBuilder();
            for (Class<?> type : m.getParameterTypes()) {
                if (params.length() > 0) {
                    params.append(", ");
                }
                params.append(type.getSimpleName());
            }
            String rtype = m.getReturnType().getSimpleName();
            if (buf.length() > 0) {
                buf.append("\n");
            }
            if (opt.isSet("types")) {
                String cname = m.getDeclaringClass().getName();
                buf.append(String.format("%s %s.%s(%s)", rtype, cname, m.getName(), params));
            } else {
                buf.append(String.format("%s is %s %s(%s)", name, rtype, key, params));
            }
        }
    }
    if (buf.length() > 0) {
        if (!opt.isSet("quiet")) {
            process.out().println(buf);
        }
        return true;
    }
    if (!opt.isSet("quiet")) {
        process.err().println("type: " + name + " not found.");
    }
    return false;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Options(org.jline.builtins.Options) Process(org.apache.felix.service.command.Process) Method(java.lang.reflect.Method) TreeMap(java.util.TreeMap) TreeSet(java.util.TreeSet)

Example 3 with Process

use of org.apache.felix.service.command.Process in project felix by apache.

the class Builtin method set.

public void set(CommandSession session, String[] argv) throws Exception {
    final String[] usage = { "set - show session variables", "Usage: set [OPTIONS] [PREFIX]", "  -? --help                show help", "  -a --all                 show all variables, including those starting with .", "  -x                       set xtrace option", "  +x                       unset xtrace option", "If PREFIX given, then only show variable(s) starting with PREFIX" };
    Process process = Process.Utils.current();
    Options opt = Options.compile(usage).parse(argv);
    if (opt.isSet("help")) {
        opt.usage(process.err());
        return;
    }
    List<String> args = opt.args();
    String prefix = (args.isEmpty() ? "" : args.get(0));
    if (opt.isSet("x")) {
        session.put("echo", true);
    } else if ("+x".equals(prefix)) {
        session.put("echo", null);
    } else {
        boolean all = opt.isSet("all");
        for (String key : new TreeSet<String>(Shell.getVariables(session))) {
            if (!key.startsWith(prefix))
                continue;
            if (key.startsWith(".") && !(all || prefix.length() > 0))
                continue;
            Object target = session.get(key);
            String type = null;
            String value = null;
            if (target != null) {
                Class<? extends Object> clazz = target.getClass();
                type = clazz.getSimpleName();
                value = target.toString();
            }
            String trunc = value == null || value.length() < 55 ? "" : "...";
            process.out().println(String.format("%-15.15s %-15s %.45s%s", type, key, value, trunc));
        }
    }
}
Also used : Options(org.jline.builtins.Options) Process(org.apache.felix.service.command.Process)

Example 4 with Process

use of org.apache.felix.service.command.Process in project felix by apache.

the class Builtin method keymap.

public void keymap(CommandSession session, String[] argv) {
    Process process = Process.Utils.current();
    Commands.keymap(Shell.getReader(session), process.out(), process.err(), argv);
}
Also used : Process(org.apache.felix.service.command.Process)

Example 5 with Process

use of org.apache.felix.service.command.Process in project felix by apache.

the class Builtin method jobs.

public void jobs(CommandSession session, String[] argv) {
    final String[] usage = { "jobs - list jobs", "Usage: jobs [OPTIONS]", "  -? --help                show help" };
    Process process = Process.Utils.current();
    Options opt = Options.compile(usage).parse(argv);
    if (opt.isSet("help")) {
        opt.usage(process.err());
        return;
    }
    if (!opt.args().isEmpty()) {
        process.err().println("usage: jobs");
        process.error(2);
        return;
    }
    List<Job> jobs = session.jobs();
    Job current = Job.Utils.current();
    for (Job job : jobs) {
        if (job != current) {
            process.out().println("[" + job.id() + "] " + job.status().toString().toLowerCase() + " " + job.command());
        }
    }
}
Also used : Options(org.jline.builtins.Options) Process(org.apache.felix.service.command.Process) Job(org.apache.felix.service.command.Job)

Aggregations

Process (org.apache.felix.service.command.Process)16 Options (org.jline.builtins.Options)10 ArrayList (java.util.ArrayList)6 BufferedReader (java.io.BufferedReader)5 InputStreamReader (java.io.InputStreamReader)5 Path (java.nio.file.Path)5 HashSet (java.util.HashSet)5 TreeMap (java.util.TreeMap)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 IOException (java.io.IOException)4 PrintStream (java.io.PrintStream)4 Files (java.nio.file.Files)4 Arrays (java.util.Arrays)4 Collections (java.util.Collections)4 List (java.util.List)4 Map (java.util.Map)4 Set (java.util.Set)4 Job (org.apache.felix.service.command.Job)4 Closeable (java.io.Closeable)3