Search in sources :

Example 1 with Command

use of org.aesh.command.Command in project wildfly-core by wildfly.

the class DeployArchiveCommand method buildRequest.

/**
 * null attachments means that the command is in a batch, non null means
 * command executed.
 *
 * Inside a batch, the attachments must be added to the existing batch and
 * NOT to the temporary batch created to build the composite request.
 * Outside of a batch, the attachments MUST be added to the passed non null
 * attachments.
 *
 * @param context
 * @param attachments
 * @return
 * @throws CommandFormatException
 */
@Override
public ModelNode buildRequest(CommandContext context, Attachments attachments) throws CommandFormatException {
    CommandContext ctx = context;
    TempFileProvider tempFileProvider;
    MountHandle root;
    try {
        String name = "cli-" + System.currentTimeMillis();
        tempFileProvider = TempFileProvider.create(name, Executors.newSingleThreadScheduledExecutor((r) -> new Thread(r, "CLI (un)deploy archive tempFile")), true);
        root = extractArchive(file, tempFileProvider, name);
    } catch (IOException e) {
        e.printStackTrace();
        throw new OperationFormatException("Unable to extract archive '" + file.getAbsolutePath() + "' to temporary location");
    }
    Consumer<Attachments> cl = (a) -> {
        VFSUtils.safeClose(root, tempFileProvider);
    };
    if (attachments != null) {
        attachments.addConsumer(cl);
    }
    final File currentDir = ctx.getCurrentDir();
    ctx.setCurrentDir(root.getMountSource());
    String holdbackBatch = activateNewBatch(ctx);
    try {
        if (script == null) {
            script = getDefaultScript();
        }
        File scriptFile = new File(ctx.getCurrentDir(), script);
        if (!scriptFile.exists()) {
            throw new CommandFormatException("ERROR: script '" + script + "' not found.");
        }
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader(scriptFile));
            String line = reader.readLine();
            while (!ctx.isTerminated() && line != null) {
                context.handle(line);
                line = reader.readLine();
            }
        } catch (FileNotFoundException e) {
            throw new CommandFormatException("ERROR: script '" + script + "' not found.");
        } catch (IOException e) {
            throw new CommandFormatException("Failed to read the next command from " + scriptFile.getName() + ": " + e.getMessage(), e);
        } catch (CommandLineException ex) {
            throw new CommandFormatException(ex);
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                }
            }
        }
        return ctx.getBatchManager().getActiveBatch().toRequest();
    } catch (CommandFormatException cfex) {
        cl.accept(attachments);
        throw cfex;
    } finally {
        // reset current dir in context
        ctx.setCurrentDir(currentDir);
        discardBatch(ctx, holdbackBatch, attachments, cl);
    }
}
Also used : CommandDefinition(org.aesh.command.CommandDefinition) TempFileProvider(org.jboss.vfs.TempFileProvider) Operation(org.jboss.as.controller.client.Operation) OperationBuilder(org.jboss.as.controller.client.OperationBuilder) OperationFormatException(org.jboss.as.cli.operation.OperationFormatException) LegacyBridge(org.jboss.as.cli.impl.aesh.cmd.LegacyBridge) Command(org.aesh.command.Command) Argument(org.aesh.command.option.Argument) HideOptionActivator(org.wildfly.core.cli.command.aesh.activator.HideOptionActivator) CommandResult(org.aesh.command.CommandResult) BatchCompliantCommand(org.wildfly.core.cli.command.BatchCompliantCommand) AccessRequirements(org.jboss.as.cli.impl.aesh.cmd.deployment.security.AccessRequirements) MountHandle(org.jboss.vfs.spi.MountHandle) Option(org.aesh.command.option.Option) VFS(org.jboss.vfs.VFS) CommandWithPermissions(org.jboss.as.cli.impl.aesh.cmd.deployment.security.CommandWithPermissions) IOException(java.io.IOException) BatchManager(org.jboss.as.cli.batch.BatchManager) ControlledCommandActivator(org.jboss.as.cli.impl.aesh.cmd.security.ControlledCommandActivator) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) Executors(java.util.concurrent.Executors) CommandException(org.aesh.command.CommandException) Util(org.jboss.as.cli.Util) CommandLineException(org.jboss.as.cli.CommandLineException) Consumer(java.util.function.Consumer) VFSUtils(org.jboss.vfs.VFSUtils) Batch(org.jboss.as.cli.batch.Batch) Permissions(org.jboss.as.cli.impl.aesh.cmd.deployment.security.Permissions) CommandContext(org.jboss.as.cli.CommandContext) CommandFormatException(org.jboss.as.cli.CommandFormatException) ModelNode(org.jboss.dmr.ModelNode) CLICommandInvocation(org.wildfly.core.cli.command.aesh.CLICommandInvocation) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) Attachments(org.jboss.as.cli.Attachments) CommandContext(org.jboss.as.cli.CommandContext) OperationFormatException(org.jboss.as.cli.operation.OperationFormatException) MountHandle(org.jboss.vfs.spi.MountHandle) TempFileProvider(org.jboss.vfs.TempFileProvider) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) Attachments(org.jboss.as.cli.Attachments) CommandLineException(org.jboss.as.cli.CommandLineException) CommandFormatException(org.jboss.as.cli.CommandFormatException) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) File(java.io.File)

