-
Notifications
You must be signed in to change notification settings - Fork 88
/
filetree.go
137 lines (112 loc) · 2.99 KB
/
filetree.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package supportbundle
import (
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/mholt/archiver"
"github.com/pkg/errors"
"github.com/replicatedhq/kots/pkg/supportbundle/types"
)
var (
SupportBundleNameRegex = regexp.MustCompile(`^\/?support-bundle-(\d{4})-(\d{2})-(\d{2})T(\d{2})_(\d{2})_(\d{2})\/?`)
)
func archiveToFileTree(archivePath string) (*types.FileTree, error) {
workDir, err := ioutil.TempDir("", "kotsadm")
if err != nil {
return nil, errors.Wrap(err, "failed to create workdir")
}
defer os.RemoveAll(workDir)
// extract the current archive to this root
tarGz := &archiver.TarGz{
Tar: &archiver.Tar{
ImplicitTopLevelFolder: false,
},
}
if err := tarGz.Unarchive(archivePath, workDir); err != nil {
return nil, errors.Wrap(err, "failed to unarchive")
}
directories := []string{}
files := []string{}
err = filepath.Walk(workDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if len(path) <= len(workDir) {
return nil
}
relPath, err := filepath.Rel(workDir, path)
if err != nil {
return errors.Wrap(err, "failed to get relative path")
}
// don't include the top level subdirectory in the file tree
trimmedRelPath := SupportBundleNameRegex.ReplaceAllString(relPath, "")
if trimmedRelPath == "" {
return nil
}
if info.IsDir() {
directories = append(directories, trimmedRelPath)
return nil
}
files = append(files, trimmedRelPath)
return nil
})
if err != nil {
return nil, errors.Wrap(err, "failed to walk")
}
ft := types.FileTree{
Nodes: []types.FileTreeNode{},
}
for _, directory := range directories {
if !strings.Contains(directory, string(os.PathSeparator)) {
node, err := getDirectoryNode(directory, directories, files)
if err != nil {
return nil, errors.Wrap(err, "failed to get directory node")
}
ft.Nodes = append(ft.Nodes, *node)
}
}
for _, file := range files {
if !strings.Contains(file, string(os.PathSeparator)) {
node := types.FileTreeNode{
Name: file,
Path: file,
}
ft.Nodes = append(ft.Nodes, node)
}
}
return &ft, nil
}
func getDirectoryNode(directory string, directories []string, files []string) (*types.FileTreeNode, error) {
_, f := filepath.Split(directory)
node := types.FileTreeNode{
Name: f,
Path: directory,
}
children := []types.FileTreeNode{}
for _, file := range files {
d, f := filepath.Split(file)
if strings.TrimSuffix(d, string(os.PathSeparator)) == directory {
child := types.FileTreeNode{
Name: f,
Path: file,
}
children = append(children, child)
}
}
for _, d := range directories {
parent, _ := filepath.Split(d)
if strings.TrimSuffix(parent, string(os.PathSeparator)) == directory {
child, err := getDirectoryNode(d, directories, files)
if err != nil {
return nil, errors.Wrap(err, "failed to recursively call getDirectoryNode")
}
children = append(children, *child)
}
}
if len(children) > 0 {
node.Children = children
}
return &node, nil
}