Search in sources :

Example 1 with FilenameTabCompleter

use of org.jboss.as.cli.handlers.FilenameTabCompleter in project wildfly-core by wildfly.

the class EmbedHostControllerHandler method create.

static EmbedHostControllerHandler create(final AtomicReference<EmbeddedProcessLaunch> hostControllerReference, final CommandContext ctx, final boolean modular) {
    EmbedHostControllerHandler result = new EmbedHostControllerHandler(hostControllerReference);
    final FilenameTabCompleter pathCompleter = FilenameTabCompleter.newCompleter(ctx);
    if (!modular) {
        result.jbossHome = new FileSystemPathArgument(result, pathCompleter, "--jboss-home");
    }
    result.stdOutHandling = new ArgumentWithValue(result, new SimpleTabCompleter(new String[] { ECHO, DISCARD_STDOUT }), "--std-out");
    result.domainConfig = new ArgumentWithValue(result, DOMAIN_CONFIG);
    result.hostConfig = new ArgumentWithValue(result, HOST_CONFIG);
    result.dashC = new ArgumentWithValue(result, "-c");
    result.dashC.addCantAppearAfter(result.domainConfig);
    result.domainConfig.addCantAppearAfter(result.dashC);
    result.timeout = new ArgumentWithValue(result, "--timeout");
    result.emptyDomainConfig = new ArgumentWithoutValue(result, EMPTY_DOMAIN_CONFIG);
    result.removeExistingDomainConfig = new ArgumentWithoutValue(result, REMOVE_EXISTING_DOMAIN_CONFIG);
    result.emptyHostConfig = new ArgumentWithoutValue(result, EMPTY_HOST_CONFIG);
    result.removeExistingHostConfig = new ArgumentWithoutValue(result, REMOVE_EXISTING_HOST_CONFIG);
    return result;
}
Also used : SimpleTabCompleter(org.jboss.as.cli.handlers.SimpleTabCompleter) FileSystemPathArgument(org.jboss.as.cli.impl.FileSystemPathArgument) ArgumentWithValue(org.jboss.as.cli.impl.ArgumentWithValue) FilenameTabCompleter(org.jboss.as.cli.handlers.FilenameTabCompleter) ArgumentWithoutValue(org.jboss.as.cli.impl.ArgumentWithoutValue)

Example 2 with FilenameTabCompleter

use of org.jboss.as.cli.handlers.FilenameTabCompleter in project wildfly-core by wildfly.

the class ASModuleHandler method addModule.

