/
detector.go
64 lines (49 loc) · 1.29 KB
/
detector.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
package trellis
import (
"os"
"path/filepath"
)
type Detector interface {
Detect(path string) (projectPath string, ok bool)
}
type ProjectDetector struct{}
/*
Detect if a path is a Trellis project or not
This will traverse up the directory tree until it finds a valid project,
or stop at the root and give up.
*/
func (p *ProjectDetector) Detect(path string) (projectPath string, ok bool) {
configPaths, _ := filepath.Glob(filepath.Join(path, GlobPattern))
if len(configPaths) == 0 {
if p.detectTrellisCLIProject(path) {
return filepath.Join(path, "trellis"), true
}
parent := filepath.Dir(path)
if len(parent) == 1 && (parent == "." || os.IsPathSeparator(parent[0])) {
return "", false
}
return p.Detect(parent)
}
return path, true
}
func (p *ProjectDetector) detectTrellisCLIProject(path string) bool {
trellisPath := filepath.Join(path, "trellis")
sitePath := filepath.Join(path, "site")
configPath := filepath.Join(trellisPath, ConfigDir)
trellisDir, err := os.Stat(trellisPath)
if err != nil {
return false
}
configDir, err := os.Stat(configPath)
if err != nil {
return false
}
siteDir, err := os.Stat(sitePath)
if err != nil {
return false
}
if trellisDir.Mode().IsDir() && siteDir.Mode().IsDir() && configDir.Mode().IsDir() {
return true
}
return false
}