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