Search in sources :

Example 6 with CommandSpec

use of picocli.CommandLine.Model.CommandSpec in project aion by aionnetwork.

the class Cli method call.

private ReturnType call(final String[] args, CfgAion cfg, boolean initializeAvm) {
    final CommandLine.ParseResult parseResult;
    try {
        // the pre-process method handles arguments that are separated by space
        // parsing populates the options object
        parseResult = parser.parseArgs(Arguments.preProcess(args));
    } catch (Exception e) {
        System.out.println("Unable to parse the input arguments due to: ");
        if (e.getMessage() != null) {
            System.out.println(e.getMessage());
        } else {
            e.printStackTrace();
        }
        System.out.println();
        printHelp();
        return ERROR;
    }
    // make sure that there is no conflicting arguments; otherwise send warning
    checkArguments(options, parseResult);
    try {
        if (options.isHelp()) {
            printHelp();
            return EXIT;
        }
        if (options.isVersion() || options.isVersionTag()) {
            if (options.isVersion()) {
                System.out.println("\nVersion");
                System.out.println("--------------------------------------------");
            }
            System.out.println(Version.KERNEL_VERSION);
            return EXIT;
        }
        if (options.getNetwork() != null || (options.getConfig() != null && !options.getConfig().isEmpty())) {
            String strNet = options.getNetwork();
            // the network given in config overwrites the -n option
            if (options.getConfig() != null && !options.getConfig().isEmpty()) {
                strNet = options.getConfig();
            }
            setNetwork(strNet, cfg);
        // no return -> allow for other parameters combined with -n
        }
        if (options.getDirectory() != null) {
            if (!setDirectory(options.getDirectory(), cfg)) {
                return ERROR;
            }
        // no return -> allow for other parameters combined with -d
        }
        // reading from correct config file
        File configFile = cfg.getExecConfigFile();
        if (!configFile.exists()) {
            configFile = cfg.getInitialConfigFile();
        } else {
            cfg.setReadConfigFile(configFile);
        }
        // reading from correct fork file
        File forkFile = cfg.getForkFile();
        if (forkFile != null && forkFile.exists()) {
            cfg.setForkProperties(cfg.getNetwork(), forkFile);
        }
        // true means the UUID must be set
        boolean overwrite = cfg.fromXML(configFile);
        // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ initialize the avm ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        if (initializeAvm) {
            try {
                // Grab the project root directory.
                String projectRootDirectory = System.getProperty("user.dir") + File.separator;
                // Create the multi-version schedule. Note that avm version 1 is always enabled, from block zero
                // because it handles balance transfers. The kernel is responsible for ensuring it is not called
                // with anything else.
                Properties forkProperties = CfgAion.inst().getFork().getProperties();
                String fork2 = forkProperties.getProperty("fork1.0");
                AvmVersionSchedule schedule;
                if (fork2 != null) {
                    schedule = AvmVersionSchedule.newScheduleForBothVersions(0, Long.valueOf(fork2), 100);
                } else {
                    schedule = AvmVersionSchedule.newScheduleForOnlySingleVersionSupport(0, 100);
                }
                AvmConfigurations.initializeConfigurationsAsReadOnly(schedule, projectRootDirectory);
            } catch (Exception e) {
                System.out.println("A fatal error occurred attempting to configure the AVM: " + e.getMessage());
                System.exit(SystemExitCodes.INITIALIZATION_ERROR);
            }
        }
        // determine the port configuration, can be combined with the -n, -d, -c, -i arguments
        if (parseResult.subcommand() != null && parseResult.subcommand().commandSpec().userObject().getClass() == EditCli.class && editCli.runCommand(cfg)) {
            overwrite = true;
        }
        if (editCli.help) {
            return ReturnType.EXIT;
        }
        if (options.getPort() != null) {
            int currentPort = cfg.getNet().getP2p().getPort();
            int portNumber = currentPort;
            boolean validPort = true;
            try {
                portNumber = Integer.parseInt(options.getPort());
            } catch (NumberFormatException e) {
                validPort = false;
                System.out.println("Port must be a positive integer value");
            }
            if (portNumber < 0 || portNumber > 0xFFFF) {
                validPort = false;
                System.out.println("Port out of range: " + portNumber);
            }
            if (validPort && portNumber != currentPort) {
                // update port in config
                cfg.getNet().getP2p().setPort(portNumber);
                overwrite = true;
                System.out.println("Port set to: " + portNumber);
            } else {
                System.out.println("Using the current port configuration: " + currentPort);
            }
        // no return, allow for other parameters combined with -p
        }
        if (options.getConfig() != null) {
            // if the directory was set we generate a new file
            if (options.getDirectory() != null) {
                configFile = cfg.getExecConfigFile();
                // ensure path exists
                File dir = cfg.getExecConfigDirectory();
                if (!dir.exists()) {
                    if (!dir.mkdirs()) {
                        System.out.println("ERROR: Unable to create directory: " + getRelativePath(dir.getAbsolutePath()));
                        return ERROR;
                    }
                }
                try {
                    configFile.createNewFile();
                } catch (IOException e) {
                    System.out.println("ERROR: Unable to create file: " + getRelativePath(configFile.getAbsolutePath()));
                    return ERROR;
                }
            }
            // save to disk
            cfg.toXML(null, configFile);
            System.out.println("\nNew config generated at: " + getRelativePath(configFile.getAbsolutePath()));
            return ReturnType.EXIT;
        }
        if (options.isInfo()) {
            System.out.println("Reading config file from: " + getRelativePath(configFile.getAbsolutePath()));
            if (overwrite) {
                // updating the file in case the user id was not set; overwrite port
                cfg.toXML(null, configFile);
            }
            printInfo(cfg);
            return ReturnType.EXIT;
        }
        // make directories for kernel execution
        makeDirs(configFile, forkFile, cfg);
        if (overwrite) {
            // updating the file in case the user id was not set; overwrite port
            cfg.toXML(null, cfg.getExecConfigFile());
        }
        // set correct keystore directory
        Keystore.setKeystorePath(cfg.getKeystoreDir().getAbsolutePath());
        CommandSpec commandSpec = findCommandSpec(parseResult, AccountCli.class);
        if (commandSpec != null) {
            return ((AccountCli) commandSpec.userObject()).runCommand(passwordReader);
        }
        if (options.getSsl() != null) {
            String[] parameters = options.getSsl();
            if (parameters.length == 0 || parameters.length == 2) {
                createKeystoreDirIfMissing();
                Console console = System.console();
                checkConsoleExists(console);
                List<String> scriptArgs = new ArrayList<>();
                scriptArgs.add("/bin/bash");
                scriptArgs.add("script/generateSslCert.sh");
                scriptArgs.add(getCertName(console));
                scriptArgs.add(getCertPass(console));
                // add the hostname and ip optionally passed in as cli args
                scriptArgs.addAll(Arrays.asList(parameters));
                new ProcessBuilder(scriptArgs).inheritIO().start().waitFor();
                return EXIT;
            } else {
                System.out.println("Incorrect usage of -s create command.\n" + "Command must enter both hostname AND ip or else neither one.");
                return ERROR;
            }
        }
        if (options.isRebuildBlockInfo()) {
            System.out.println("Starting database clean-up.");
            CfgAion.inst().dbFromXML();
            Map<LogEnum, LogLevel> cfgLog = new HashMap<>();
            cfgLog.put(LogEnum.GEN, LogLevel.INFO);
            AionLoggerFactory.initAll(cfgLog);
            // get the current blockchain
            AionRepositoryImpl repository = AionRepositoryImpl.inst();
            repository.pruneAndCorrectBlockStore(AionLoggerFactory.getLogger(LogEnum.GEN.name()));
            repository.close();
            System.out.println("Finished database clean-up.");
            return EXIT;
        }
        if (options.getRevertToBlock() != null) {
            String block = options.getRevertToBlock();
            if (revertTo(block)) {
                System.out.println("Blockchain successfully reverted to block number " + block + ".");
                return EXIT;
            } else {
                System.out.println("Unable to revert to block number " + block + ".");
                return ERROR;
            }
        }
        if (options.getPruneStateOption() != null) {
            String pruning_type = options.getPruneStateOption();
            try {
                // ensure mining is disabled
                CfgAion localCfg = CfgAion.inst();
                localCfg.fromXML();
                localCfg.getConsensus().setMining(false);
                // setting pruning to the version requested
                CfgDb.PruneOption option = CfgDb.PruneOption.fromValue(pruning_type);
                localCfg.getDb().setPrune(option.toString());
                AionLoggerFactory.initAll(Map.of(LogEnum.GEN, LogLevel.INFO));
                final Logger log = AionLoggerFactory.getLogger(LogEnum.GEN.name());
                log.info("Reorganizing the state storage to " + option + " mode ...");
                AionBlockchainImpl chain = new AionBlockchainImpl(localCfg, null, false);
                chain.pruneOrRecoverState(pruning_type.equals("spread"), localCfg.getGenesis(), log);
                chain.close();
                return EXIT;
            } catch (Exception e) {
                System.out.println("Reorganizing the state storage FAILED due to:");
                e.printStackTrace();
                return ERROR;
            }
        }
        CommandLine.Model.CommandSpec spec = findCommandSpec(parseResult, DevCLI.class);
        if (spec != null) {
            ReturnType returnType = ((DevCLI) spec.userObject()).runCommand();
            if (returnType != RUN) {
                return returnType;
            }
        }
        if (options.isDbCompact()) {
            // read database configuration
            CfgAion.inst().dbFromXML();
            AionLoggerFactory.initAll(Map.of(LogEnum.DB, LogLevel.INFO));
            // get the current blockchain
            AionRepositoryImpl repository = AionRepositoryImpl.inst();
            // compact database after the changes were applied
            repository.compact();
            repository.close();
            return EXIT;
        }
        if (options.isRedoImport() != null) {
            long height = 0L;
            String parameter = options.isRedoImport();
            // ensure mining is disabled
            CfgAion localCfg = CfgAion.inst();
            localCfg.dbFromXML();
            localCfg.getConsensus().setMining(false);
            AionLoggerFactory.initAll(Map.of(LogEnum.GEN, LogLevel.INFO));
            final Logger log = AionLoggerFactory.getLogger(LogEnum.GEN.name());
            if (height < 0) {
                log.error("Negative values are not valid as starting height. Nothing to do.");
                return ERROR;
            }
            log.info("Importing stored blocks INITIATED...");
            AionBlockchainImpl chain = new AionBlockchainImpl(localCfg, null, false);
            if (parameter.isEmpty()) {
                chain.redoMainChainImport(height, localCfg.getGenesis(), log);
                chain.close();
                return EXIT;
            } else {
                try {
                    height = Long.parseLong(parameter);
                } catch (NumberFormatException e) {
                    log.error("The given argument «" + parameter + "» cannot be converted to a number.");
                    chain.close();
                    return ERROR;
                }
                chain.redoMainChainImport(height, localCfg.getGenesis(), log);
                chain.close();
                return EXIT;
            }
        }
        // if no return happened earlier, run the kernel
        return RUN;
    } catch (Exception e) {
        // TODO: should be moved to individual procedures
        System.out.println();
        e.printStackTrace();
        return ERROR;
    }
}
Also used : CfgDb(org.aion.zero.impl.config.CfgDb) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) AionRepositoryImpl(org.aion.zero.impl.db.AionRepositoryImpl) Properties(java.util.Properties) Logger(org.slf4j.Logger) LogLevel(org.aion.log.LogLevel) LogEnum(org.aion.log.LogEnum) CommandSpec(picocli.CommandLine.Model.CommandSpec) Console(java.io.Console) CommandSpec(picocli.CommandLine.Model.CommandSpec) AvmVersionSchedule(org.aion.zero.impl.vm.avm.schedule.AvmVersionSchedule) IOException(java.io.IOException) IOException(java.io.IOException) CfgAion(org.aion.zero.impl.config.CfgAion) CommandLine(picocli.CommandLine) AionBlockchainImpl(org.aion.zero.impl.blockchain.AionBlockchainImpl) File(java.io.File)

