This repository has been archived by the owner on Apr 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
util.go
95 lines (80 loc) · 1.9 KB
/
util.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package tftest
import (
"os"
"path/filepath"
)
func symlinkFile(src string, dest string) (err error) {
err = os.Symlink(src, dest)
if err == nil {
srcInfo, err := os.Stat(src)
if err != nil {
err = os.Chmod(dest, srcInfo.Mode())
}
}
return
}
// symlinkDir is a simplistic function for recursively symlinking all files in a directory to a new path.
// It is intended only for limited internal use and does not cover all edge cases.
func symlinkDir(srcDir string, destDir string) (err error) {
srcInfo, err := os.Stat(srcDir)
if err != nil {
return err
}
err = os.MkdirAll(destDir, srcInfo.Mode())
if err != nil {
return err
}
directory, _ := os.Open(srcDir)
defer directory.Close()
objects, err := directory.Readdir(-1)
for _, obj := range objects {
srcPath := filepath.Join(srcDir, obj.Name())
destPath := filepath.Join(destDir, obj.Name())
if obj.IsDir() {
err = symlinkDir(srcPath, destPath)
if err != nil {
return err
}
} else {
err = symlinkFile(srcPath, destPath)
if err != nil {
return err
}
}
}
return
}
// symlinkDirectoriesOnly finds only the first-level child directories in srcDir
// and symlinks them into destDir.
// Unlike symlinkDir, this is done non-recursively in order to limit the number
// of file descriptors used.
func symlinkDirectoriesOnly(srcDir string, destDir string) (err error) {
srcInfo, err := os.Stat(srcDir)
if err != nil {
return err
}
err = os.MkdirAll(destDir, srcInfo.Mode())
if err != nil {
return err
}
directory, err := os.Open(srcDir)
if err != nil {
return err
}
defer directory.Close()
objects, err := directory.Readdir(-1)
if err != nil {
return err
}
for _, obj := range objects {
srcPath := filepath.Join(srcDir, obj.Name())
destPath := filepath.Join(destDir, obj.Name())
if obj.IsDir() {
err = symlinkFile(srcPath, destPath)
if err != nil {
return err
}
}
}
return
}