fix: validate config after PostLoad hooks
Re-run validator after PostLoad, return errors from RootDir, and add Config alias for C(). Hooks receive *T instead of *Manager. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
+33
-24
@@ -6,47 +6,56 @@ import (
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
// RootDir checks the current working directory and its parent directories for
|
||||
// a given filename and returns the absolute path to the directory. If the file
|
||||
// is not found within the specified depth or any other error occurs, it will
|
||||
// panic. If depth is zero or negative, RootDir checks the current directory only.
|
||||
func RootDir(searchName string, depth ...int) string {
|
||||
// Get the current working directory
|
||||
// RootDir walks from the working directory upward, looking for searchName as a
|
||||
// regular file. depth limits how many parent directories are checked (0 = CWD only).
|
||||
func RootDir(searchName string, depth ...int) (string, error) {
|
||||
cwd, err := os.Getwd()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
return "", err
|
||||
}
|
||||
|
||||
// Apply default depth if not provided
|
||||
depthVal := 0
|
||||
if len(depth) > 0 {
|
||||
depthVal = depth[0]
|
||||
}
|
||||
|
||||
// Walk directories up to the specified depth to find the file
|
||||
path := walkRootDir(searchName, cwd, depthVal)
|
||||
path, err := walkRootDir(searchName, cwd, depthVal)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if path == "" {
|
||||
panic(fmt.Errorf("RootDir checked %d directories, no '%s' file found", depthVal+1, searchName))
|
||||
return "", fmt.Errorf("RootDir checked %d directories, no '%s' file found", depthVal+1, searchName)
|
||||
}
|
||||
|
||||
// Try to get the absolute path of the found directory
|
||||
abs, err := filepath.Abs(path)
|
||||
if err != nil || abs == "" {
|
||||
panic(fmt.Errorf("RootDir failed to get absolute path: %v", err))
|
||||
}
|
||||
|
||||
return abs
|
||||
return filepath.Abs(path)
|
||||
}
|
||||
|
||||
// walkRootDir recursively checks directories up to the specified reverseDepth.
|
||||
func walkRootDir(searchName, path string, reverseDepth int) string {
|
||||
if _, err := os.Stat(filepath.Join(path, searchName)); err == nil {
|
||||
return path
|
||||
// MustRootDir is like [RootDir] but panics on error.
|
||||
func MustRootDir(searchName string, depth ...int) string {
|
||||
dir, err := RootDir(searchName, depth...)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return dir
|
||||
}
|
||||
|
||||
func walkRootDir(searchName, path string, reverseDepth int) (string, error) {
|
||||
candidate := filepath.Join(path, searchName)
|
||||
info, err := os.Stat(candidate)
|
||||
if err == nil {
|
||||
if info.IsDir() {
|
||||
return "", fmt.Errorf("RootDir found directory %q, expected a regular file", candidate)
|
||||
}
|
||||
return path, nil
|
||||
}
|
||||
|
||||
if reverseDepth > 0 {
|
||||
return walkRootDir(searchName, path+"/..", reverseDepth-1)
|
||||
parent := filepath.Dir(path)
|
||||
if parent == path {
|
||||
return "", nil
|
||||
}
|
||||
return walkRootDir(searchName, parent, reverseDepth-1)
|
||||
}
|
||||
|
||||
return ""
|
||||
return "", nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user