diff --git a/cli/cmd/provision/config/config.go b/cli/cmd/provision/config/config.go index 046c401..f973bb2 100644 --- a/cli/cmd/provision/config/config.go +++ b/cli/cmd/provision/config/config.go @@ -22,6 +22,7 @@ func GetRepoRoot() (string, error) { } // findRepoRoot attempts to find the repository root by looking for marker files/directories +// Update this function in cli/cmd/provision/config/config.go func findRepoRoot() (string, error) { // Start from the current working directory cwd, err := os.Getwd() @@ -32,34 +33,53 @@ func findRepoRoot() (string, error) { // Walk up the directory tree until we find a marker of the repository root currentDir := cwd for { - // Check for common repository root markers: + // Check for multiple repository root markers in a specific order - // 1. Check if ansible directory exists + // First check for the most reliable markers ansibleDir := filepath.Join(currentDir, "ansible") - if dirExists(ansibleDir) { + ansibleDefaultsDir := filepath.Join(currentDir, "ansible", "defaults") + scriptsDir := filepath.Join(currentDir, "scripts") + + // Add more specific file checks + kubernetesYml := filepath.Join(currentDir, "ansible", "defaults", "kubernetes.yml") + provisionScript := filepath.Join(currentDir, "scripts", "provision-kubernetes.sh") + + // Check all variations + if dirExists(ansibleDir) && dirExists(ansibleDefaultsDir) { return currentDir, nil } - // 2. Check for scripts directory with our kubernetes scripts - scriptsDir := filepath.Join(currentDir, "scripts", "provision-kubernetes.sh") - if fileExists(scriptsDir) { + if dirExists(scriptsDir) && fileExists(provisionScript) { return currentDir, nil } - // 3. Check for .git directory as a fallback + if fileExists(kubernetesYml) { + return currentDir, nil + } + + // Check for .git directory as a fallback gitDir := filepath.Join(currentDir, ".git") if dirExists(gitDir) { - return currentDir, nil + // Additional validation when using .git as marker + // Make sure this is actually our repository by checking for a known file + if fileExists(filepath.Join(currentDir, "README.md")) || + dirExists(filepath.Join(currentDir, "ansible")) || + dirExists(filepath.Join(currentDir, "scripts")) { + return currentDir, nil + } } // Move up one directory parentDir := filepath.Dir(currentDir) if parentDir == currentDir { // We've reached the filesystem root without finding the repo root - return "", fmt.Errorf("repository root not found: no repository markers found in parent directories") + break } currentDir = parentDir } + + // More helpful error message + return "", fmt.Errorf("repository root not found: the CLI needs to be run within the kubernetes repository that contains ansible/defaults/kubernetes.yml file. Please verify your directory structure or run 'git clone' to ensure you have the complete repository") } // dirExists checks if a directory exists diff --git a/cli/cmd/provision/kubernetes/provision.go b/cli/cmd/provision/kubernetes/provision.go index a0c00b8..3b78f40 100644 --- a/cli/cmd/provision/kubernetes/provision.go +++ b/cli/cmd/provision/kubernetes/provision.go @@ -33,12 +33,14 @@ type Config struct { func LoadDefaultConfig() (*Config, error) { defaultsPath, err := config.GetAnsiblePath("defaults/kubernetes.yml") if err != nil { - return nil, fmt.Errorf("failed to locate defaults file: %w", err) + return nil, fmt.Errorf("failed to locate defaults file: %w\n"+ + "Please make sure you are running the command from within the repository and that the ansible/defaults/kubernetes.yml file exists", err) } data, err := os.ReadFile(defaultsPath) if err != nil { - return nil, fmt.Errorf("failed to read defaults file: %w", err) + return nil, fmt.Errorf("failed to read defaults file at %s: %w\n"+ + "Please check file permissions and that the repository is correctly cloned", defaultsPath, err) } config := &Config{} @@ -256,4 +258,3 @@ func Cleanup() error { return cmd.Run() } -