Skip to content

Commit

Permalink
Merge pull request #326 from roots/more-flexible-project-detection
Browse files Browse the repository at this point in the history
Project detection: drop site folder requirement
  • Loading branch information
swalkinshaw authored Oct 6, 2022
2 parents 271cf69 + 281bc78 commit 561fdee
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 27 deletions.
41 changes: 18 additions & 23 deletions trellis/detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,46 +19,41 @@ 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
}
if len(configPaths) > 0 {
return path, true
}

parent := filepath.Dir(path)
trellisPath, ok := p.detectTrellisCLIProject(path)
if ok {
return trellisPath, true
}

if len(parent) == 1 && (parent == "." || os.IsPathSeparator(parent[0])) {
return "", false
}
parent := filepath.Dir(path)

return p.Detect(parent)
if len(parent) == 1 && (parent == "." || os.IsPathSeparator(parent[0])) {
return "", false
}

return path, true
return p.Detect(parent)
}

func (p *ProjectDetector) detectTrellisCLIProject(path string) bool {
trellisPath := filepath.Join(path, "trellis")
sitePath := filepath.Join(path, "site")
func (p *ProjectDetector) detectTrellisCLIProject(path string) (trellisPath string, ok bool) {
trellisPath = filepath.Join(path, "trellis")
configPath := filepath.Join(trellisPath, ConfigDir)

trellisDir, err := os.Stat(trellisPath)
if err != nil {
return false
return "", false
}

configDir, err := os.Stat(configPath)
if err != nil {
return false
}

siteDir, err := os.Stat(sitePath)
if err != nil {
return false
return "", false
}

if trellisDir.Mode().IsDir() && siteDir.Mode().IsDir() && configDir.Mode().IsDir() {
return true
if trellisDir.Mode().IsDir() && configDir.Mode().IsDir() {
return trellisPath, true
}

return false
return "", false
}
4 changes: 0 additions & 4 deletions trellis/detector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (
"testing"
)

const testDir = "tmp"

func TestDetect(t *testing.T) {
testDir := t.TempDir()

Expand Down Expand Up @@ -69,10 +67,8 @@ func TestDetectTrellisProjectStructure(t *testing.T) {

trellisDir := filepath.Join(testDir, "trellis")
siteDir := filepath.Join(testDir, "site")

os.Mkdir(trellisDir, 0700)
os.Mkdir(siteDir, 0700)

os.Mkdir(filepath.Join(trellisDir, ConfigDir), 0700)

devDir := filepath.Join(trellisDir, "group_vars", "development")
Expand Down

0 comments on commit 561fdee

Please sign in to comment.