Search in sources :

Example 76 with DefaultParser

use of org.apache.commons.cli.DefaultParser in project CzechIdMng by bcvsolutions.

the class ConsoleRunner method run.

@Override
public void run(String... args) throws ParseException, IOException {
    // 
    // available commands
    Option optionVersion = Option.builder("v").longOpt("version").desc("print tool version.").build();
    Option optionHelp = Option.builder("h").longOpt("help").desc("print this message.").build();
    Option optionBuild = Option.builder().longOpt("build").desc("Build project or build product only (under current develop version in develop branch).\n" + "Maven 'install' command is used for product build, artifact will be installed into the local maven repository (=> usable as dependency for other module).\n" + "Use '-p' argument to project build.").build();
    Option optionRelease = Option.builder().longOpt("release").desc("Release product or module under '--release-version' argument. New development version will be set as '--develop-version' argument.").build();
    Option optionPublish = Option.builder().longOpt("publish").desc("Push prepared development, production and tags into origin repository.").build();
    Option optionReleaseAndPublish = Option.builder().longOpt("release-publish").desc("Release and publish shortcut as one command, taking the same arguments as '--release' command.").build();
    Option optionGetVersion = Option.builder().longOpt("get-version").desc("Get current product version (on the development branch or set '--development-branch' argument).").build();
    Option optionSetVersion = Option.builder().longOpt("set-version").desc("Set current product version (on the development branch - development branch can be changed only).").build();
    Option optionRevertVersion = Option.builder().longOpt("revert-version").desc("Changed versions by release command can be reverted if needed (before commit, usable after change product version only).").build();
    // 
    // product root
    Option optionRepositoryLocation = Option.builder("r").longOpt("repository-location").desc("Repository root folder - should contain all modules (<repository-location>/Realization/...).\n" + "Folder 'CzechIdMng' in the same folder as idm-tool.jar will be used as default for product.\n" + "Folder '<module>' in the same folder as idm-tool.jar will be used as default for module.\n").argName("path").hasArg().build();
    // 
    // optional arguments
    Option optionMavenHome = Option.builder().required(false).longOpt("maven-home").desc("Maven home directory.\n" + "MAVEN_HOME system property will be used as default.\n" + "Maven directory should contain command <maven-home>/bin/mvn.").argName("path").hasArg().build();
    Option optionNodeHome = Option.builder().required(false).longOpt("node-home").desc("Node home directory.\n" + "Global node instalation directory should contain executable node command.\n" + "Node and npm will be dowloaded and installed localy automaticaly into tool target folder (<target>/npm) by default otherwise.\n" + "For Windows <node-home>/node/node.exe.\n" + "For Linux <node-home>/node").argName("path").hasArg().build();
    Option optionDevelopBranch = Option.builder().required(false).longOpt("develop-branch").desc("Branch with feature - working branch.\n" + "'develop' will be used as default.").argName("branchName").hasArg().build();
    Option optionMasterBranch = Option.builder().required(false).longOpt("master-branch").desc("Branch for releases - where feature has to be merged after release.\n" + "'master' will be used as default.\n" + "'none' can be given - merge don't be executed (e.g. when some old hotfix branch is released).\n").argName("masterBranch").hasArg().build();
    Option optionReleaseVersion = Option.builder().required(false).longOpt("release-version").desc("Usable with '--release' command. Release will be create under this version.\n" + "Stable semantic version will be used as default (=> current no snapshot version).").argName("version").hasArg().build();
    Option optionDevelopVersion = Option.builder().required(false).longOpt("develop-version").desc("Usable with '--release' command. After release this version will be used in development branch.\n" + "Next minor snapshot semantic version will be used as default (=> current minor snapshot version + 1).\n" + "See major, minor, patch, hotfix argument if different version is needed.").argName("snapshot version").hasArg().build();
    Option optionUsername = Option.builder().required(false).longOpt("username").desc("Git username, if https repitory is used. When ssh repository is used, then passphrase for ssh key is needed only.").argName("git username").hasArg().build();
    Option optionPassword = Option.builder().required(false).longOpt("password").desc("If ssh repository is used / cloned, then passphrase for ssh key is needed only.\n" + "If https repository is used / cloned, then git username and password is needed.\n" + "If two-factor authentication is enabled for <username>, " + "then token has to be given (see git documentation, how to generate authentication token for developers).\n" + "If ssh key is used, then put passphrase for ssh key (It loads the known hosts and private keys from their " + "default locations (identity, id_rsa and id_dsa) in the user .ssh directory.).").argName("git password / token / ssh passphrase").hasArg().build();
    // 
    // module switch
    Option optionModule = Option.builder("m").required(false).longOpt("module").desc("Switch to module release / build.").argName("moduleId").hasArg().build();
    // 
    // project switch
    Option optionProject = Option.builder("p").required(false).longOpt("project").desc("Switch to project build.").build();
    // 
    // force commit / changes is not checked
    Option optionForce = Option.builder().required(false).longOpt("force").desc(String.format("Count of files changed by release command will not be checked. Limit of changed files is [%s].", ReleaseManager.MAX_RELEASE_CHANGES)).build();
    // major / minor / patch / hotfix
    Option optionMajor = Option.builder().required(false).longOpt("major").desc("Next develop version will be major, e.g. release 1.2.3 => develop 2.0.0-SNAPSHOT.").build();
    Option optionMinor = Option.builder().required(false).longOpt("minor").desc("Next develop version will be minor, e.g. release 1.2.3 => develop 1.3.0-SNAPSHOT. Will be applied as default.").build();
    Option optionPatch = Option.builder().required(false).longOpt("patch").desc("Next develop version will be patch, e.g. release 1.2.3 => develop 1.2.4-SNAPSHOT.").build();
    Option optionHotfix = Option.builder().required(false).longOpt("hotfix").desc("Next develop version will be hotfix, e.g. release 1.2.3 => develop 1.2.3.1-SNAPSHOT.").build();
    Option optionClean = Option.builder("c").required(false).longOpt("clean").desc("Clean up dowloaded frontend libraries in node_modules.").build();
    Option optionResolveDependencies = Option.builder().required(false).longOpt("resolve-dependencies").desc("Third party module dependencies will be resolved automatically (not resolved by default), when project is built.\n" + "Dependencies will not be resolved and included in build, if feature is not enabled => \n" + "all module dependencies has to be installed manually (prepared ~ copied in 'modules' folder).\n" + "Parameter is available for build a project only.").build();
    Option optionSkipFrontendBuild = Option.builder().required(false).longOpt("skip-frontend-build").desc("Frontend build will be skipped - product provided frontend will be used for build distribution artifacts.\n" + "Parameter is available for build a project only.").build();
    Options options = new Options();
    // 
    // available commands
    OptionGroup commandGroup = new OptionGroup();
    commandGroup.setRequired(true);
    commandGroup.addOption(optionVersion);
    commandGroup.addOption(optionHelp);
    commandGroup.addOption(optionBuild);
    commandGroup.addOption(optionRelease);
    commandGroup.addOption(optionPublish);
    commandGroup.addOption(optionReleaseAndPublish);
    commandGroup.addOption(optionSetVersion);
    commandGroup.addOption(optionGetVersion);
    commandGroup.addOption(optionRevertVersion);
    options.addOptionGroup(commandGroup);
    // 
    options.addOption(optionRepositoryLocation);
    options.addOption(optionMavenHome);
    options.addOption(optionNodeHome);
    options.addOption(optionDevelopBranch);
    options.addOption(optionMasterBranch);
    options.addOption(optionReleaseVersion);
    options.addOption(optionDevelopVersion);
    options.addOption(optionUsername);
    options.addOption(optionPassword);
    options.addOption(optionModule);
    options.addOption(optionProject);
    options.addOption(optionForce);
    options.addOption(optionMajor);
    options.addOption(optionMinor);
    options.addOption(optionPatch);
    options.addOption(optionHotfix);
    options.addOption(optionClean);
    options.addOption(optionResolveDependencies);
    options.addOption(optionSkipFrontendBuild);
    // 
    // parse arguments
    CommandLineParser parser = new DefaultParser();
    CommandLine commandLine = parser.parse(options, args, false);
    // log given arguments (for bug report, without password value)
    List<String> arguments = Arrays.stream(commandLine.getOptions()).map(option -> {
        if (!option.hasArg()) {
            return option.getLongOpt();
        }
        return String.format("%s=%s", option.getLongOpt(), // prevent to print password into logs
        option.getLongOpt().equals(optionPassword.getLongOpt()) ? GuardedString.SECRED_PROXY_STRING : option.getValue());
    }).collect(Collectors.toList());
    LOG.info("Running tool with arguments {}.", arguments);
    // 
    boolean projectBuild = commandLine.hasOption(optionProject.getLongOpt());
    boolean releaseModule = commandLine.hasOption(optionModule.getLongOpt());
    // 
    if (productReleaseManager == null) {
        // Product manager will be inited by default
        // manager's methods are used by console runner
        productReleaseManager = new ProductReleaseManager();
    }
    if (releaseModule && !projectBuild && moduleReleaseManager == null) {
        moduleReleaseManager = new ModuleReleaseManager(commandLine.getOptionValue(optionModule.getLongOpt()));
    }
    // 
    if (commandLine.hasOption(optionVersion.getLongOpt())) {
        System.out.println(String.format("v%s", getVersion()));
        return;
    }
    // 
    if (commandLine.hasOption(optionHelp.getLongOpt())) {
        printHelp(options);
        return;
    }
    // 
    String rootFolder = null;
    if (commandLine.hasOption(optionRepositoryLocation.getLongOpt())) {
        rootFolder = commandLine.getOptionValue(optionRepositoryLocation.getLongOpt());
    }
    // 
    String mavenHome = null;
    if (commandLine.hasOption(optionMavenHome.getLongOpt())) {
        mavenHome = commandLine.getOptionValue(optionMavenHome.getLongOpt());
    }
    String nodeHome = null;
    if (commandLine.hasOption(optionNodeHome.getLongOpt())) {
        nodeHome = commandLine.getOptionValue(optionNodeHome.getLongOpt());
    }
    // 
    if (projectBuild) {
        if (!commandLine.hasOption(optionBuild.getLongOpt())) {
            throw new BuildException("Build a project is supported only.");
        }
        boolean clean = commandLine.hasOption(optionClean.getLongOpt());
        boolean resolveDependencies = commandLine.hasOption(optionResolveDependencies.getLongOpt());
        boolean skipFrontendBuild = commandLine.hasOption(optionSkipFrontendBuild.getLongOpt());
        // 
        if (projectManager == null) {
            projectManager = new ProjectManager();
            projectManager.setMavenHome(mavenHome);
            projectManager.setNodeHome(nodeHome);
            projectManager.setResolveDependencies(resolveDependencies);
            projectManager.setSkipFrontendBuild(skipFrontendBuild);
            projectManager.init();
        }
        // /tool folder by default => project is in parent folder.
        projectManager.build(rootFolder == null ? "../" : rootFolder, clean);
        // 
        LOG.info("Complete!");
        return;
    }
    // 
    // Release
    ReleaseManager releaseManager = getReleaseManager(releaseModule);
    releaseManager.setMavenHome(mavenHome);
    // 
    if (commandLine.hasOption(optionRepositoryLocation.getLongOpt())) {
        releaseManager.setRepositoryRoot(commandLine.getOptionValue(optionRepositoryLocation.getLongOpt()));
    }
    if (commandLine.hasOption(optionDevelopBranch.getLongOpt())) {
        releaseManager.setDevelopBranch(commandLine.getOptionValue(optionDevelopBranch.getLongOpt()));
        LOG.debug("Using develop branch [{}].", releaseManager.getDevelopBranch());
    }
    if (commandLine.hasOption(optionMasterBranch.getLongOpt())) {
        String masterBranch = commandLine.getOptionValue(optionMasterBranch.getLongOpt());
        if (masterBranch.equals("none")) {
            masterBranch = null;
        }
        releaseManager.setMasterBranch(masterBranch);
        LOG.debug("Using production branch [{}].", releaseManager.getMasterBranch());
    }
    if (commandLine.hasOption(optionUsername.getLongOpt())) {
        String username = commandLine.getOptionValue(optionUsername.getLongOpt());
        releaseManager.setUsername(username);
        LOG.debug("Using git username [{}].", username);
    }
    // 
    GuardedString password = null;
    if (commandLine.hasOption(optionPassword.getLongOpt())) {
        password = new GuardedString(commandLine.getOptionValue(optionPassword.getLongOpt()));
    } else {
        // get password from file
        String externalPassword = getProperty(PROPERTY_PASSWORD);
        if (StringUtils.isNotEmpty(externalPassword)) {
            password = new GuardedString(externalPassword);
        } else if (commandLine.hasOption(optionRelease.getLongOpt()) || commandLine.hasOption(optionReleaseAndPublish.getLongOpt())) {
            // prompt when publish / release-publish command is used
            // creates a console object
            Console cnsl = System.console();
            if (cnsl != null) {
                System.out.println(optionPassword.getDescription());
                char[] pwd = cnsl.readPassword(String.format("%s: ", optionPassword.getArgName()));
                if (pwd != null && pwd.length > 0) {
                    password = new GuardedString(new String(pwd));
                }
            }
        }
    }
    if (password != null) {
        releaseManager.setPassword(password);
        LOG.info(String.format("Password (%s) is given.", optionPassword.getArgName()));
    }
    // 
    if (commandLine.hasOption(optionForce.getLongOpt())) {
        releaseManager.setForce(true);
        LOG.debug("Force argument was given, count of files changed by release command will not be checked.");
    }
    // before run - check props is set
    releaseManager.init();
    // 
    if (commandLine.hasOption(optionBuild.getLongOpt())) {
        String currentVersion = releaseManager.build();
        // 
        LOG.info("Product version [{}] successfully built and installed into local maven repository.", currentVersion);
    } else if (commandLine.hasOption(optionRelease.getLongOpt()) || commandLine.hasOption(optionReleaseAndPublish.getLongOpt())) {
        String releaseVersion = commandLine.getOptionValue(optionReleaseVersion.getLongOpt());
        String developVersion = commandLine.getOptionValue(optionDevelopVersion.getLongOpt());
        // current [snapshot] develop version
        String currentVersion = releaseManager.getCurrentVersion(null);
        // 
        if (StringUtils.isEmpty(developVersion)) {
            // prepare next development version by major / minor / patch / hotfix switch
            ReleaseManager.VersionType versionType = null;
            if (commandLine.hasOption(optionMajor.getLongOpt())) {
                versionType = ReleaseManager.VersionType.MAJOR;
            }
            if (commandLine.hasOption(optionMinor.getLongOpt())) {
                versionType = ReleaseManager.VersionType.MINOR;
            }
            if (commandLine.hasOption(optionPatch.getLongOpt())) {
                versionType = ReleaseManager.VersionType.PATCH;
            }
            if (commandLine.hasOption(optionHotfix.getLongOpt())) {
                versionType = ReleaseManager.VersionType.HOTFIX;
            }
            // 
            if (versionType == null) {
                // minor as default
                versionType = ReleaseManager.VersionType.MINOR;
            }
            developVersion = releaseManager.getNextSnapshotVersionNumber(StringUtils.isEmpty(releaseVersion) ? currentVersion : releaseVersion, versionType);
        }
        // 
        String releasedVersion = releaseManager.release(releaseVersion, developVersion);
        // 
        LOG.info("Product version released [{}]. New development version [{}].", releasedVersion, currentVersion);
        LOG.info("Branches [{}], [{}] and tag [{}] are prepared to push into origin (use --publish command).", releaseManager.getDevelopBranch(), releaseManager.getMasterBranch(), releasedVersion);
        // publish shortcut after release
        if (commandLine.hasOption(optionReleaseAndPublish.getLongOpt())) {
            releaseManager.publish();
            LOG.info("Branches [{}], [{}] and prepared tags pushed into origin.", releaseManager.getDevelopBranch(), releaseManager.getMasterBranch());
        }
    } else if (commandLine.hasOption(optionPublish.getLongOpt())) {
        // standalone publish
        releaseManager.publish();
        LOG.info("Branches [{}], [{}] and prepared tags pushed into origin.", releaseManager.getDevelopBranch(), releaseManager.getMasterBranch());
    } else if (commandLine.hasOption(optionRevertVersion.getLongOpt())) {
        LOG.info("Current product version [{}].", releaseManager.revertVersion());
    } else if (commandLine.hasOption(optionSetVersion.getLongOpt())) {
        String developVersion = commandLine.getOptionValue(optionDevelopVersion.getLongOpt());
        // 
        LOG.info("Current product version [{}].", releaseManager.setVersion(developVersion));
    } else if (commandLine.hasOption(optionGetVersion.getLongOpt())) {
        String branch = null;
        if (commandLine.hasOption(optionDevelopBranch.getLongOpt())) {
            branch = commandLine.getOptionValue(optionDevelopBranch.getLongOpt());
        }
        String currentVersion = releaseManager.getCurrentVersion(branch);
        // 
        LOG.info("Current product version [{}].", currentVersion);
    }
    // 
    LOG.info("Complete!");
}
Also used : Arrays(java.util.Arrays) Options(org.apache.commons.cli.Options) Autowired(org.springframework.beans.factory.annotation.Autowired) HelpFormatter(org.apache.commons.cli.HelpFormatter) StringUtils(org.apache.commons.lang3.StringUtils) ProductReleaseManager(eu.bcvsolutions.idm.tool.service.impl.ProductReleaseManager) DefaultParser(org.apache.commons.cli.DefaultParser) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) CommandLine(org.apache.commons.cli.CommandLine) Console(java.io.Console) CommandLineRunner(org.springframework.boot.CommandLineRunner) Option(org.apache.commons.cli.Option) ReleaseManager(eu.bcvsolutions.idm.tool.service.api.ReleaseManager) Properties(java.util.Properties) CommandLineParser(org.apache.commons.cli.CommandLineParser) ProjectManager(eu.bcvsolutions.idm.tool.service.impl.ProjectManager) IOException(java.io.IOException) ModuleReleaseManager(eu.bcvsolutions.idm.tool.service.impl.ModuleReleaseManager) FileInputStream(java.io.FileInputStream) Collectors(java.util.stream.Collectors) File(java.io.File) List(java.util.List) ParseException(org.apache.commons.cli.ParseException) OptionGroup(org.apache.commons.cli.OptionGroup) GuardedString(eu.bcvsolutions.idm.core.security.api.domain.GuardedString) InputStream(java.io.InputStream) BuildException(eu.bcvsolutions.idm.tool.exception.BuildException) Options(org.apache.commons.cli.Options) GuardedString(eu.bcvsolutions.idm.core.security.api.domain.GuardedString) ModuleReleaseManager(eu.bcvsolutions.idm.tool.service.impl.ModuleReleaseManager) GuardedString(eu.bcvsolutions.idm.core.security.api.domain.GuardedString) ProjectManager(eu.bcvsolutions.idm.tool.service.impl.ProjectManager) ProductReleaseManager(eu.bcvsolutions.idm.tool.service.impl.ProductReleaseManager) CommandLine(org.apache.commons.cli.CommandLine) OptionGroup(org.apache.commons.cli.OptionGroup) ProductReleaseManager(eu.bcvsolutions.idm.tool.service.impl.ProductReleaseManager) ReleaseManager(eu.bcvsolutions.idm.tool.service.api.ReleaseManager) ModuleReleaseManager(eu.bcvsolutions.idm.tool.service.impl.ModuleReleaseManager) Console(java.io.Console) Option(org.apache.commons.cli.Option) CommandLineParser(org.apache.commons.cli.CommandLineParser) BuildException(eu.bcvsolutions.idm.tool.exception.BuildException) DefaultParser(org.apache.commons.cli.DefaultParser)

