@ShellMethod(value = "Read and execute commands from a file.") public void script(File file) throws IOException { Reader reader = new FileReader(file); try (FileInputProvider inputProvider = new FileInputProvider(reader, parser)) { shell.run(inputProvider); } }
@Bean public Shell shell(@Qualifier("main") ResultHandler resultHandler) { return new Shell(resultHandler); }
int l = 0; String best = null; for (String command : shell.listCommands().keySet()) { if (buffer.startsWith(command) && command.length() > l) { l = command.length(); shell.run(new SshShellInputProvider(reader, promptProvider)); LOGGER.debug("{}: end", session.toString()); quit(0);
String raw = input.rawText(); Input newInput = () -> raw != null && isKeyCharInLine(raw) ? raw.substring(0, firstIndexOfKeyChar(raw)) : raw; Object toReturn = super.evaluate(newInput); if (isKeyCharInList(words)) { List<Integer> indexes = IntStream.range(0, words.size()).filter(i -> KEY_CHARS.contains(words.get(i))).boxed().collect(Collectors.toList());
/** * Gather completion proposals given some (incomplete) input the user has already typed * in. When and how this method is invoked is implementation specific and decided by the * actual user interface. */ public List<CompletionProposal> complete(CompletionContext context) { String prefix = context.upToCursor(); List<CompletionProposal> candidates = new ArrayList<>(); candidates.addAll(commandsStartingWith(prefix)); String best = findLongestCommand(prefix); if (best != null) { CompletionContext argsContext = context.drop(best.split(" ").length); // Try to complete arguments MethodTarget methodTarget = methodTargets.get(best); Method method = methodTarget.getMethod(); List<MethodParameter> parameters = Utils.createMethodParameters(method).collect(Collectors.toList()); for (ParameterResolver resolver : parameterResolvers) { for (int index = 0; index < parameters.size(); index++) { MethodParameter parameter = parameters.get(index); if (resolver.supports(parameter)) { resolver.complete(parameter, argsContext).stream().forEach(candidates::add); } } } } return candidates; }
@Override public AttributedString highlight(LineReader reader, String buffer) { int l = 0; String best = null; for (String command : shell.listCommands().keySet()) { if (buffer.startsWith(command) && command.length() > l) { l = command.length(); best = command; } } if (best != null) { return new AttributedStringBuilder(buffer.length()).append(best, AttributedStyle.BOLD).append(buffer.substring(l)).toAttributedString(); } else { return new AttributedString(buffer, AttributedStyle.DEFAULT.foreground(AttributedStyle.RED)); } } })
@Override public void complete(LineReader reader, ParsedLine line, List<Candidate> candidates) { CompletingParsedLine cpl = (line instanceof CompletingParsedLine) ? ((CompletingParsedLine) line) : t -> t; CompletionContext context = new CompletionContext(sanitizeInput(line.words()), line.wordIndex(), line.wordCursor()); List<CompletionProposal> proposals = shell.complete(context); proposals.stream() .map(p -> new Candidate( p.dontQuote() ? p.value() : cpl.emit(p.value()).toString(), p.displayText(), p.category(), p.description(), null, null, true) ) .forEach(candidates::add); }
result = evaluate(input); if (result != NO_INPUT && !(result instanceof ExitRequest)) { resultHandler.handleResult(result);
@Override public void run(ApplicationArguments args) throws Exception { List<File> scriptsToRun = args.getNonOptionArgs().stream() .filter(s -> s.startsWith("@")) .map(s -> new File(s.substring(1))) .collect(Collectors.toList()); boolean batchEnabled = environment.getProperty(SPRING_SHELL_SCRIPT_ENABLED, boolean.class, true); if (!scriptsToRun.isEmpty() && batchEnabled) { InteractiveShellApplicationRunner.disable(environment); for (File file : scriptsToRun) { try (Reader reader = new FileReader(file); FileInputProvider inputProvider = new FileInputProvider(reader, parser)) { shell.run(inputProvider); } } } } //end::documentation[]
@Override public void run(ApplicationArguments args) throws Exception { boolean interactive = isEnabled(); if (interactive) { InputProvider inputProvider = new JLineInputProvider(lineReader, promptProvider); shell.run(inputProvider); } }
@Override public void run(ApplicationArguments args) throws Exception { // if we have args, assume one time run ArrayList<String> argsToShellCommand = new ArrayList<>(); for (String arg : args.getSourceArgs()) { // consider client connection options as non command args if (!arg.contains("spring.cloud.skipper.client")) { argsToShellCommand.add(arg); } } if (argsToShellCommand.size() > 0) { if (ShellUtils.hasHelpOption(args)) { // going into 'help' mode. HelpAwareShellApplicationRunner already // printed usage, we just force running just help. argsToShellCommand.clear(); argsToShellCommand.add("help"); } } if (!argsToShellCommand.isEmpty()) { InteractiveShellApplicationRunner.disable(environment); shell.run(new StringInputProvider(argsToShellCommand)); } }
@Override public void run() { Terminal terminal; try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); PrintStream ps = new PrintStream(baos, true, "utf-8")) { terminal = TerminalBuilder.builder().system(false).type(terminalType).streams(is, os).build(); DefaultResultHandler resultHandler = new DefaultResultHandler(); resultHandler.setTerminal(terminal); shellBanner.printBanner(environment, this.getClass(), ps); resultHandler.handleResult(new String(baos.toByteArray(), StandardCharsets.UTF_8)); resultHandler.handleResult("Please type `help` to see available commands"); LineReader reader = LineReaderBuilder.builder().terminal(terminal).completer(completerAdapter).build(); InputProvider inputProvider = new InteractiveShellApplicationRunner.JLineInputProvider(reader, promptProvider); THREAD_CONTEXT.set(new SshContext(terminal, ec, sshThread)); if (terminalDelegate instanceof SshShellTerminalDelegate) { ((SshShellTerminalDelegate) terminalDelegate).setDelegate(terminal); } shell.run(inputProvider); quit(0); } catch (IOException | RuntimeException e) { // log ex quit(1); } }