public static LoginCommand parse()

in odps-console-basic/src/main/java/com/aliyun/openservices/odps/console/commands/LoginCommand.java [96:166]


  public static LoginCommand parse(List<String> optionList, ExecutionContext sessionContext)
      throws ODPSConsoleException {

    LoginCommand command;

    // 处理login, 这是默认的方式,走aliyun的签名认证
    if (optionList.contains(OPTION_USER) && optionList.contains(OPTION_PASSWORD)
        && optionList.indexOf(OPTION_USER) + 1 < optionList.size()
        && optionList.indexOf(OPTION_PASSWORD) + 1 < optionList.size()) {

      String uPara = optionList.get(optionList.indexOf(OPTION_USER) + 1);
      String pPara = optionList.get(optionList.indexOf(OPTION_PASSWORD) + 1);

      // 如果-u\-p后面还是命令,则是无效的命令
      if (uPara.startsWith("-") || pPara.startsWith("-")) {
        throw new ODPSConsoleException(ODPSConsoleConstants.BAD_COMMAND);
      }

      command = new LoginCommand(uPara, pPara, null, sessionContext);

      // 消费掉这一个参数
      optionList.remove(optionList.indexOf(OPTION_USER));
      optionList.remove(optionList.indexOf(OPTION_PASSWORD));
      optionList.remove(optionList.indexOf(uPara));
      optionList.remove(optionList.indexOf(pPara));

    } else {
      // Handle the login info based on the account provider.
      String accountProviderStr =
          ODPSConsoleUtils.shiftOption(optionList, LONG_OPTION_ACCOUNT_PROVIDER);

      if (accountProviderStr == null) {
        return null;
      }

      AccountProvider accountProvider = AccountProvider.valueOf(accountProviderStr.toUpperCase());
      command = new LoginCommand(accountProvider, null, sessionContext);
      switch (accountProvider) {
        case ALIYUN: {
          String accessId = ODPSConsoleUtils.shiftOption(optionList, LONG_OPTION_ACCESS_ID);
          String accessKey = ODPSConsoleUtils.shiftOption(optionList, LONG_OPTION_ACCESS_KEY);
          if (accessId == null || accessKey == null) {
            String errMsg = "Aliyun account requires accessKeyId and accessKeySecret.";
            throw new ODPSConsoleException(errMsg);
          }
          command.setAccessId(accessId);
          command.setAccessKey(accessKey);
          break;
        }
        case STS: {
          String accessId = ODPSConsoleUtils.shiftOption(optionList, LONG_OPTION_ACCESS_ID);
          String accessKey = ODPSConsoleUtils.shiftOption(optionList, LONG_OPTION_ACCESS_KEY);
          String stsToken = ODPSConsoleUtils.shiftOption(optionList, LONG_OPTION_STS_TOKEN);
          if (accessId == null || accessKey == null || stsToken == null) {
            String errMsg = "STS account requires accessKeyId, accessKeySecret and STS token";
            throw new ODPSConsoleException(errMsg);
          }
          command.setAccessId(accessId);
          command.setAccessKey(accessKey);
          command.setStsToken(stsToken);
          break;
        }
        case TAOBAO:
        case BEARER_TOKEN:
        default:
          throw new ODPSConsoleException(ODPSConsoleConstants.UNSUPPORTED_ACCOUNT_PROVIDER);
      }
    }

    return command;
  }