Example 77 with DefaultParser

use of org.apache.commons.cli.DefaultParser in project toolkit by googleapis.

the class GeneratorMain method createCodeGeneratorOptionsFromProtoc.

public static ToolOptions createCodeGeneratorOptionsFromProtoc(String[] args) throws ParseException {
    Options options = new Options();
    // We can add more options as they become needed.
    options.addOption(DESCRIPTOR_SET_OPTION);
    options.addOption(LANGUAGE_OPTION);
    options.addOption(TARGET_API_PROTO_PACKAGE);
    options.addOption(TRANSPORT);
    CommandLine cl = (new DefaultParser()).parse(options, args);
    ToolOptions toolOptions = ToolOptions.create();
    toolOptions.set(ToolOptions.DESCRIPTOR_SET, cl.getOptionValue(DESCRIPTOR_SET_OPTION.getLongOpt()));
    toolOptions.set(GapicGeneratorApp.PROTO_PACKAGE, cl.getOptionValue(TARGET_API_PROTO_PACKAGE.getLongOpt()));
    toolOptions.set(GapicGeneratorApp.LANGUAGE, cl.getOptionValue(LANGUAGE_OPTION.getLongOpt()));
    checkFile(toolOptions.get(ToolOptions.DESCRIPTOR_SET));
    return toolOptions;
}
Also used : Options(org.apache.commons.cli.Options) ToolOptions(com.google.api.tools.framework.tools.ToolOptions) CommandLine(org.apache.commons.cli.CommandLine) ToolOptions(com.google.api.tools.framework.tools.ToolOptions) DefaultParser(org.apache.commons.cli.DefaultParser)

