-
Notifications
You must be signed in to change notification settings - Fork 2
/
directory_visitor.go
111 lines (101 loc) · 2.59 KB
/
directory_visitor.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package main
import (
"fmt"
"os"
"path"
"path/filepath"
"strings"
)
type DirectoryVisitor struct {
NodeVisitor
rootDir string
flatten bool
maxDepth int
currentLevel int
currentLevelDir string
previousLevel int
previousLevelDir string
indentLevel int
pathParts []string
}
func NewDirectoryVisitor(root string, flatten bool, maxDepth int) *DirectoryVisitor {
return &DirectoryVisitor{
rootDir: root,
flatten: flatten,
pathParts: []string{root, "", "", ""},
maxDepth: maxDepth,
}
}
func (v *DirectoryVisitor) Visit(n *Node, depth int) {
v.currentLevel = depth
v.currentLevelDir = v.rootDir
if v.currentLevel == 0 {
v.indentLevel = 0
v.previousLevel = 0
v.pathParts[1] = ""
v.pathParts[2] = ""
v.pathParts[3] = ""
return
}
v.pathParts[depth-1] = FileNameByDepth(n.FileName, depth)
// We're probably at a month
if depth == 3 && !n.HasNext() {
v.pathParts[depth] = ""
}
dirs := []string{outputDirectory}
dirs = append(dirs, v.pathParts[:v.maxDepth]...)
if flatten && n.HasChildren() {
return
}
var dest string
source := path.Join(v.rootDir, n.FileName)
sfi, err := os.Stat(source)
if os.IsNotExist(err) {
// some internal nodes in our tree won't exist
return
}
destParts := []string{outputDirectory}
if copyOnly && sfi.IsDir() {
destParts = []string{}
}
if flatten {
dirs = []string{v.rootDir, strings.Join(v.pathParts[:v.maxDepth], "-")}
flattenedParent := strings.Join(v.pathParts[:v.maxDepth], "-")
destParts = append(destParts, flattenedParent)
destParts = append(destParts, n.FileName)
} else {
destParts = append(destParts, v.pathParts...)
}
dest = path.Join(destParts...)
// Create the destination directories
perm := os.FileMode(0755)
rootStat, _ := os.Stat(directory)
// use permissions of the root directory
perm = rootStat.Mode()
err = os.MkdirAll(path.Join(dirs...), perm)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to create directory %s \n", dirs)
os.Exit(1)
return
}
if copyOnly && sfi.IsDir() {
odabs, _ := filepath.Abs(outputDirectory)
dest = path.Join(odabs, dest)
source, _ = filepath.Abs(source)
// fmt.Fprintf(os.Stderr, "Creating sylink for directory ln -s %s %s \n", source, dest)
err = os.Symlink(source, dest)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to create symlink from=%s to =%s", source, dest)
os.Exit(1)
return
}
return
}
// Move the file from the source to the directory
err = moveOrCopyFile(source, dest)
if err != nil {
fmt.Fprintf(os.Stderr, "Error while moving/copying file to %s", dest)
return
}
v.previousLevel = depth
}