Ejemplo n.º 1
0
    public SessionTerminal(CommandProcessor commandProcessor, ThreadIO threadIO)
        throws IOException {
      try {
        this.terminal = new Terminal(TERM_WIDTH, TERM_HEIGHT);
        terminal.write("\u001b\u005B20\u0068"); // set newline mode on

        in = new PipedOutputStream();
        out = new PipedInputStream();
        PrintStream pipedOut = new PrintStream(new PipedOutputStream(out), true);

        console =
            new Console(
                commandProcessor,
                threadIO,
                new PipedInputStream(in),
                pipedOut,
                pipedOut,
                new WebTerminal(TERM_WIDTH, TERM_HEIGHT),
                null,
                null);
        CommandSession session = console.getSession();
        session.put("APPLICATION", System.getProperty("karaf.name", "root"));
        session.put("USER", "karaf");
        session.put("COLUMNS", Integer.toString(TERM_WIDTH));
        session.put("LINES", Integer.toString(TERM_HEIGHT));
      } catch (IOException e) {
        LOG.info("Exception attaching to console", e);
        throw e;
      } catch (Exception e) {
        LOG.info("Exception attaching to console", e);
        throw (IOException) new IOException().initCause(e);
      }
      new Thread(console).start();
      new Thread(this).start();
    }
Ejemplo n.º 2
0
  private void run(
      final CommandProcessorImpl commandProcessor,
      String[] args,
      final InputStream in,
      final PrintStream out,
      final PrintStream err)
      throws Exception {

    if (args.length > 0) {
      // Commands have the form: jclouds:category-action.
      List<String> commands = Lists.newArrayList(commandProcessor.getCommands());
      Collections.sort(commands);
      if (!commands.contains(args[0])) {
        final String INDENT = "    ";
        StringBuilder sb =
            new StringBuilder("Usage: jclouds {category} {action} {options/arguments}");

        // list commands
        sb.append("\n\nValid commands:");
        for (String command : commands) {
          if (command.startsWith("jclouds:")) {
            command = command.substring("jclouds:".length());
            int index = command.indexOf('-');
            String category = command.substring(0, index);
            String action = command.substring(index + 1);
            sb.append('\n').append(INDENT).append(category).append(' ').append(action);
            // TODO: expose command descriptions
          }
        }

        // list options
        sb.append("\n\nOptions:")
            .append('\n')
            .append(INDENT)
            .append("--properties: File containing properties")
            .append('\n')
            .append(INDENT)
            .append("--provider:   The id of the provider")
            .append('\n')
            .append(INDENT)
            .append("--api:        The id of the api")
            .append('\n')
            .append(INDENT)
            .append("--endpoint:   The endpoint")
            .append('\n')
            .append(INDENT)
            .append("--identity:   The identity")
            .append('\n')
            .append(INDENT)
            .append("--credential: The credential");

        throw new CommandNotFoundException(sb.toString());
      }

      StringBuilder sb = new StringBuilder();
      for (int i = 0; i < args.length; i++) {
        if (i > 0) {
          sb.append(" ");
        }
        if (args[i].contains(" ")) {
          // quote arguments with spaces
          sb.append("\"").append(args[i]).append("\"");
        } else {
          sb.append(args[i]);
        }
      }

      // Shell is directly executing a sub/command, we don't setup a terminal and console
      // in this case, this avoids us reading from stdin un-necessarily.
      CommandSession session = commandProcessor.createSession(in, out, err);
      session.put("USER", user);
      session.put("APPLICATION", application);
      session.put(NameScoping.MULTI_SCOPE_MODE_KEY, Boolean.toString(isMultiScopeMode()));
      session.execute(sb);
    } else {
      // We are going into full blown interactive shell mode.

      final TerminalFactory terminalFactory = new TerminalFactory();
      final Terminal terminal = terminalFactory.getTerminal();
      Console console = createConsole(commandProcessor, in, out, err, terminal);
      CommandSession session = console.getSession();
      session.put("USER", user);
      session.put("APPLICATION", application);
      session.put(NameScoping.MULTI_SCOPE_MODE_KEY, Boolean.toString(isMultiScopeMode()));
      session.put(
          "#LINES",
          new Function() {
            public Object execute(CommandSession session, List<Object> arguments) throws Exception {
              return Integer.toString(terminal.getHeight());
            }
          });
      session.put(
          "#COLUMNS",
          new Function() {
            public Object execute(CommandSession session, List<Object> arguments) throws Exception {
              return Integer.toString(terminal.getWidth());
            }
          });
      session.put(".jline.terminal", terminal);

      console.run();

      terminalFactory.destroy();
    }
  }