Example 78 with DefaultParser

use of org.apache.commons.cli.DefaultParser in project toolkit by googleapis.

the class GeneratorMain method packageGeneratorMain.

public static void packageGeneratorMain(String[] args) throws Exception {
    Options options = new Options();
    options.addOption("h", "help", false, "show usage");
    options.addOption(DESCRIPTOR_SET_OPTION);
    options.addOption(TARGET_API_PROTO_PACKAGE);
    options.addOption(SERVICE_YAML_NONREQUIRED_OPTION);
    options.addOption(LANGUAGE_OPTION);
    Option inputOption = Option.builder("i").longOpt("input").desc("The input directory containing the gRPC package.").hasArg().argName("INPUT-DIR").required(true).build();
    options.addOption(inputOption);
    options.addOption(OUTPUT_OPTION);
    options.addOption(PACKAGE_YAML2_OPTION);
    Option artifactTypeOption = Option.builder().longOpt("artifact_type").desc("Optional. Artifacts enabled for the generator. Currently supports " + "'GRPC' and 'PROTOBUF' and is ignored for all languages except Java").hasArg().argName("ARTIFACT-TYPE").required(false).build();
    options.addOption(artifactTypeOption);
    CommandLine cl = (new DefaultParser()).parse(options, args);
    if (cl.hasOption("help")) {
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("PackageGeneratorTool", options);
    }
    ToolOptions toolOptions = ToolOptions.create();
    toolOptions.set(PackageGeneratorApp.LANGUAGE, cl.getOptionValue(LANGUAGE_OPTION.getLongOpt()));
    toolOptions.set(PackageGeneratorApp.INPUT_DIR, cl.getOptionValue(inputOption.getLongOpt()));
    toolOptions.set(PackageGeneratorApp.OUTPUT_DIR, cl.getOptionValue(OUTPUT_OPTION.getLongOpt()));
    toolOptions.set(ToolOptions.DESCRIPTOR_SET, cl.getOptionValue(DESCRIPTOR_SET_OPTION.getLongOpt()));
    toolOptions.set(PackageGeneratorApp.PROTO_PACKAGE, cl.getOptionValue(TARGET_API_PROTO_PACKAGE.getLongOpt()));
    if (cl.getOptionValues(SERVICE_YAML_NONREQUIRED_OPTION.getLongOpt()) != null) {
        toolOptions.set(ToolOptions.CONFIG_FILES, Lists.newArrayList(cl.getOptionValues(SERVICE_YAML_NONREQUIRED_OPTION.getLongOpt())));
    }
    toolOptions.set(PackageGeneratorApp.PACKAGE_CONFIG2_FILE, cl.getOptionValue(PACKAGE_YAML2_OPTION.getLongOpt()));
    toolOptions.set(PackageGeneratorApp.LANGUAGE, cl.getOptionValue(LANGUAGE_OPTION.getLongOpt()));
    if (cl.getOptionValue(artifactTypeOption.getLongOpt()) != null) {
        toolOptions.set(PackageGeneratorApp.ARTIFACT_TYPE, PackagingArtifactType.of(cl.getOptionValue(artifactTypeOption.getLongOpt())));
    }
    PackageGeneratorApp generator = new PackageGeneratorApp(toolOptions);
    int exitCode = generator.run();
    System.exit(exitCode);
}
Also used : HelpFormatter(org.apache.commons.cli.HelpFormatter) Options(org.apache.commons.cli.Options) ToolOptions(com.google.api.tools.framework.tools.ToolOptions) CommandLine(org.apache.commons.cli.CommandLine) PackageGeneratorApp(com.google.api.codegen.packagegen.PackageGeneratorApp) Option(org.apache.commons.cli.Option) ToolOptions(com.google.api.tools.framework.tools.ToolOptions) DefaultParser(org.apache.commons.cli.DefaultParser)

