forked from containerd/continuity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testutil_test.go
53 lines (50 loc) · 986 Bytes
/
testutil_test.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
package continuity
import (
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
)
func tree(w io.Writer, dir string) error {
fmt.Fprintf(w, "%s\n", dir)
return _tree(w, dir, "")
}
func _tree(w io.Writer, dir string, indent string) error {
files, err := ioutil.ReadDir(dir)
if err != nil {
return err
}
for i, f := range files {
fPath := filepath.Join(dir, f.Name())
fIndent := indent
if i < len(files)-1 {
fIndent += "|-- "
} else {
fIndent += "`-- "
}
target := ""
if f.Mode()&os.ModeSymlink == os.ModeSymlink {
realPath, err := os.Readlink(fPath)
if err != nil {
target += fmt.Sprintf(" -> unknown (error: %v)", err)
} else {
target += " -> " + realPath
}
}
fmt.Fprintf(w, "%s%s%s\n",
fIndent, f.Name(), target)
if f.IsDir() {
dIndent := indent
if i < len(files)-1 {
dIndent += "| "
} else {
dIndent += " "
}
if err := _tree(w, fPath, dIndent); err != nil {
return err
}
}
}
return nil
}