-
Notifications
You must be signed in to change notification settings - Fork 2k
/
copy.go
53 lines (43 loc) · 1007 Bytes
/
copy.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Copyright (c) The Thanos Authors.
// Licensed under the Apache License 2.0.
package e2eutil
import (
"io"
"os"
"path/filepath"
"testing"
"github.com/pkg/errors"
"github.com/thanos-io/thanos/pkg/testutil"
)
func Copy(t testing.TB, src, dst string) {
testutil.Ok(t, copyRecursive(src, dst))
}
func copyRecursive(src, dst string) error {
return filepath.Walk(src, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
relPath, err := filepath.Rel(src, path)
if err != nil {
return err
}
if info.IsDir() {
return os.MkdirAll(filepath.Join(dst, relPath), os.ModePerm)
}
if !info.Mode().IsRegular() {
return errors.Errorf("%s is not a regular file", path)
}
source, err := os.Open(path)
if err != nil {
return err
}
defer source.Close()
destination, err := os.Create(filepath.Join(dst, relPath))
if err != nil {
return err
}
defer destination.Close()
_, err = io.Copy(destination, source)
return err
})
}