Example 79 with DefaultParser

use of org.apache.commons.cli.DefaultParser in project toolkit by googleapis.

the class GeneratorMain method gapicGeneratorMain.

public static void gapicGeneratorMain(ArtifactType artifactType, String[] args) throws Exception {
    Options options = new Options();
    options.addOption("h", "help", false, "show usage");
    options.addOption(DESCRIPTOR_SET_OPTION);
    options.addOption(SERVICE_YAML_NONREQUIRED_OPTION);
    // TODO make required after artman passes this in
    options.addOption(LANGUAGE_NONREQUIRED_OPTION);
    options.addOption(GAPIC_YAML_NONREQUIRED_OPTION);
    options.addOption(PACKAGE_YAML2_OPTION);
    options.addOption(TARGET_API_PROTO_PACKAGE);
    options.addOption(OUTPUT_OPTION);
    options.addOption(SAMPLE_YAML_NONREQUIRED_OPTION);
    options.addOption(GRPC_SERVICE_CONFIG_OPTION);
    options.addOption(TRANSPORT);
    Option enabledArtifactsOption = Option.builder().longOpt("enabled_artifacts").desc("Optional. Artifacts enabled for the generator. " + "Currently supports 'surface' and 'test'.").hasArg().argName("ENABLED_ARTIFACTS").required(false).build();
    options.addOption(enabledArtifactsOption);
    Option devSamplesOption = Option.builder().longOpt("dev_samples").desc("Whether to generate samples in non-production-ready languages.").argName("DEV_SAMPLES").required(false).build();
    options.addOption(devSamplesOption);
    CommandLine cl = (new DefaultParser()).parse(options, args);
    if (cl.hasOption("help")) {
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("GapicGeneratorTool", options);
    }
    ToolOptions toolOptions = ToolOptions.create();
    toolOptions.set(ToolOptions.DESCRIPTOR_SET, cl.getOptionValue(DESCRIPTOR_SET_OPTION.getLongOpt()));
    // TODO(andrealin): Write system tests to ensure at least one option given.
    checkAtLeastOneOption(cl, SERVICE_YAML_NONREQUIRED_OPTION, TARGET_API_PROTO_PACKAGE);
    checkAtLeastOneOption(cl, GAPIC_YAML_NONREQUIRED_OPTION, TARGET_API_PROTO_PACKAGE);
    toolOptions.set(GapicGeneratorApp.PROTO_PACKAGE, cl.getOptionValue(TARGET_API_PROTO_PACKAGE.getLongOpt()));
    toolOptions.set(GapicGeneratorApp.LANGUAGE, cl.getOptionValue(LANGUAGE_NONREQUIRED_OPTION.getLongOpt()));
    toolOptions.set(GapicGeneratorApp.OUTPUT_FILE, cl.getOptionValue(OUTPUT_OPTION.getLongOpt(), ""));
    toolOptions.set(GapicGeneratorApp.PACKAGE_CONFIG2_FILE, cl.getOptionValue(PACKAGE_YAML2_OPTION.getLongOpt()));
    toolOptions.set(GapicGeneratorApp.GRPC_SERVICE_CONFIG, cl.getOptionValue(GRPC_SERVICE_CONFIG_OPTION.getLongOpt()));
    checkFile(toolOptions.get(ToolOptions.DESCRIPTOR_SET));
    if (cl.getOptionValue(TRANSPORT.getLongOpt()) != null) {
        toolOptions.set(GapicGeneratorApp.TRANSPORT, cl.getOptionValue(TRANSPORT.getLongOpt()));
    }
    if (cl.getOptionValues(SERVICE_YAML_NONREQUIRED_OPTION.getLongOpt()) != null) {
        toolOptions.set(ToolOptions.CONFIG_FILES, Lists.newArrayList(cl.getOptionValues(SERVICE_YAML_NONREQUIRED_OPTION.getLongOpt())));
        checkFiles(toolOptions.get(ToolOptions.CONFIG_FILES));
    }
    if (cl.getOptionValues(GAPIC_YAML_NONREQUIRED_OPTION.getLongOpt()) != null) {
        toolOptions.set(GapicGeneratorApp.GENERATOR_CONFIG_FILES, Lists.newArrayList(cl.getOptionValues(GAPIC_YAML_NONREQUIRED_OPTION.getLongOpt())));
        checkFiles(toolOptions.get(GapicGeneratorApp.GENERATOR_CONFIG_FILES));
    }
    if (cl.getOptionValues(SAMPLE_YAML_NONREQUIRED_OPTION.getLongOpt()) != null) {
        toolOptions.set(GapicGeneratorApp.SAMPLE_CONFIG_FILES, Lists.newArrayList(cl.getOptionValues(SAMPLE_YAML_NONREQUIRED_OPTION.getLongOpt())));
        checkFiles(toolOptions.get(GapicGeneratorApp.SAMPLE_CONFIG_FILES));
    }
    if (!Strings.isNullOrEmpty(toolOptions.get(GapicGeneratorApp.PACKAGE_CONFIG2_FILE))) {
        checkFile(toolOptions.get(GapicGeneratorApp.PACKAGE_CONFIG2_FILE));
    }
    if (cl.getOptionValues(enabledArtifactsOption.getLongOpt()) != null) {
        toolOptions.set(GapicGeneratorApp.ENABLED_ARTIFACTS, Lists.newArrayList(cl.getOptionValues(enabledArtifactsOption.getLongOpt())));
    }
    if (!Strings.isNullOrEmpty(toolOptions.get(GapicGeneratorApp.GRPC_SERVICE_CONFIG))) {
        checkFile(toolOptions.get(GapicGeneratorApp.GRPC_SERVICE_CONFIG));
    }
    toolOptions.set(GapicGeneratorApp.DEV_SAMPLES, cl.hasOption(devSamplesOption.getLongOpt()));
    String outputPath = toolOptions.get(GapicGeneratorApp.OUTPUT_FILE);
    GapicGeneratorApp codeGen = new GapicGeneratorApp(toolOptions, artifactType, new FileGapicWriter(outputPath));
    int exitCode = codeGen.run();
    System.exit(exitCode);
}
Also used : HelpFormatter(org.apache.commons.cli.HelpFormatter) FileGapicWriter(com.google.api.codegen.gapic.FileGapicWriter) Options(org.apache.commons.cli.Options) ToolOptions(com.google.api.tools.framework.tools.ToolOptions) CommandLine(org.apache.commons.cli.CommandLine) GapicGeneratorApp(com.google.api.codegen.gapic.GapicGeneratorApp) DiscoGapicGeneratorApp(com.google.api.codegen.discogapic.DiscoGapicGeneratorApp) Option(org.apache.commons.cli.Option) ToolOptions(com.google.api.tools.framework.tools.ToolOptions) DefaultParser(org.apache.commons.cli.DefaultParser)

