Search in sources :

Example 1 with IO

use of org.codehaus.groovy.tools.shell.IO in project groovy-core by groovy.

the class Logger method log.

private void log(final String level, Object msg, Throwable cause) {
    assert level != null;
    assert msg != null;
    if (io == null) {
        io = new IO();
    }
    // Allow the msg to be a Throwable, and handle it properly if no cause is given
    if (cause == null) {
        if (msg instanceof Throwable) {
            cause = (Throwable) msg;
            msg = cause.getMessage();
        }
    }
    Color color = GREEN;
    if (WARN.equals(level) || ERROR.equals(level)) {
        color = RED;
    }
    io.out.println(ansi().a(INTENSITY_BOLD).fg(color).a(level).reset().a(" [").a(name).a("] ").a(msg));
    if (cause != null) {
        cause.printStackTrace(io.out);
    }
    try {
        io.flush();
    } catch (IOException io) {
        throw new RuntimeException(io);
    }
}
Also used : IO(org.codehaus.groovy.tools.shell.IO) Color(org.fusesource.jansi.Ansi.Color) IOException(java.io.IOException)

Example 2 with IO

use of org.codehaus.groovy.tools.shell.IO in project jackrabbit-oak by apache.

the class Console method main.

public static void main(String[] args) throws Exception {
    OptionParser parser = new OptionParser();
    OptionSpec quiet = parser.accepts("quiet", "be less chatty");
    OptionSpec shell = parser.accepts("shell", "run the shell after executing files");
    Options opts = new Options();
    OptionSet options = opts.parseAndConfigure(parser, args);
    NodeStoreFixture fixture = NodeStoreFixtureProvider.create(opts);
    List<String> nonOptions = opts.getCommonOpts().getNonOptions();
    List<String> scriptArgs = nonOptions.size() > 1 ? nonOptions.subList(1, nonOptions.size()) : Collections.emptyList();
    IO io = new IO();
    if (options.has(quiet)) {
        io.setVerbosity(IO.Verbosity.QUIET);
    }
    if (!opts.getCommonOpts().isReadWrite()) {
        io.out.println("Repository connected in read-only mode. Use '--read-write' for write operations");
    }
    GroovyConsole console = new GroovyConsole(ConsoleSession.create(fixture.getStore(), fixture.getWhiteboard()), new IO(), fixture);
    int code = 0;
    if (!scriptArgs.isEmpty()) {
        code = console.execute(scriptArgs);
    }
    if (scriptArgs.isEmpty() || options.has(shell)) {
        code = console.run();
    }
    System.exit(code);
}
Also used : OptionSpec(joptsimple.OptionSpec) Options(org.apache.jackrabbit.oak.run.cli.Options) IO(org.codehaus.groovy.tools.shell.IO) NodeStoreFixture(org.apache.jackrabbit.oak.run.cli.NodeStoreFixture) OptionSet(joptsimple.OptionSet) OptionParser(joptsimple.OptionParser)

Example 3 with IO

use of org.codehaus.groovy.tools.shell.IO in project hudson-2.x by hudson.

the class GroovyshCommand method createShell.

protected Groovysh createShell(InputStream stdin, PrintStream stdout, PrintStream stderr) {
    Binding binding = new Binding();
    // redirect "println" to the CLI
    binding.setProperty("out", new PrintWriter(stdout, true));
    binding.setProperty("hudson", hudson.model.Hudson.getInstance());
    IO io = new IO(new BufferedInputStream(stdin), stdout, stderr);
    final ClassLoader cl = Thread.currentThread().getContextClassLoader();
    Closure registrar = new Closure(null, null) {

        public Object doCall(Object[] args) {
            assert (args.length == 1);
            assert (args[0] instanceof Shell);
            Shell shell = (Shell) args[0];
            XmlCommandRegistrar r = new XmlCommandRegistrar(shell, cl);
            r.register(GroovyshCommand.class.getResource("commands.xml"));
            return null;
        }
    };
    Groovysh shell = new Groovysh(cl, binding, io, registrar);
    shell.getImports().add("import hudson.model.*");
    // defaultErrorHook doesn't re-throw IOException, so ShellRunner in
    // Groovysh will keep looping forever if we don't terminate when the
    // channel is closed
    final Closure originalErrorHook = shell.getErrorHook();
    shell.setErrorHook(new Closure(shell, shell) {

        public Object doCall(Object[] args) throws ChannelClosedException {
            if (args.length == 1 && args[0] instanceof ChannelClosedException) {
                throw (ChannelClosedException) args[0];
            }
            return originalErrorHook.call(args);
        }
    });
    return shell;
}
Also used : Binding(groovy.lang.Binding) Closure(groovy.lang.Closure) IO(org.codehaus.groovy.tools.shell.IO) ChannelClosedException(hudson.remoting.ChannelClosedException) Shell(org.codehaus.groovy.tools.shell.Shell) Groovysh(org.codehaus.groovy.tools.shell.Groovysh) BufferedInputStream(java.io.BufferedInputStream) XmlCommandRegistrar(org.codehaus.groovy.tools.shell.util.XmlCommandRegistrar) PrintWriter(java.io.PrintWriter)

Example 4 with IO

use of org.codehaus.groovy.tools.shell.IO in project groovy by apache.

the class Logger method log.

private void log(final String level, Object msg, Throwable cause) {
    assert level != null;
    assert msg != null;
    if (io == null) {
        synchronized (Logger.class) {
            if (io == null) {
                io = new IO();
            }
        }
    }
    // Allow the msg to be a Throwable, and handle it properly if no cause is given
    if (cause == null) {
        if (msg instanceof Throwable) {
            cause = (Throwable) msg;
            msg = cause.getMessage();
        }
    }
    Color color = GREEN;
    if (WARN.equals(level) || ERROR.equals(level)) {
        color = RED;
    }
    io.out.println(ansi().a(INTENSITY_BOLD).fg(color).a(level).reset().a(" [").a(name).a("] ").a(msg));
    if (cause != null) {
        cause.printStackTrace(io.out);
    }
    io.flush();
}
Also used : IO(org.codehaus.groovy.tools.shell.IO) Color(org.fusesource.jansi.Ansi.Color)

Aggregations

IO (org.codehaus.groovy.tools.shell.IO)4 Color (org.fusesource.jansi.Ansi.Color)2 Binding (groovy.lang.Binding)1 Closure (groovy.lang.Closure)1 ChannelClosedException (hudson.remoting.ChannelClosedException)1 BufferedInputStream (java.io.BufferedInputStream)1 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 OptionParser (joptsimple.OptionParser)1 OptionSet (joptsimple.OptionSet)1 OptionSpec (joptsimple.OptionSpec)1 NodeStoreFixture (org.apache.jackrabbit.oak.run.cli.NodeStoreFixture)1 Options (org.apache.jackrabbit.oak.run.cli.Options)1 Groovysh (org.codehaus.groovy.tools.shell.Groovysh)1 Shell (org.codehaus.groovy.tools.shell.Shell)1 XmlCommandRegistrar (org.codehaus.groovy.tools.shell.util.XmlCommandRegistrar)1