Search in sources :

Example 71 with Options

use of org.apache.commons.cli.Options in project zm-mailbox by Zimbra.

the class WaitSetValidator method main.

/**
     * @param args
     */
public static void main(String[] args) {
    CliUtil.toolSetup();
    WaitSetValidator t = new WaitSetValidator();
    CommandLineParser parser = new PosixParser();
    Options options = new Options();
    options.addOption("i", "id", true, "Wait Set ID");
    options.addOption("h", "host", true, "Hostname");
    options.addOption("u", "user", true, "Username");
    options.addOption("p", "pw", true, "Password");
    options.addOption("?", "help", false, "Help");
    options.addOption("v", "verbose", false, "Verbose");
    CommandLine cl = null;
    boolean err = false;
    String[] hosts;
    String[] ids;
    try {
        cl = parser.parse(options, args, true);
    } catch (ParseException pe) {
        printError("error: " + pe.getMessage());
        err = true;
    }
    if (err || cl.hasOption('?')) {
        t.usage();
    }
    String id = null, host = null, user = null, pw = null;
    if (!cl.hasOption('i'))
        t.usage();
    id = cl.getOptionValue('i');
    if (cl.hasOption('h'))
        host = cl.getOptionValue('h');
    else
        host = "http://localhost:7071/service/admin";
    if (cl.hasOption('u'))
        user = cl.getOptionValue('u');
    else
        user = "admin";
    if (cl.hasOption('p'))
        pw = cl.getOptionValue('p');
    else
        pw = "test123";
    if (cl.hasOption('v'))
        t.setVerbose(true);
    hosts = host.split(",");
    ids = id.split(",");
    if (hosts.length != ids.length) {
        System.err.println("If multiple hosts or ids are specified, the same number is required of each");
        System.exit(3);
    }
    for (int i = 0; i < hosts.length; i++) {
        if (i > 0)
            System.out.println("\n\n");
        System.out.println("Checking server " + hosts[i] + " waitsetId=" + ids[i]);
        t.run(ids[i], hosts[i], user, pw);
    }
}
Also used : Options(org.apache.commons.cli.Options) CommandLine(org.apache.commons.cli.CommandLine) PosixParser(org.apache.commons.cli.PosixParser) CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.commons.cli.ParseException)

Example 72 with Options

use of org.apache.commons.cli.Options in project zm-mailbox by Zimbra.

the class ZMailboxUtil method main.