Example 7 with CommandSpec

use of picocli.CommandLine.Model.CommandSpec in project Valkyrien-Warfare-Revamped by ValkyrienWarfare.

the class VSCommandBase method getTabCompletions.

@SneakyThrows
@Override
public List<String> getTabCompletions(MinecraftServer server, ICommandSender sender, final String[] splitArgs, @Nullable BlockPos targetPos) {
    VSCommandFactory factory = new VSCommandFactory(sender);
    CommandSpec spec = CommandSpec.forAnnotatedObject(factory.create(cmdClass), factory);
    String[] args = VSCommandUtil.toTabCompleteArgs(splitArgs);
    List<CharSequence> candidates = new ArrayList<>();
    AutoComplete.complete(spec, args, args.length - 1, args[args.length - 1].length(), 500, candidates);
    return candidates.stream().distinct().map(CharSequence::toString).map(s -> args[args.length - 1] + s).collect(Collectors.toList());
}
Also used : PrintWriter(java.io.PrintWriter) java.util(java.util) MethodsReturnNonnullByDefault(mcp.MethodsReturnNonnullByDefault) SneakyThrows(lombok.SneakyThrows) World(net.minecraft.world.World) WorldConverter(org.valkyrienskies.mod.common.command.converters.WorldConverter) CommandBase(net.minecraft.command.CommandBase) BlockPos(net.minecraft.util.math.BlockPos) AutoComplete(picocli.AutoComplete) Collectors(java.util.stream.Collectors) ParametersAreNonnullByDefault(javax.annotation.ParametersAreNonnullByDefault) TextComponentString(net.minecraft.util.text.TextComponentString) ITypeConverter(picocli.CommandLine.ITypeConverter) MinecraftServer(net.minecraft.server.MinecraftServer) Vec3d(net.minecraft.util.math.Vec3d) ICommandSender(net.minecraft.command.ICommandSender) NullOutputStream(org.apache.commons.io.output.NullOutputStream) CommandSpec(picocli.CommandLine.Model.CommandSpec) Command(picocli.CommandLine.Command) Vec3dDataConverter(org.valkyrienskies.mod.common.command.converters.Vec3dDataConverter) ShipData(org.valkyrienskies.mod.common.ships.ShipData) ShipDataConverter(org.valkyrienskies.mod.common.command.converters.ShipDataConverter) CommandLine(picocli.CommandLine) Nullable(javax.annotation.Nullable) CommandSpec(picocli.CommandLine.Model.CommandSpec) TextComponentString(net.minecraft.util.text.TextComponentString) SneakyThrows(lombok.SneakyThrows)

