Groovy流程输出得到削减 - 只打印第一行

时间:2018-02-14 11:07:19

标签: curl groovy

当我从命令行调用curl时:

AppIntentVocabulary.plist

我得到多行输出(-v选项):

curl -X POST -v -k -u admin:admin https://[my-host]

我试图用这段代码从groovy做同样的事情:

% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                  Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0* About to connect() to my-host 443 (#0)
 *   Trying xxx.xx.xx.xx...
 * Connected to my-host (xxx.xx.xx.xx) port 443 (#0)
 ...
 * Server auth using Basic with user '****'
  < HTTP/1.1 204 No Content

但是当我运行该代码时,我只得到一行输出:

  public void post() {
    def command = "curl -X POST -v -k -u admin:admin https://[my-host]"
    String result = executeOnShell(command, new File("."))
    println result
  }


  private String executeOnShell(String command, File workingDir) {
    def result = ""
    def process = new ProcessBuilder(addShellPrefix(command))
        .directory(workingDir)
        .redirectErrorStream(true)
        .start()
    process.inputStream.eachLine {
      // Trying to concat the output 
      result += it + "\n"

    }
    process.waitFor();
    return result
  }

  private def addShellPrefix(String command) {
    def commandArray = new String[3]
    commandArray[0] = "sh"
    commandArray[1] = "-c"
    commandArray[2] = command
    return commandArray
  }

如何捕获上面% Total % Received % Xferd Average Speed Time Time Time Current 变量中的所有输出/行?

0 个答案:

没有答案