public static void main(String[] args) throws IOException, ServiceException {
    CliUtil.toolSetup();
    SoapTransport.setDefaultUserAgent("zmmailbox", BuildInfo.VERSION);
    ZMailboxUtil pu = new ZMailboxUtil();
    CommandLineParser parser = new GnuParser();
    Options options = new Options();
    options.addOption("a", "admin", true, "admin account name to auth as");
    options.addOption("z", "zadmin", false, "use zimbra admin name/password from localconfig for admin/password");
    options.addOption("h", "help", false, "display usage");
    options.addOption("f", "file", true, "use file as input stream");
    options.addOption("u", "url", true, "http[s]://host[:port] of server to connect to");
    options.addOption("r", "protocol", true, "protocol to use for request/response [soap11, soap12, json]");
    options.addOption("m", "mailbox", true, "mailbox to open");
    options.addOption(null, "auth", true, "account name to auth as; defaults to -m unless -A is used");
    options.addOption("A", "admin-priv", false, "execute requests with admin privileges");
    options.addOption("p", "password", true, "auth password");
    options.addOption("P", "passfile", true, "filename with password in it");
    options.addOption("t", "timeout", true, "timeout (in seconds)");
    options.addOption("v", "verbose", false, "verbose mode");
    options.addOption("d", "debug", false, "debug mode");
    options.addOption(SoapCLI.OPT_AUTHTOKEN);
    options.addOption(SoapCLI.OPT_AUTHTOKENFILE);
    CommandLine cl = null;
    boolean err = false;
    try {
        cl = parser.parse(options, args, true);
    } catch (ParseException pe) {
        stderr.println("error: " + pe.getMessage());
        err = true;
    }
    if (err || cl.hasOption('h')) {
        pu.usage();
    }
    try {
        boolean isAdmin = false;
        pu.setVerbose(cl.hasOption('v'));
        if (cl.hasOption('a')) {
            pu.setAdminAccountName(cl.getOptionValue('a'));
            pu.setUrl(DEFAULT_ADMIN_URL, true);
            isAdmin = true;
        }
        if (cl.hasOption('z')) {
            pu.setAdminAccountName(LC.zimbra_ldap_user.value());
            pu.setPassword(LC.zimbra_ldap_password.value());
            pu.setUrl(DEFAULT_ADMIN_URL, true);
            isAdmin = true;
        }
        if (cl.hasOption(SoapCLI.O_AUTHTOKEN) && cl.hasOption(SoapCLI.O_AUTHTOKENFILE))
            pu.usage();
        if (cl.hasOption(SoapCLI.O_AUTHTOKEN)) {
            pu.setAdminAuthToken(ZAuthToken.fromJSONString(cl.getOptionValue(SoapCLI.O_AUTHTOKEN)));
            pu.setUrl(DEFAULT_ADMIN_URL, true);
            isAdmin = true;
        }
        if (cl.hasOption(SoapCLI.O_AUTHTOKENFILE)) {
            String authToken = StringUtil.readSingleLineFromFile(cl.getOptionValue(SoapCLI.O_AUTHTOKENFILE));
            pu.setAdminAuthToken(ZAuthToken.fromJSONString(authToken));
            pu.setUrl(DEFAULT_ADMIN_URL, true);
            isAdmin = true;
        }
        String authAccount, targetAccount;
        if (cl.hasOption('m')) {
            if (!cl.hasOption('p') && !cl.hasOption('P') && !cl.hasOption('y') && !cl.hasOption('Y') && !cl.hasOption('z')) {
                throw ZClientException.CLIENT_ERROR("-m requires one of the -p/-P/-y/-Y/-z options", null);
            }
            targetAccount = cl.getOptionValue('m');
        } else {
            targetAccount = null;
        }
        if ((cl.hasOption("A") || cl.hasOption("auth")) && !cl.hasOption('m')) {
            throw ZClientException.CLIENT_ERROR("-A/--auth requires -m", null);
        }
        if (cl.hasOption("A")) {
            if (!isAdmin) {
                throw ZClientException.CLIENT_ERROR("-A requires admin auth", null);
            }
            if (cl.hasOption("auth")) {
                throw ZClientException.CLIENT_ERROR("-A cannot be combined with --auth", null);
            }
            authAccount = null;
        } else if (cl.hasOption("auth")) {
            authAccount = cl.getOptionValue("auth");
        } else {
            // default case
            authAccount = targetAccount;
        }
        if (!StringUtil.isNullOrEmpty(authAccount))
            pu.setAuthAccountName(authAccount);
        if (!StringUtil.isNullOrEmpty(targetAccount))
            pu.setTargetAccountName(targetAccount);
        if (cl.hasOption('u'))
            pu.setUrl(cl.getOptionValue('u'), isAdmin);
        if (cl.hasOption('p'))
            pu.setPassword(cl.getOptionValue('p'));
        if (cl.hasOption('P')) {
            pu.setPassword(StringUtil.readSingleLineFromFile(cl.getOptionValue('P')));
        }
        if (cl.hasOption('d'))
            pu.setDebug(true);
        if (cl.hasOption('t'))
            pu.setTimeout(cl.getOptionValue('t'));
        args = cl.getArgs();
        pu.setInteractive(args.length < 1);
        pu.initMailbox();
        if (args.length < 1) {
            InputStream is = null;
            if (cl.hasOption('f')) {
                is = new FileInputStream(cl.getOptionValue('f'));
            } else {
                if (LC.command_line_editing_enabled.booleanValue()) {
                    try {
                        CliUtil.enableCommandLineEditing(LC.zimbra_home.value() + "/.zmmailbox_history");
                    } catch (IOException e) {
                        System.err.println("Command line editing will be disabled: " + e);
                        if (pu.mGlobalVerbose) {
                            e.printStackTrace(System.err);
                        }
                    }
                }
                // This has to happen last because JLine modifies System.in.
                is = System.in;
            }
            pu.interactive(new BufferedReader(new InputStreamReader(is, "UTF-8")));
        } else {
            pu.execute(args);
        }
    } catch (ServiceException e) {
        Throwable cause = e.getCause();
        stderr.println("ERROR: " + e.getCode() + " (" + e.getMessage() + ")" + (cause == null ? "" : " (cause: " + cause.getClass().getName() + " " + cause.getMessage() + ")"));
        if (pu.mGlobalVerbose)
            e.printStackTrace(stderr);
        System.exit(2);
    }
}
Also used : Options(org.apache.commons.cli.Options) InputStreamReader(java.io.InputStreamReader) GZIPInputStream(java.util.zip.GZIPInputStream) BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) SharedByteArrayInputStream(javax.mail.util.SharedByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) GnuParser(org.apache.commons.cli.GnuParser) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) CommandLine(org.apache.commons.cli.CommandLine) ServiceException(com.zimbra.common.service.ServiceException) BufferedReader(java.io.BufferedReader) CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.commons.cli.ParseException)

