Skip to content

Commit b60cb41

Browse files
authored
Stop rsync directly printing to the stdout (#178)
1 parent a4c1c79 commit b60cb41

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

command/file.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,28 @@ package command
22

33
import (
44
"errors"
5+
"fmt"
56
"os"
7+
"os/exec"
68
"strings"
79

810
"github.com/bitrise-io/go-utils/pathutil"
911
)
1012

13+
func runCommand(name string, args ...string) error {
14+
cmd := exec.Command(name, args...)
15+
if out, err := cmd.CombinedOutput(); err != nil {
16+
printableCmd := PrintableCommandArgs(false, append([]string{name}, args...))
17+
18+
var exitErr *exec.ExitError
19+
if errors.As(err, &exitErr) {
20+
return fmt.Errorf("command failed with exit status %d (%s): %w", exitErr.ExitCode(), printableCmd, errors.New(string(out)))
21+
}
22+
return fmt.Errorf("executing command failed (%s): %w", printableCmd, err)
23+
}
24+
return nil
25+
}
26+
1127
// CopyFile ...
1228
func CopyFile(src, dst string) error {
1329
// replace with a pure Go implementation?
@@ -17,10 +33,10 @@ func CopyFile(src, dst string) error {
1733
return err
1834
}
1935
if isDir {
20-
return errors.New("Source is a directory: " + src)
36+
return errors.New("source is a directory: " + src)
2137
}
2238
args := []string{src, dst}
23-
return RunCommand("rsync", args...)
39+
return runCommand("rsync", args...)
2440
}
2541

2642
// CopyDir ...
@@ -29,7 +45,7 @@ func CopyDir(src, dst string, isOnlyContent bool) error {
2945
src = src + "/"
3046
}
3147
args := []string{"-ar", src, dst}
32-
return RunCommand("rsync", args...)
48+
return runCommand("rsync", args...)
3349
}
3450

3551
// RemoveDir ...

command/file_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ func TestCopyFileErrorIfDirectory(t *testing.T) {
1212
{
1313
tmpFolder, err := pathutil.NormalizedOSTempDirPath("_tmp")
1414
require.NoError(t, err)
15-
require.EqualError(t, CopyFile(tmpFolder, "./nothing/whatever"), "Source is a directory: "+tmpFolder)
15+
require.EqualError(t, CopyFile(tmpFolder, "./nothing/whatever"), "source is a directory: "+tmpFolder)
1616
}
1717
}

0 commit comments

Comments
 (0)