Example 2 with Command

use of org.aesh.command.Command in project wildfly-core by wildfly.

the class ExtensionsLoader method loadHandlers.

/**
 * Using the client, iterates through the available domain management model extensions
 * and tries to load CLI command handlers from their modules.
 *
 * @param registry
 * @param address
 * @param client
 */
void loadHandlers(ControllerAddress address) throws CommandLineException, CommandLineParserException {
    ModelControllerClient client = ctx.getModelControllerClient();
    assert client != null : "client is null";
    if (moduleLoader == null) {
        ctx.printLine("Warning! The CLI is running in a non-modular environment and cannot load commands from management extensions.");
        return;
    }
    if (address != null && currentAddress != null && address.equals(currentAddress)) {
        return;
    }
    // remove previously loaded commands
    resetHandlers();
    currentAddress = address;
    final ModelNode req = new ModelNode();
    req.get(Util.ADDRESS).setEmptyList();
    req.get(Util.OPERATION).set(Util.READ_CHILDREN_RESOURCES);
    req.get(Util.CHILD_TYPE).set(Util.EXTENSION);
    final ModelNode response;
    try {
        response = client.execute(req);
    } catch (IOException e) {
        throw new CommandLineException("Extensions loader failed to read extensions", e);
    }
    if (!Util.isSuccess(response)) {
        throw new CommandLineException("Extensions loader failed to read extensions: " + Util.getFailureDescription(response));
    }
    final ModelNode result = response.get(Util.RESULT);
    if (!result.isDefined()) {
        throw new CommandLineException("Extensions loader failed to read extensions: " + result.asString());
    }
    for (Property ext : result.asPropertyList()) {
        ModelNode module = ext.getValue().get(Util.MODULE);
        if (!module.isDefined()) {
            addError("Extension " + ext.getName() + " is missing module attribute");
        } else {
            final ModuleIdentifier moduleId = ModuleIdentifier.fromString(module.asString());
            ModuleClassLoader cl;
            try {
                cl = moduleLoader.loadModule(moduleId).getClassLoader();
                ServiceLoader<CommandHandlerProvider> loader = ServiceLoader.load(CommandHandlerProvider.class, cl);
                for (CommandHandlerProvider provider : loader) {
                    try {
                        registry.registerHandler(provider.createCommandHandler(ctx), provider.isTabComplete(), provider.getNames());
                        addHandlers(Arrays.asList(provider.getNames()));
                    } catch (CommandRegistry.RegisterHandlerException e) {
                        addError(e.getLocalizedMessage());
                        final List<String> addedCommands = new ArrayList<String>(Arrays.asList(provider.getNames()));
                        addedCommands.removeAll(e.getNotAddedNames());
                        addHandlers(addedCommands);
                    }
                }
                ServiceLoader<Command> loader2 = ServiceLoader.load(Command.class, cl);
                for (Command provider : loader2) {
                    try {
                        CommandContainer container = aeshRegistry.addCommand(provider);
                        addCommand(container.getParser().getProcessedCommand().name());
                    } catch (CommandLineException e) {
                        addError(e.getLocalizedMessage());
                    }
                }
            } catch (ModuleLoadException e) {
                addError("Module " + module.asString() + " from extension " + ext.getName() + " available on the server couldn't be loaded locally: " + e.getLocalizedMessage());
            }
        }
    }
    if (!errors.isEmpty()) {
        ctx.printLine("Warning! There were errors trying to load extensions. For more details, please, execute 'extension-commands --errors'");
    }
}
Also used : ModuleLoadException(org.jboss.modules.ModuleLoadException) CommandRegistry(org.jboss.as.cli.CommandRegistry) CLICommandRegistry(org.jboss.as.cli.impl.aesh.CLICommandRegistry) ModuleClassLoader(org.jboss.modules.ModuleClassLoader) IOException(java.io.IOException) CommandLineException(org.jboss.as.cli.CommandLineException) CommandHandlerProvider(org.jboss.as.cli.CommandHandlerProvider) ModelControllerClient(org.jboss.as.controller.client.ModelControllerClient) Command(org.aesh.command.Command) CommandContainer(org.aesh.command.container.CommandContainer) ArrayList(java.util.ArrayList) List(java.util.List) ModuleIdentifier(org.jboss.modules.ModuleIdentifier) ModelNode(org.jboss.dmr.ModelNode) Property(org.jboss.dmr.Property)