Example 73 with Options

use of org.apache.commons.cli.Options in project zm-mailbox by Zimbra.

the class VolumeCLI method printOpt.

private void printOpt(String optStr, int leftPad) {
    Options options = getOptions();
    Option opt = options.getOption(optStr);
    StringBuilder buf = new StringBuilder();
    buf.append(Strings.repeat(" ", leftPad));
    buf.append('-').append(opt.getOpt()).append(",--").append(opt.getLongOpt());
    if (opt.hasArg()) {
        buf.append(" <arg>");
    }
    buf.append(Strings.repeat(" ", 35 - buf.length()));
    buf.append(opt.getDescription());
    System.err.println(buf.toString());
}
Also used : Options(org.apache.commons.cli.Options) Option(org.apache.commons.cli.Option)

Example 74 with Options

use of org.apache.commons.cli.Options in project zm-mailbox by Zimbra.

the class VolumeCLI method setupCommandLineOptions.

@Override
protected void setupCommandLineOptions() {
    super.setupCommandLineOptions();
    Options options = getOptions();
    OptionGroup og = new OptionGroup();
    og.addOption(new Option(O_A, "add", false, "Adds a volume."));
    og.addOption(new Option(O_D, "delete", false, "Deletes a volume."));
    og.addOption(new Option(O_L, "list", false, "Lists volumes."));
    og.addOption(new Option(O_E, "edit", false, "Edits a volume."));
    og.addOption(new Option(O_DC, "displayCurrent", false, "Displays the current volumes."));
    og.addOption(new Option(O_SC, "setCurrent", false, "Sets the current volume."));
    og.addOption(new Option(O_TS, "turnOffSecondary", false, "Turns off the current secondary message volume"));
    og.setRequired(true);
    options.addOptionGroup(og);
    options.addOption(O_ID, "id", true, "Volume ID");
    options.addOption(O_T, "type", true, "Volume type (primaryMessage, secondaryMessage, or index)");
    options.addOption(O_N, "name", true, "volume name");
    options.addOption(O_P, "path", true, "Root path");
    options.addOption(O_C, "compress", true, "Compress blobs; \"true\" or \"false\"");
    options.addOption(O_CT, "compressionThreshold", true, "Compression threshold; default 4KB");
    options.addOption(SoapCLI.OPT_AUTHTOKEN);
    options.addOption(SoapCLI.OPT_AUTHTOKENFILE);
}
Also used : Options(org.apache.commons.cli.Options) OptionGroup(org.apache.commons.cli.OptionGroup) Option(org.apache.commons.cli.Option)

Example 75 with Options

use of org.apache.commons.cli.Options in project zm-mailbox by Zimbra.

the class TestLdapReadTimeout method main.

/**
     * /Users/pshao/dev/workspace/sandbox/sandbox/bin>/System/Library/Frameworks/JavaVM.framework/Versions/1.6/Home/bin/java  LdapReadTimeout
     * /System/Library/Frameworks/JavaVM.framework/Versions/1.6/Home/bin/java LdapReadTimeout
     * 
     * zmjava com.zimbra.qa.unittest.TestLdapReadTimeout -s 5
     * zmjava com.zimbra.qa.unittest.TestLdapReadTimeout -H ldap://localhost:389 -D uid=zimbra,cn=admins,cn=zimbra -w zimbra -s 5
     * 
     * @param args
     */
