| 1 | /* |
| 2 | * Copyright (C) 2015, EfficiOS Inc., Alexandre Montplaisir <alexmonthy@efficios.com> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License along |
| 15 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 16 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 17 | */ |
| 18 | |
| 19 | package org.lttng.tools.utils; |
| 20 | |
| 21 | import java.io.IOException; |
| 22 | import java.lang.ProcessBuilder.Redirect; |
| 23 | import java.nio.file.Files; |
| 24 | import java.nio.file.Path; |
| 25 | import java.util.Arrays; |
| 26 | import java.util.List; |
| 27 | import java.util.StringJoiner; |
| 28 | |
| 29 | /** |
| 30 | * Utility methods to execute commands on the command line. |
| 31 | * |
| 32 | * @author Alexandre Montplaisir |
| 33 | */ |
| 34 | public final class ShellUtils { |
| 35 | |
| 36 | private ShellUtils() {} |
| 37 | |
| 38 | /** |
| 39 | * Simple command to test that the environment / stdout are working |
| 40 | * correctly. |
| 41 | * |
| 42 | * @param args |
| 43 | * Command-line arguments |
| 44 | */ |
| 45 | public static void main(String[] args) { |
| 46 | List<String> command = Arrays.asList("ls", "-l"); |
| 47 | executeCommand(command); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Execute a shell command and retrieve its return value. |
| 52 | * |
| 53 | * @param command |
| 54 | * The command to execute, as a list of individual arguments (do |
| 55 | * not use spaces) |
| 56 | * @return If the command returned successfully (ret code = 0) |
| 57 | */ |
| 58 | public static boolean executeCommand(List<String> command) { |
| 59 | try { |
| 60 | /* "echo" the command to stdout */ |
| 61 | StringJoiner sj = new StringJoiner(" ", "$ ", ""); |
| 62 | command.stream().forEach(sj::add); |
| 63 | System.out.println(sj.toString()); |
| 64 | |
| 65 | ProcessBuilder builder = new ProcessBuilder(command); |
| 66 | builder.redirectErrorStream(true); |
| 67 | builder.redirectOutput(Redirect.INHERIT); |
| 68 | |
| 69 | Process p = builder.start(); |
| 70 | int ret = p.waitFor(); |
| 71 | |
| 72 | System.out.println(String.format("(returned from command: %d)", Integer.valueOf(ret))); |
| 73 | |
| 74 | return (ret == 0); |
| 75 | |
| 76 | } catch (IOException | InterruptedException e) { |
| 77 | return false; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Execute a shell command and retrieve its output. |
| 83 | * |
| 84 | * @param print |
| 85 | * Should the output also be printed to stdout as usual |
| 86 | * @param command |
| 87 | * The command to execute, as a list of individual arguments (do |
| 88 | * not use spaces) |
| 89 | * @return The output of the command, as one list element per line |
| 90 | */ |
| 91 | public static List<String> getOutputFromCommand(boolean print, List<String> command) { |
| 92 | try { |
| 93 | /* "echo" the command to stdout */ |
| 94 | StringJoiner sj = new StringJoiner(" ", "$ ", ""); |
| 95 | command.stream().forEach(sj::add); |
| 96 | System.out.println(sj.toString()); |
| 97 | |
| 98 | Path tempFile = Files.createTempFile("test-output", null); |
| 99 | |
| 100 | ProcessBuilder builder = new ProcessBuilder(command); |
| 101 | builder.redirectErrorStream(true); |
| 102 | builder.redirectOutput(Redirect.to(tempFile.toFile())); |
| 103 | |
| 104 | Process p = builder.start(); |
| 105 | p.waitFor(); |
| 106 | |
| 107 | List<String> lines = Files.readAllLines(tempFile); |
| 108 | Files.delete(tempFile); |
| 109 | |
| 110 | if (print) { |
| 111 | /* Also print the output to the console */ |
| 112 | lines.stream().forEach(System.out::println); |
| 113 | } else { |
| 114 | System.out.println("(output silenced)"); |
| 115 | } |
| 116 | |
| 117 | System.out.println("(returned from command)"); |
| 118 | return lines; |
| 119 | |
| 120 | } catch (IOException | InterruptedException e) { |
| 121 | return null; |
| 122 | } |
| 123 | } |
| 124 | } |