Example 3 with Command

use of org.aesh.command.Command in project wildfly-core by wildfly.

the class AeshCommands method addExtraCommands.

private void addExtraCommands(Iterable<Command> loader, List<String> filter, Set<String> skip, Map<String, String> renaming) throws CommandLineException, CommandLineParserException {
    for (Command command : loader) {
        if (filter != null && !filter.isEmpty()) {
            String name = getRegistry().getCommandName(command);
            if (!filter.contains(name) || skip.contains(name)) {
                continue;
            }
        }
        CommandContainer c = getRegistry().addCommand(command, renaming, false);
        plugins.add(c.getParser().getProcessedCommand().name());
    }
}
Also used : OperationCommand(org.jboss.as.cli.impl.aesh.cmd.operation.OperationCommandContainer.OperationCommand) Command(org.aesh.command.Command) SpecialCommand(org.jboss.as.cli.impl.aesh.cmd.operation.SpecialCommand) DMRCommand(org.wildfly.core.cli.command.DMRCommand) BatchCompliantCommand(org.wildfly.core.cli.command.BatchCompliantCommand) LegacyCommand(org.jboss.as.cli.impl.aesh.cmd.operation.LegacyCommandContainer.LegacyCommand) CommandContainer(org.aesh.command.container.CommandContainer) OperationCommandContainer(org.jboss.as.cli.impl.aesh.cmd.operation.OperationCommandContainer)

Example 4 with Command

use of org.aesh.command.Command in project infinispan by infinispan.

the class SiteCompleter method getAvailableItems.

@Override
protected Collection<String> getAvailableItems(ContextAwareCompleterInvocation invocation) throws IOException {
    Context context = invocation.context;
    Command<?> cmd = invocation.getCommand();
    Connection connection = context.getConnection();
    Optional<String> cacheName = getCacheName(context, cmd);
    return cacheName.map(name -> getAvailableSites(connection, name)).orElseGet(connection::getSitesView);
}
Also used : Context(org.infinispan.cli.Context) CacheCompleter.getCacheName(org.infinispan.cli.completers.CacheCompleter.getCacheName) Context(org.infinispan.cli.Context) Collection(java.util.Collection) Optional(java.util.Optional) IOException(java.io.IOException) Connection(org.infinispan.cli.connection.Connection) Collections(java.util.Collections) Command(org.aesh.command.Command) Connection(org.infinispan.cli.connection.Connection)

Example 5 with Command

use of org.aesh.command.Command in project wildfly-core by wildfly.

the class HelpSupportTestCase method testStandaloneOnly.

private static void testStandaloneOnly(Class<? extends Command> clazz) throws Exception {
    Command c = clazz.newInstance();
    String synopsis = getStandaloneOnlySynopsis(c);
    Assert.assertEquals(clazz.getName() + ". EXPECTED [" + ((TestCommand) c).getSynopsis() + "]. FOUND [" + synopsis + "]", ((TestCommand) c).getSynopsis(), synopsis);
}
Also used : Command(org.aesh.command.Command)

Aggregations

Command (org.aesh.command.Command)10 IOException (java.io.IOException)5 ArrayList (java.util.ArrayList)3 List (java.util.List)2 CommandDefinition (org.aesh.command.CommandDefinition)2 CommandException (org.aesh.command.CommandException)2 CommandResult (org.aesh.command.CommandResult)2 CommandContainer (org.aesh.command.container.CommandContainer)2 CommandInvocation (org.aesh.command.invocation.CommandInvocation)2 CommandLineException (org.jboss.as.cli.CommandLineException)2 ModelNode (org.jboss.dmr.ModelNode)2 BatchCompliantCommand (org.wildfly.core.cli.command.BatchCompliantCommand)2 BufferedReader (java.io.BufferedReader)1 BufferedWriter (java.io.BufferedWriter)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 FileReader (java.io.FileReader)1 InputStream (java.io.InputStream)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1