public void run()

in odps-console-basic/src/main/java/com/aliyun/openservices/odps/console/commands/InteractiveCommand.java [51:139]


  public void run() throws OdpsException, ODPSConsoleException {

    // 设定交互模式
    isInteractiveMode = true;
    // 交互模式没有step的说法
    getContext().setStep(0);
    getContext().setInteractiveMode(isInteractiveMode);

    // 欢迎版本信息
    getWriter().writeError(ODPSConsoleConstants.LOGO);
    getWriter().writeError(ODPSConsoleConstants.ALIYUN_ODPS_UTILITIES_VERSION);

    checkUpdate();

    // window下用sconner来读取command,其它的都用jline来处理,因为jline在window下处理不好输入。
    consoleReader = ODPSConsoleUtils.getOdpsConsoleReader();

    String inputStr = "";
    String endPoint = getContext().getEndpoint();
    if (StringUtils.isNullOrEmpty(endPoint)) {
      throw new ODPSConsoleException("Failed: endpoint cannot be null or empty.");
    }

    if (getContext().isInitialized()) {
      getContext().print();
    } else {
      if (getContext().getProjectName() != null && !getContext().getProjectName().isEmpty()) {
        String projectName = getContext().getProjectName();
        String message = String.format("Connecting to %s, project: %s", endPoint, projectName);
        // TODO schema: connecting to has no schema info
        System.err.println(message);
        try {
          String commandText = "use project " + projectName;
          UseProjectCommand useProjectCommand =
              new UseProjectCommand(commandText, getContext(), projectName);
          useProjectCommand.run();
          System.err.println("Connected!");
        } catch (Exception ex) {
          System.err.println("Accessing project '" + projectName + "' failed: " + ex.getMessage());
        }
      }
    }

    if (getContext().getAutoSessionMode()) {
      SessionUtils.autoAttachSession(getContext(), getCurrentOdps());
    }

    // q;会退出,还有一种情况,ctrl+d时inputStr返回null
    while (inputStr != null) {
      // 和declient一样,在交互模式下,忽略注释行
      if (!StringUtils.isNullOrEmpty(inputStr) && !inputStr.trim().startsWith("--")) {
        // 如果输入不为空,则解析命令并执行
        try {
          // drop command, need confirm
          if (isConfirm(inputStr)) {
            AbstractCommand command =
                CommandParserUtils.parseCommand(inputStr, this.getContext());
            command.execute();
          }

          if (quit) {
            break;
          }
        } catch (UserInterruptException e) {
          // isConfirm may throw too
          inputStr = "";
        } catch (Exception e) {
          String extraMsg = "";
          if (e instanceof OdpsException) {
             extraMsg = String.format(" [ RequestId: %s ]. ", ((OdpsException) e).getRequestId());
          }
          getWriter().writeError(ODPSConsoleConstants.FAILED_MESSAGE + e.getMessage() + extraMsg);
          if (StringUtils.isNullOrEmpty(e.getMessage())) {
            getWriter().writeError(StringUtils.stringifyException(e));
          }
          getWriter().writeDebug(e);
        }
      }

      if (getContext().getProjectName() != null) {
        // inputStr will end with ';', but there may be multiple '\n' before ';'
        // e.g. show tables\n\n\n;
        inputStr = consoleReader.readLine(getPrompt());
      }
    }

    // 退出后,换一行,显示格式更好看一些
    System.err.println();
  }