public static void main(String[] args) throws Exception {
    Options options = new Options();
    options.addOption(O_HELP, false, "print usage");
    options.addOption(O_SLEEP, true, "upon hitting an Exception, minutes to wait until exiting the program.  " + "If not specified, will exit immediately.");
    options.addOption(O_URI, true, "URI. e.g. ldap://localhost:389");
    options.addOption(O_BINDDN, true, "bind DN");
    options.addOption(O_PASSWORD, true, "password");
    CommandLine cl = null;
    try {
        CommandLineParser parser = new GnuParser();
        cl = parser.parse(options, args);
        if (cl == null) {
            throw new ParseException("");
        }
    } catch (ParseException e) {
        usage(options);
        e.printStackTrace();
        System.exit(1);
    }
    if (cl.hasOption(O_HELP)) {
        usage(options);
        System.exit(0);
    }
    String uri = null;
    String bindDN = null;
    String password = null;
    Integer minutesToWait = null;
    if (cl.hasOption(O_URI)) {
        uri = cl.getOptionValue(O_URI);
    } else {
        uri = "ldap://localhost:389";
    }
    if (cl.hasOption(O_BINDDN)) {
        bindDN = cl.getOptionValue(O_BINDDN);
    } else {
        bindDN = "uid=zimbra,cn=admins,cn=zimbra";
    }
    if (cl.hasOption(O_PASSWORD)) {
        password = cl.getOptionValue(O_PASSWORD);
    } else {
        password = "zimbra";
    }
    if (cl.hasOption(O_SLEEP)) {
        String wait = cl.getOptionValue(O_SLEEP);
        try {
            minutesToWait = Integer.valueOf(wait);
        } catch (NumberFormatException e) {
            usage(options);
            e.printStackTrace();
            System.exit(1);
        }
    }
    LdapReadTimeoutTester tester = null;
    try {
        // tester = new JNDITest(uri, bindDN, password);  // fails
        // works
        tester = new UnboundIDTest(uri, bindDN, password);
        System.out.println("=============");
        System.out.println(tester.getClass().getCanonicalName());
        System.out.println("LDAP server URI: " + uri);
        System.out.println("bind DN: " + bindDN);
        System.out.println("=============");
        System.out.println();
    } catch (Exception e) {
        e.printStackTrace();
        System.exit(1);
    }
    long startTime = System.currentTimeMillis();
    test(tester);
    long endTime = System.currentTimeMillis();
    long elapsed = endTime - startTime;
    SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    System.out.println();
    System.out.println(tester.getClass().getCanonicalName());
    System.out.println("Started at: " + fmt.format(new Date(startTime)));
    System.out.println("Ended at: " + fmt.format(new Date(endTime)));
    System.out.println("Elapsed = " + (elapsed / 1000) + " seconds");
    System.out.println();
    if (minutesToWait != null) {
        System.out.println("Sleeping for " + minutesToWait + " minutes before exiting...");
        Thread.sleep(minutesToWait * 60 * 1000);
    }
}
Also used : Options(org.apache.commons.cli.Options) LDAPConnectionOptions(com.unboundid.ldap.sdk.LDAPConnectionOptions) GnuParser(org.apache.commons.cli.GnuParser) ParseException(org.apache.commons.cli.ParseException) Date(java.util.Date) CommandLine(org.apache.commons.cli.CommandLine) CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.commons.cli.ParseException) SimpleDateFormat(java.text.SimpleDateFormat)

Aggregations

Options (org.apache.commons.cli.Options)1086 CommandLine (org.apache.commons.cli.CommandLine)557 CommandLineParser (org.apache.commons.cli.CommandLineParser)382 ParseException (org.apache.commons.cli.ParseException)341 Option (org.apache.commons.cli.Option)325 HelpFormatter (org.apache.commons.cli.HelpFormatter)275 GnuParser (org.apache.commons.cli.GnuParser)207 DefaultParser (org.apache.commons.cli.DefaultParser)166 Test (org.junit.Test)148 PosixParser (org.apache.commons.cli.PosixParser)135 IOException (java.io.IOException)118 File (java.io.File)97 OptionGroup (org.apache.commons.cli.OptionGroup)56 DMLScript (org.apache.sysml.api.DMLScript)56 Path (org.apache.hadoop.fs.Path)54 ArrayList (java.util.ArrayList)38 BasicParser (org.apache.commons.cli.BasicParser)36 Properties (java.util.Properties)33 Configuration (org.apache.hadoop.conf.Configuration)31 FileInputStream (java.io.FileInputStream)29