All Downloads are FREE. Search and download functionalities are using the official Maven repository.

sap.prd.cmintegration.cli.Commands Maven / Gradle / Ivy

There is a newer version: 3.0.0
Show newest version
package sap.prd.cmintegration.cli;

import static com.google.common.collect.Sets.newHashSet;
import static java.lang.String.format;
import static java.util.Arrays.asList;
import static sap.prd.cmintegration.cli.Commands.Helpers.getArgsLogString;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Optional;
import java.util.Set;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.StatusLine;
import org.apache.olingo.client.api.communication.ODataClientErrorException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.collect.Sets;
import com.sap.cmclient.http.UnexpectedHttpResponseException;

import sap.ai.st.cm.plugins.ciintegration.odataclient.CMODataSolmanClient;

/**
 * Helpers for using/calling commands.
 */
class Commands {

    final static private Logger logger = LoggerFactory.getLogger(Commands.class);
    private final static String DASH = "-";

    /**
     * The common command line options used by all commands.
     */
    static class CMOptions {

        static Option USER = newOption("u", "user", "Service user.", "user", true),
                      PASSWORD = newOption("p", "password", "Service password, if '-' is provided, password will be read from stdin.", "pwd", true),
                      BACKEND_TYPE = newOption("t", "backend-type", format("Backend Type, one of %s.", asList(BackendType.values())), "type", true),
                      HOST = newOption("e", "endpoint", "Service endpoint.", "url", true),
                      HELP = newOption("h", "help", "Prints this help.", null, false),
                      VERSION = newOption("v", "version", "Prints the version.", null, false),

                      CHANGE_ID = newOption("cID", "change-id", "changeID.", "cID", false),
                      DEVELOPMENT_SYSTEM_ID = newOption("dID", "development-system-id", "DevelopmentSystemID", "devSysID", false),

                      RETURN_CODE = newOption("rc", "return-code",
                          format("If used with this option return code is %s " +
                          "in case of a modifiable transport and %d in case " +
                          "the transport is not modifiable. In this mode nothing is " +
                          "emitted to STDOUT.", ExitException.ExitCodes.OK, ExitException.ExitCodes.FALSE), null, false);

        static Option newOption(String shortKey, String longKey, String desc, String argName, boolean required) {
            return Option.builder(shortKey)
                        .hasArg(argName != null)
                        .argName(argName)
                        .longOpt(longKey)
                        .desc(desc)
                        .required(required).build();
        }
    }

    /**
     * Common helper methods.
     */
    static class Helpers {

        static Options getStandardOptions() {
            return addStandardParameters(new Options());
        }

        static Options addStandardParameters(Options o) {
            return getStandardParameters(o, false);
        }

        static Options getStandardParameters(boolean optional) {
            return getStandardParameters(new Options(), optional);
        }

        static Options getStandardParameters(Options options, boolean optional) {
            Set




© 2015 - 2024 Weber Informatics LLC | Privacy Policy