github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/state/backups/exec.go (about)

     1  // Copyright 2014 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package backups
     5  
     6  import (
     7  	"os/exec"
     8  	"strings"
     9  
    10  	"github.com/juju/errors"
    11  )
    12  
    13  // runCommand execs the provided command. It exists
    14  // here so it can be overridden in export_test.go
    15  func runCommand(cmd string, args ...string) error {
    16  	logger.Tracef("runCommand cmd=%s, args=%s", cmd, args)
    17  	command := exec.Command(cmd, args...)
    18  	out, err := command.CombinedOutput()
    19  	if err == nil {
    20  		logger.Tracef("runCommand succeeded, output is:\n%s", out)
    21  		return nil
    22  	}
    23  	logger.Tracef("runCommand error %v; output is:\n%s", out)
    24  	if _, ok := err.(*exec.ExitError); ok && len(out) > 0 {
    25  		return errors.Errorf(
    26  			"error executing %q: %s",
    27  			cmd,
    28  			strings.Replace(string(out), "\n", "; ", -1),
    29  		)
    30  	}
    31  	return errors.Annotatef(err, "error executing %q", cmd)
    32  }