From 281bc78a543d4397909b7f8b955f3b389cae83b4 Mon Sep 17 00:00:00 2001 From: Scott Walkinshaw Date: Tue, 4 Oct 2022 19:38:55 -0400 Subject: [PATCH] Project detection: drop site folder requirement MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This makes the project detector more flexible and less strict by dropping the `site` folder requirement. Previously the detector would only recognize the recommended folder structure when not directly inside a `trellis` folder. Your project would have to be structured like this: example.com/ # → Root folder for the project ├── trellis/ # → Your server configuration (a customized install of Trellis) └── .trellis/ # → trellis-cli config directory └── site/ # → A Bedrock-based WordPress site └── web/ `site` was required to limit false positives but isn't necessary due to the other requirement for the `.trellis` config directory. By dropping the `site requirement`, this means trellis-cli will work better in two other known folder structures: * where the "site" folder exists but it named differently some_project_name/ # → Root folder for the project ├── trellis/ # → Your server configuration (a customized install of Trellis) └── .trellis/ # → trellis-cli config directory └── example.com/ # → A Bedrock-based WordPress site └── web/ * where the root _is_ the site contents, and trellis is a subdirectory example.com/ # → A Bedrock-based WordPress site └── web/ ├── trellis/ # → Your server configuration (a customized install of Trellis) └── .trellis/ # → trellis-cli config directory --- trellis/detector.go | 41 ++++++++++++++++++---------------------- trellis/detector_test.go | 4 ---- 2 files changed, 18 insertions(+), 27 deletions(-) diff --git a/trellis/detector.go b/trellis/detector.go index 2abaeb06..1208a0f6 100644 --- a/trellis/detector.go +++ b/trellis/detector.go @@ -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 } diff --git a/trellis/detector_test.go b/trellis/detector_test.go index ab722b39..d780a277 100644 --- a/trellis/detector_test.go +++ b/trellis/detector_test.go @@ -6,8 +6,6 @@ import ( "testing" ) -const testDir = "tmp" - func TestDetect(t *testing.T) { testDir := t.TempDir() @@ -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")