feat(cli): discover ThothII projects and installations
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
// Package project finds a ThothII source checkout from a directory within it.
|
||||
package project
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
// Root identifies one ThothII checkout. Path is resolved for stable identity across symlinks.
|
||||
type Root struct {
|
||||
Path string
|
||||
IsWorktree bool
|
||||
}
|
||||
|
||||
// Discover walks from start towards the filesystem root until it finds the ThothII source-layout
|
||||
// contract. A regular .git file identifies a linked Git worktree; a .git directory identifies a
|
||||
// main checkout.
|
||||
func Discover(start string) (Root, error) {
|
||||
if start == "" {
|
||||
return Root{}, errors.New("ThothII project root could not be discovered from an empty directory")
|
||||
}
|
||||
abs, err := filepath.Abs(start)
|
||||
if err != nil {
|
||||
return Root{}, errors.New("ThothII project root could not resolve the current directory")
|
||||
}
|
||||
resolved, err := filepath.EvalSymlinks(abs)
|
||||
if err != nil {
|
||||
return Root{}, fmt.Errorf("ThothII project root could not resolve %s", filepath.Clean(abs))
|
||||
}
|
||||
info, err := os.Stat(resolved)
|
||||
if err != nil || !info.IsDir() {
|
||||
return Root{}, fmt.Errorf("ThothII project root could not read %s", filepath.Clean(abs))
|
||||
}
|
||||
|
||||
for directory := filepath.Clean(resolved); ; directory = filepath.Dir(directory) {
|
||||
if isRoot(directory) {
|
||||
return Root{Path: directory, IsWorktree: isLinkedWorktree(directory)}, nil
|
||||
}
|
||||
parent := filepath.Dir(directory)
|
||||
if parent == directory {
|
||||
break
|
||||
}
|
||||
}
|
||||
return Root{}, fmt.Errorf("ThothII project root not found from %s", filepath.Clean(abs))
|
||||
}
|
||||
|
||||
func isRoot(directory string) bool {
|
||||
if !isGitMarker(filepath.Join(directory, ".git")) || !isRegularFile(filepath.Join(directory, "compose.yaml")) || !isDirectory(filepath.Join(directory, "deploy")) {
|
||||
return false
|
||||
}
|
||||
for _, name := range []string{"backend", "frontend", "harness", "tools"} {
|
||||
if !isDirectory(filepath.Join(directory, name)) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func isLinkedWorktree(directory string) bool {
|
||||
info, err := os.Lstat(filepath.Join(directory, ".git"))
|
||||
return err == nil && info.Mode().IsRegular()
|
||||
}
|
||||
|
||||
func isGitMarker(path string) bool {
|
||||
info, err := os.Lstat(path)
|
||||
return err == nil && (info.IsDir() || info.Mode().IsRegular())
|
||||
}
|
||||
|
||||
func isRegularFile(path string) bool {
|
||||
info, err := os.Stat(path)
|
||||
return err == nil && info.Mode().IsRegular()
|
||||
}
|
||||
|
||||
func isDirectory(path string) bool {
|
||||
info, err := os.Stat(path)
|
||||
return err == nil && info.IsDir()
|
||||
}
|
||||
Reference in New Issue
Block a user