Example 80 with DefaultParser

use of org.apache.commons.cli.DefaultParser in project toolkit by googleapis.

the class GeneratorMain method discoGapicMain.

public static void discoGapicMain(ArtifactType artifactType, String[] args) throws Exception {
    Options options = new Options();
    options.addOption("h", "help", false, "show usage");
    // TODO make required after artman passes this in
    options.addOption(LANGUAGE_NONREQUIRED_OPTION);
    options.addOption(DISCOVERY_DOC_OPTION);
    // TODO (andrealin): Make Gapic YAML optional
    options.addOption(GAPIC_YAML_OPTION);
    options.addOption(PACKAGE_YAML2_OPTION);
    options.addOption(OUTPUT_OPTION);
    Option enabledArtifactsOption = Option.builder().longOpt("enabled_artifacts").desc("Optional. Artifacts enabled for the generator. " + "Currently supports 'surface' and 'test'.").hasArg().argName("ENABLED_ARTIFACTS").required(false).build();
    options.addOption(enabledArtifactsOption);
    CommandLine cl = (new DefaultParser()).parse(options, args);
    if (cl.hasOption("help")) {
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("DiscoGapicGeneratorTool", options);
    }
    ToolOptions toolOptions = ToolOptions.create();
    toolOptions.set(DiscoGapicGeneratorApp.LANGUAGE, cl.getOptionValue(LANGUAGE_NONREQUIRED_OPTION.getLongOpt()));
    toolOptions.set(DiscoGapicGeneratorApp.DISCOVERY_DOC, cl.getOptionValue(DISCOVERY_DOC_OPTION.getLongOpt()));
    toolOptions.set(GapicGeneratorApp.OUTPUT_FILE, cl.getOptionValue(OUTPUT_OPTION.getLongOpt(), ""));
    toolOptions.set(GapicGeneratorApp.GENERATOR_CONFIG_FILES, Lists.newArrayList(cl.getOptionValues(GAPIC_YAML_OPTION.getLongOpt())));
    toolOptions.set(GapicGeneratorApp.PACKAGE_CONFIG2_FILE, cl.getOptionValue(PACKAGE_YAML2_OPTION.getLongOpt()));
    if (cl.getOptionValues(enabledArtifactsOption.getLongOpt()) != null) {
        toolOptions.set(GapicGeneratorApp.ENABLED_ARTIFACTS, Lists.newArrayList(cl.getOptionValues(enabledArtifactsOption.getLongOpt())));
    }
    DiscoGapicGeneratorApp codeGen = new DiscoGapicGeneratorApp(toolOptions, artifactType);
    int exitCode = codeGen.run();
    System.exit(exitCode);
}
Also used : HelpFormatter(org.apache.commons.cli.HelpFormatter) Options(org.apache.commons.cli.Options) ToolOptions(com.google.api.tools.framework.tools.ToolOptions) CommandLine(org.apache.commons.cli.CommandLine) DiscoGapicGeneratorApp(com.google.api.codegen.discogapic.DiscoGapicGeneratorApp) Option(org.apache.commons.cli.Option) ToolOptions(com.google.api.tools.framework.tools.ToolOptions) DefaultParser(org.apache.commons.cli.DefaultParser)

Aggregations

DefaultParser (org.apache.commons.cli.DefaultParser)344 CommandLine (org.apache.commons.cli.CommandLine)290 Options (org.apache.commons.cli.Options)241 CommandLineParser (org.apache.commons.cli.CommandLineParser)234 ParseException (org.apache.commons.cli.ParseException)224 HelpFormatter (org.apache.commons.cli.HelpFormatter)116 Option (org.apache.commons.cli.Option)67 File (java.io.File)34 IOException (java.io.IOException)32 HashMap (java.util.HashMap)30 JobConfig (edu.iu.dsc.tws.api.JobConfig)26 ArrayList (java.util.ArrayList)26 Config (edu.iu.dsc.tws.api.config.Config)25 Twister2Job (edu.iu.dsc.tws.api.Twister2Job)21 Properties (java.util.Properties)19 Test (org.junit.jupiter.api.Test)19 Field (core.field.Field)17 Level (java.util.logging.Level)13 ToolOptions (com.google.api.tools.framework.tools.ToolOptions)12 OptionGroup (org.apache.commons.cli.OptionGroup)11