protected void addModule(CommandContext ctx, final ParsedCommandLine parsedCmd) throws CommandLineException {
    final String moduleName = name.getValue(parsedCmd, true);
    // resources required only if we are generating module.xml
    if (!moduleArg.isPresent(parsedCmd) && !(resources.isPresent(parsedCmd) || absoluteResources.isPresent(parsedCmd))) {
        throw new CommandFormatException("You must specify at least one resource: use --resources or --absolute-resources parameter");
    }
    final String resourcePaths = resources.getValue(parsedCmd);
    final String absoluteResourcePaths = absoluteResources.getValue(parsedCmd);
    String pathDelimiter = PATH_SEPARATOR;
    if (resourceDelimiter.isPresent(parsedCmd)) {
        pathDelimiter = resourceDelimiter.getValue(parsedCmd);
    }
    final FilenameTabCompleter pathCompleter = Util.isWindows() ? new WindowsFilenameTabCompleter(ctx) : new DefaultFilenameTabCompleter(ctx);
    final String[] resourceArr = (resourcePaths == null) ? new String[0] : resourcePaths.split(pathDelimiter);
    File[] resourceFiles = new File[resourceArr.length];
    boolean allowNonExistent = allowNonExistentResources.isPresent(parsedCmd);
    for (int i = 0; i < resourceArr.length; ++i) {
        final File f = new File(pathCompleter.translatePath(resourceArr[i]));
        if (!f.exists() && !allowNonExistent) {
            throw new CommandLineException("Failed to locate " + f.getAbsolutePath() + ", if you defined a nonexistent resource on purpose you should " + "use the " + allowNonExistentResources.getFullName() + " option");
        }
        resourceFiles[i] = f;
    }
    final String[] absoluteResourceArr = (absoluteResourcePaths == null) ? new String[0] : absoluteResourcePaths.split(pathDelimiter);
    File[] absoluteResourceFiles = new File[absoluteResourceArr.length];
    for (int i = 0; i < absoluteResourceArr.length; ++i) {
        final File f = new File(pathCompleter.translatePath(absoluteResourceArr[i]));
        if (!f.exists()) {
            throw new CommandLineException("Failed to locate " + f.getAbsolutePath());
        }
        absoluteResourceFiles[i] = f;
    }
    final File moduleDir = getModulePath(getModulesDir(ctx), moduleName, slot.getValue(parsedCmd));
    if (moduleDir.exists()) {
        throw new CommandLineException("Module " + moduleName + " already exists at " + moduleDir.getAbsolutePath());
    }
    if (!moduleDir.mkdirs()) {
        throw new CommandLineException("Failed to create directory " + moduleDir.getAbsolutePath());
    }
    final ModuleConfigImpl config;
    final String moduleXml = moduleArg.getValue(parsedCmd);
    if (moduleXml != null) {
        config = null;
        final File source = new File(moduleXml);
        if (!source.exists()) {
            throw new CommandLineException("Failed to locate the file on the filesystem: " + source.getAbsolutePath());
        }
        copy(source, new File(moduleDir, "module.xml"));
    } else {
        config = new ModuleConfigImpl(moduleName);
    }
    for (File f : resourceFiles) {
        copyResource(f, new File(moduleDir, f.getName()), ctx, this);
        if (config != null) {
            config.addResource(new ResourceRoot(f.getName()));
        }
    }
    for (File f : absoluteResourceFiles) {
        if (config != null) {
            try {
                config.addResource(new ResourceRoot(f.getCanonicalPath()));
            } catch (IOException ioe) {
                throw new CommandLineException("Failed to read path: " + f.getAbsolutePath(), ioe);
            }
        }
    }
    if (config != null) {
        Set<String> modules = new HashSet<>();
        final String dependenciesStr = dependencies.getValue(parsedCmd);
        if (dependenciesStr != null) {
            final String[] depsArr = dependenciesStr.split(",+");
            for (String dep : depsArr) {
                // TODO validate dependencies
                String depName = dep.trim();
                config.addDependency(new ModuleDependency(depName));
                modules.add(depName);
            }
        }
        final String exportDependenciesStr = exportDependencies.getValue(parsedCmd);
        if (exportDependenciesStr != null) {
            final String[] depsArr = exportDependenciesStr.split(",+");
            for (String dep : depsArr) {
                // TODO validate dependencies
                String depName = dep.trim();
                if (modules.contains(depName)) {
                    deleteRecursively(moduleDir);
                    throw new CommandLineException("Error, duplicated dependency " + depName);
                }
                modules.add(depName);
                config.addDependency(new ModuleDependency(depName, true));
            }
        }
        final String propsStr = props.getValue(parsedCmd);
        if (propsStr != null) {
            final String[] pairs = propsStr.split(",");
            for (String pair : pairs) {
                int equals = pair.indexOf('=');
                if (equals == -1) {
                    throw new CommandFormatException("Property '" + pair + "' in '" + propsStr + "' is missing the equals sign.");
                }
                final String propName = pair.substring(0, equals);
                if (propName.isEmpty()) {
                    throw new CommandFormatException("Property name is missing for '" + pair + "' in '" + propsStr + "'");
                }
                config.setProperty(propName, pair.substring(equals + 1));
            }
        }
        final String slotVal = slot.getValue(parsedCmd);
        if (slotVal != null) {
            config.setSlot(slotVal);
        }
        final String mainCls = mainClass.getValue(parsedCmd);
        if (mainCls != null) {
            config.setMainClass(mainCls);
        }
        FileOutputStream fos = null;
        final File moduleFile = new File(moduleDir, "module.xml");
        try {
            fos = new FileOutputStream(moduleFile);
            XMLExtendedStreamWriter xmlWriter = create(XMLOutputFactory.newInstance().createXMLStreamWriter(fos, StandardCharsets.UTF_8.name()));
            config.writeContent(xmlWriter, null);
            xmlWriter.flush();
        } catch (IOException e) {
            throw new CommandLineException("Failed to create file " + moduleFile.getAbsolutePath(), e);
        } catch (XMLStreamException e) {
            throw new CommandLineException("Failed to write to " + moduleFile.getAbsolutePath(), e);
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                }
            }
        }
    }
}
Also used : DefaultFilenameTabCompleter(org.jboss.as.cli.handlers.DefaultFilenameTabCompleter) IOException(java.io.IOException) WindowsFilenameTabCompleter(org.jboss.as.cli.handlers.WindowsFilenameTabCompleter) DefaultFilenameTabCompleter(org.jboss.as.cli.handlers.DefaultFilenameTabCompleter) FilenameTabCompleter(org.jboss.as.cli.handlers.FilenameTabCompleter) CommandLineException(org.jboss.as.cli.CommandLineException) XMLExtendedStreamWriter(org.jboss.staxmapper.XMLExtendedStreamWriter) XMLStreamException(javax.xml.stream.XMLStreamException) CommandFormatException(org.jboss.as.cli.CommandFormatException) FileOutputStream(java.io.FileOutputStream) WindowsFilenameTabCompleter(org.jboss.as.cli.handlers.WindowsFilenameTabCompleter) File(java.io.File) HashSet(java.util.HashSet)

