
org.crsh.lang.groovy.command.GroovyScriptShellCommand Maven / Gradle / Ivy
/*
* Copyright (C) 2012 eXo Platform SAS.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.crsh.lang.groovy.command;
import groovy.lang.Binding;
import groovy.lang.Closure;
import org.crsh.cli.impl.Delimiter;
import org.crsh.cli.impl.completion.CompletionMatch;
import org.crsh.cli.spi.Completion;
import org.crsh.command.CommandContext;
import org.crsh.command.CommandCreationException;
import org.crsh.command.CommandInvoker;
import org.crsh.command.DescriptionFormat;
import org.crsh.command.InvocationContextImpl;
import org.crsh.command.RuntimeContext;
import org.crsh.command.ShellCommand;
import org.crsh.shell.ErrorType;
import org.crsh.text.RenderPrintWriter;
import org.crsh.util.Strings;
import java.io.IOException;
import java.lang.reflect.UndeclaredThrowableException;
import java.util.List;
import java.util.Map;
/** @author Julien Viet */
public class GroovyScriptShellCommand implements ShellCommand {
/** . */
private final Class clazz;
public GroovyScriptShellCommand(Class clazz) {
this.clazz = clazz;
}
private C createCommand() throws CommandCreationException {
C command;
try {
command = clazz.newInstance();
}
catch (Exception e) {
String name = clazz.getSimpleName();
throw new CommandCreationException(name, ErrorType.INTERNAL, "Could not create command " + name + " instance", e);
}
return command;
}
public final CompletionMatch complete(RuntimeContext context, String line) {
return new CompletionMatch(Delimiter.EMPTY, Completion.create());
}
public final String describe(String line, DescriptionFormat mode) {
return null;
}
public final CommandInvoker, ?> resolveInvoker(String line) throws CommandCreationException {
List chunks = Strings.chunks(line);
String[] args = chunks.toArray(new String[chunks.size()]);
return getInvoker(args);
}
public final CommandInvoker, ?> resolveInvoker(String name, Map options, List> args) throws CommandCreationException {
String[] tmp = new String[args.size()];
for (int i = 0;i < tmp.length;i++) {
tmp[i] = args.get(i).toString();
}
return getInvoker(tmp);
}
private CommandInvoker
© 2015 - 2025 Weber Informatics LLC | Privacy Policy