// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.node.admin.maintenance.servicedump;

import com.yahoo.vespa.hosted.node.admin.container.ContainerOperations;
import com.yahoo.vespa.hosted.node.admin.nodeagent.NodeAgentContext;
import com.yahoo.vespa.hosted.node.admin.task.util.process.CommandResult;

import java.io.IOException;
import java.util.List;
import java.util.logging.Logger;
import java.util.stream.Collectors;

/**
 * @author bjorncs
 */
abstract class AbstractProducer implements ArtifactProducer {

    private final Logger log = Logger.getLogger(getClass().getName());

    private final ContainerOperations container;

    protected AbstractProducer(ContainerOperations container) { this.container = container; }

    protected ContainerOperations container() { return container; }

    protected CommandResult executeCommand(NodeAgentContext ctx, List<String> command, boolean logOutput) throws IOException {
        CommandResult result = container.executeCommandInContainerAsRoot(ctx, command.toArray(new String[0]));
        String cmdString = command.stream().map(s -> "'" + s + "'").collect(Collectors.joining(" ", "\"", "\""));
        int exitCode = result.getExitCode();
        String output = result.getOutput().trim();
        String prefixedOutput = output.contains("\n")
                ? "\n" + output
                : (output.isEmpty() ? "<no output>" : output);
        String logMsg = logOutput
                ? String.format("Executed command %s.\nExited with code %d and output: %s", cmdString, exitCode, prefixedOutput)
                : String.format("Executed command %s.\nExited with code %d.", cmdString, exitCode);
        ctx.log(log, logMsg);
        if (exitCode > 0) {
            String errorMsg = logOutput
                    ? String.format("Failed to execute %s: %s", cmdString, prefixedOutput)
                    : String.format("Failed to execute %s (exited with code %d)", cmdString, exitCode);
            throw new IOException(errorMsg);
        }
        return result;
    }


}

@@ -1476,16 +1476,48 @@
 utput);%0A
+        if (exitCode %3E 0) %7B%0A    
         
@@ -1523,19 +1523,21 @@
  String 
-log
+error
 Msg = lo
@@ -1536,32 +1536,36 @@
 Msg = logOutput%0A
+    
                 
@@ -1581,39 +1581,39 @@
 format(%22
-Executed command %25s.%5CnE
+Failed to execute %25s (e
 xited wi
@@ -1626,19 +1626,9 @@
 e %25d
- and output
+)
 : %25s
@@ -1675,32 +1675,36 @@
                 
+    
 : String.format(
@@ -1708,31 +1708,31 @@
 at(%22
-Executed command %25s.%5CnE
+Failed to execute %25s (e
 xite
@@ -1745,17 +1745,17 @@
  code %25d
-.
+)
 %22, cmdSt
@@ -1782,56 +1782,60 @@
     
+     
- ctx.log(log, logMsg);%0A        if (exitCode %3E 0)
+throw new IOException(errorMsg);%0A        %7D else
  %7B%0A 
@@ -1844,37 +1844,35 @@
          String 
-error
+log
 Msg = logOutput%0A
@@ -1908,36 +1908,67 @@
 format(%22
-Failed to execute %25s
+Executed command %25s. Exited with code %25d and output
 : %25s%22, c
@@ -1976,16 +1976,26 @@
 dString,
+ exitCode,
  prefixe
@@ -2040,39 +2040,38 @@
 format(%22
-Failed to execute %25s (e
+Executed command %25s. E
 xited wi
@@ -2072,33 +2072,33 @@
 ted with code %25d
-)
+.
 %22, cmdString, ex
@@ -2122,35 +2122,24 @@
     
-throw new IOException(error
+ctx.log(log, log
 Msg)

Improve logging of executed command