Example 8 with CommandSpec

use of picocli.CommandLine.Model.CommandSpec in project keycloak by keycloak.

the class ShortErrorMessageHandler method handleParseException.

public int handleParseException(ParameterException ex, String[] args) {
    CommandLine cmd = ex.getCommandLine();
    PrintWriter writer = cmd.getErr();
    writer.println(cmd.getColorScheme().errorText(ex.getMessage()));
    UnmatchedArgumentException.printSuggestions(ex, writer);
    CommandSpec spec = cmd.getCommandSpec();
    writer.printf("Try '%s --help' for more information on the available options.%n", spec.qualifiedName());
    return cmd.getExitCodeExceptionMapper() != null ? cmd.getExitCodeExceptionMapper().getExitCode(ex) : spec.exitCodeOnInvalidInput();
}
Also used : CommandLine(picocli.CommandLine) CommandSpec(picocli.CommandLine.Model.CommandSpec) PrintWriter(java.io.PrintWriter)

Aggregations

CommandSpec (picocli.CommandLine.Model.CommandSpec)8 CommandLine (picocli.CommandLine)6 PrintWriter (java.io.PrintWriter)4 ArrayList (java.util.ArrayList)3 File (java.io.File)2 IOException (java.io.IOException)2 Collectors (java.util.stream.Collectors)2 Main (org.keycloak.quarkus.runtime.cli.command.Main)2 PropertyMapper (org.keycloak.quarkus.runtime.configuration.mappers.PropertyMapper)2 Quarkus (io.quarkus.runtime.Quarkus)1 ConfigValue (io.smallrye.config.ConfigValue)1 Console (java.io.Console)1 java.util (java.util)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Properties (java.util.Properties)1 Set (java.util.Set)1