Example 3 with FilenameTabCompleter

use of org.jboss.as.cli.handlers.FilenameTabCompleter in project wildfly-core by wildfly.

the class Util method applyReplacements.

static void applyReplacements(CommandContext ctx, String name, ModelNode value, ModelNode description, ModelType mt, Attachments attachments) {
    if (value == null || !value.isDefined()) {
        return;
    }
    switch(mt) {
        case INT:
            // Server side can accept invalid content.
            if (!value.getType().equals(ModelType.STRING)) {
                break;
            }
            if (isFileAttachment(description)) {
                FilenameTabCompleter completer = FilenameTabCompleter.newCompleter(ctx);
                value.set(attachments.addFileAttachment(completer.translatePath(value.asString())));
            }
            break;
        case LIST:
            {
                // Server side can accept invalid content.
                if (!mt.equals(value.getType())) {
                    break;
                }
                ModelNode valueType = description.get("value-type");
                if (valueType.isDefined()) {
                    ModelType valueTypeType = valueType.getType();
                    // of Objects
                    if (ModelType.OBJECT.equals(valueTypeType)) {
                        for (int i = 0; i < value.asInt(); i++) {
                            applyReplacements(ctx, "value-type", value.get(i), valueType, ModelType.OBJECT, attachments);
                        }
                    // of INT
                    } else if (ModelType.INT.equals(valueType.asType())) {
                        if (isFileAttachment(description)) {
                            FilenameTabCompleter completer = FilenameTabCompleter.newCompleter(ctx);
                            for (int i = 0; i < value.asInt(); i++) {
                                value.get(i).set(attachments.addFileAttachment(completer.translatePath(value.get(i).asString())));
                            }
                        }
                    }
                }
                break;
            }
        case OBJECT:
            {
                // Server side can accept invalid content.
                if (!mt.equals(value.getType())) {
                    break;
                }
                ModelNode valueType = description.get("value-type");
                // This is a value-type value, use the description
                if (!valueType.isDefined()) {
                    valueType = description;
                }
                // If valueTypeType is an OBJECT, then we can consult the value-type.
                // If valueTypeType is not an OBJECT, then we are facing a Map<String, X>
                // where X is indicated by the valueTypeType enum value
                ModelType valueTypeType = valueType.getType();
                if (ModelType.OBJECT.equals(valueTypeType)) {
                    for (String k : value.keys()) {
                        if (value.get(k).isDefined() && valueType.hasDefined(k)) {
                            ModelNode p = valueType.get(k);
                            if (p.hasDefined("type")) {
                                applyReplacements(ctx, k, value.get(k), p, p.get("type").asType(), attachments);
                            }
                        }
                    }
                }
                break;
            }
        default:
    }
}
Also used : ModelType(org.jboss.dmr.ModelType) TerminalString(org.aesh.readline.terminal.formatting.TerminalString) ModelNode(org.jboss.dmr.ModelNode) FilenameTabCompleter(org.jboss.as.cli.handlers.FilenameTabCompleter)

