Capture accumulated application, UI, CLI, regression tests and design documentation changes, including task project mapping and default CalDAV list naming. Keep deployment data, backups, local credentials and build artifacts out of Git. Move the Compose enrollment token to an ignored local environment file.
174 lines
2.7 KiB
Go
174 lines
2.7 KiB
Go
package store
|
|
|
|
import (
|
|
"path"
|
|
"sort"
|
|
"strings"
|
|
)
|
|
|
|
type TreeNode struct {
|
|
Name string
|
|
FullPath string
|
|
Children map[string]*TreeNode
|
|
IsDir bool
|
|
ChildCount int
|
|
}
|
|
|
|
type FlatTreeNode struct {
|
|
Name string
|
|
FullPath string
|
|
Depth int
|
|
IsDir bool
|
|
ChildCount int
|
|
}
|
|
|
|
func (s *Store) BuildPathTree() (*TreeNode, error) {
|
|
domainUUID, err := s.defaultDomainUUID()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return s.BuildPathTreeInDomain(domainUUID)
|
|
}
|
|
|
|
func (s *Store) BuildPathTreeInDomain(domainUUID string) (*TreeNode, error) {
|
|
return s.BuildPathTreeAtInDomain(domainUUID, "/")
|
|
}
|
|
|
|
func (s *Store) BuildPathTreeAtInDomain(domainUUID, rootPath string) (*TreeNode, error) {
|
|
rootPath = path.Clean(normalizeBrowserPath(rootPath))
|
|
prefix := strings.TrimRight(rootPath, "/") + "/"
|
|
rows, err := s.DB.Query(`
|
|
SELECT path
|
|
FROM paths
|
|
WHERE domain_uuid = ?
|
|
AND path >= ? AND path < ?
|
|
AND path != '/u'
|
|
AND path NOT LIKE '/u/%'
|
|
ORDER BY path ASC
|
|
`, domainUUID, prefix, strings.TrimSuffix(prefix, "/")+"0")
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
root := &TreeNode{
|
|
Name: path.Base(rootPath),
|
|
FullPath: rootPath,
|
|
Children: map[string]*TreeNode{},
|
|
IsDir: true,
|
|
}
|
|
|
|
for rows.Next() {
|
|
|
|
var path string
|
|
|
|
err := rows.Scan(
|
|
&path,
|
|
)
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if path == rootPath {
|
|
continue
|
|
}
|
|
parts :=
|
|
strings.Split(
|
|
strings.TrimPrefix(
|
|
path,
|
|
prefix,
|
|
),
|
|
"/",
|
|
)
|
|
|
|
current := root
|
|
var pathBuilder strings.Builder
|
|
pathBuilder.WriteString(strings.TrimRight(rootPath, "/"))
|
|
|
|
for i, part := range parts {
|
|
|
|
pathBuilder.WriteString("/")
|
|
pathBuilder.WriteString(part)
|
|
currentPath := pathBuilder.String()
|
|
|
|
child, ok :=
|
|
current.Children[part]
|
|
|
|
if !ok {
|
|
|
|
child = &TreeNode{
|
|
Name: part,
|
|
FullPath: currentPath,
|
|
Children: map[string]*TreeNode{},
|
|
}
|
|
|
|
current.Children[part] =
|
|
child
|
|
}
|
|
|
|
if i != len(parts)-1 {
|
|
child.IsDir = true
|
|
}
|
|
|
|
current = child
|
|
}
|
|
}
|
|
|
|
var walk func(*TreeNode)
|
|
|
|
walk = func(n *TreeNode) {
|
|
|
|
n.ChildCount =
|
|
len(n.Children)
|
|
|
|
for _, c := range n.Children {
|
|
walk(c)
|
|
}
|
|
}
|
|
|
|
walk(root)
|
|
|
|
return root, rows.Err()
|
|
}
|
|
|
|
func FlattenTree(
|
|
node *TreeNode,
|
|
depth int,
|
|
out *[]FlatTreeNode,
|
|
) {
|
|
|
|
if node.FullPath != "/" {
|
|
|
|
*out = append(
|
|
*out,
|
|
FlatTreeNode{
|
|
Name: node.Name,
|
|
FullPath: node.FullPath,
|
|
Depth: depth,
|
|
IsDir: node.IsDir,
|
|
ChildCount: node.ChildCount,
|
|
},
|
|
)
|
|
}
|
|
|
|
keys := make([]string, 0)
|
|
|
|
for k := range node.Children {
|
|
keys = append(keys, k)
|
|
}
|
|
|
|
sort.Strings(keys)
|
|
|
|
for _, k := range keys {
|
|
|
|
FlattenTree(
|
|
node.Children[k],
|
|
depth+1,
|
|
out,
|
|
)
|
|
}
|
|
}
|