Example 4 with FilenameTabCompleter

use of org.jboss.as.cli.handlers.FilenameTabCompleter in project wildfly-core by wildfly.

the class EmbedServerHandler method create.

static EmbedServerHandler create(final AtomicReference<EmbeddedProcessLaunch> serverReference, CommandContext ctx, boolean modular) {
    EmbedServerHandler result = new EmbedServerHandler(serverReference);
    final FilenameTabCompleter pathCompleter = FilenameTabCompleter.newCompleter(ctx);
    if (!modular) {
        result.jbossHome = new FileSystemPathArgument(result, pathCompleter, "--jboss-home");
    }
    result.stdOutHandling = new ArgumentWithValue(result, new SimpleTabCompleter(new String[] { ECHO, DISCARD_STDOUT }), "--std-out");
    result.serverConfig = new ArgumentWithValue(result, "--server-config");
    result.dashC = new ArgumentWithValue(result, "-c");
    result.dashC.addCantAppearAfter(result.serverConfig);
    result.serverConfig.addCantAppearAfter(result.dashC);
    result.adminOnly = new ArgumentWithValue(result, SimpleTabCompleter.BOOLEAN, "--admin-only");
    result.emptyConfig = new ArgumentWithoutValue(result, "--empty-config");
    result.removeExisting = new ArgumentWithoutValue(result, "--remove-existing");
    result.removeExisting.addRequiredPreceding(result.emptyConfig);
    result.timeout = new ArgumentWithValue(result, "--timeout");
    return result;
}
Also used : SimpleTabCompleter(org.jboss.as.cli.handlers.SimpleTabCompleter) FileSystemPathArgument(org.jboss.as.cli.impl.FileSystemPathArgument) ArgumentWithValue(org.jboss.as.cli.impl.ArgumentWithValue) FilenameTabCompleter(org.jboss.as.cli.handlers.FilenameTabCompleter) ArgumentWithoutValue(org.jboss.as.cli.impl.ArgumentWithoutValue)

Aggregations

FilenameTabCompleter (org.jboss.as.cli.handlers.FilenameTabCompleter)4 SimpleTabCompleter (org.jboss.as.cli.handlers.SimpleTabCompleter)2 ArgumentWithValue (org.jboss.as.cli.impl.ArgumentWithValue)2 ArgumentWithoutValue (org.jboss.as.cli.impl.ArgumentWithoutValue)2 FileSystemPathArgument (org.jboss.as.cli.impl.FileSystemPathArgument)2 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 HashSet (java.util.HashSet)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 TerminalString (org.aesh.readline.terminal.formatting.TerminalString)1 CommandFormatException (org.jboss.as.cli.CommandFormatException)1 CommandLineException (org.jboss.as.cli.CommandLineException)1 DefaultFilenameTabCompleter (org.jboss.as.cli.handlers.DefaultFilenameTabCompleter)1 WindowsFilenameTabCompleter (org.jboss.as.cli.handlers.WindowsFilenameTabCompleter)1 ModelNode (org.jboss.dmr.ModelNode)1 ModelType (org.jboss.dmr.ModelType)1 XMLExtendedStreamWriter (org.jboss.staxmapper.XMLExtendedStreamWriter)1