# Minimal Configuration Loader # Fast-path config loading for help commands and basic operations # Contains ONLY essential path detection and workspace identification (~150 lines) # Detect current environment from ENV, workspace name, or default export def detect-current-environment [] { # Check explicit environment variable if ($env.PROVISIONING_ENVIRONMENT? | is-not-empty) { return $env.PROVISIONING_ENVIRONMENT } # Check if workspace name contains environment hints let active_ws = (get-active-workspace) if ($active_ws | is-not-empty) { let ws_name = $active_ws.name if ($ws_name | str contains "prod") { return "prod" } if ($ws_name | str contains "staging") { return "staging" } if ($ws_name | str contains "test") { return "test" } if ($ws_name | str contains "dev") { return "dev" } } # Check PWD for environment hints if ($env.PWD | str contains "prod") { return "prod" } if ($env.PWD | str contains "staging") { return "staging" } if ($env.PWD | str contains "test") { return "test" } if ($env.PWD | str contains "dev") { return "dev" } # Default environment "dev" } # Get the currently active workspace (from central user config) export def get-active-workspace [] { let user_config_dir = ([$env.HOME "Library" "Application Support" "provisioning"] | path join) if not ($user_config_dir | path exists) { return null } # Load central user config let user_config_path = ($user_config_dir | path join "user_config.yaml") if not ($user_config_path | path exists) { return null } let user_config = (open $user_config_path) # Check if active workspace is set if ($user_config.active_workspace == null) { null } else { # Find workspace in list let workspace_name = $user_config.active_workspace let workspace = ($user_config.workspaces | where name == $workspace_name | first) if ($workspace | is-empty) { null } else { { name: $workspace.name path: $workspace.path } } } } # Find project root by looking for kcl.mod or core/nulib directory export def get-project-root [] { let potential_roots = [ $env.PWD ($env.PWD | path dirname) ($env.PWD | path dirname | path dirname) ($env.PWD | path dirname | path dirname | path dirname) ] let matching_roots = ($potential_roots | where ($it | path join "kcl.mod" | path exists) or ($it | path join "core" "nulib" | path exists)) if ($matching_roots | length) > 0 { $matching_roots | first } else { $env.PWD } } # Get system defaults configuration path export def get-defaults-config-path [] { let base_path = if ($env.PROVISIONING? | is-not-empty) { $env.PROVISIONING } else { "/usr/local/provisioning" } ($base_path | path join "provisioning" "config" "config.defaults.toml") } # Check if a file is encrypted with SOPS export def check-if-sops-encrypted [file_path: string]: nothing -> bool { let file_exists = ($file_path | path exists) if not $file_exists { return false } # Read first few bytes to check for SOPS marker let content = (^bash -c $"head -c 100 \"($file_path)\"") # SOPS encrypted files contain "sops" key in the header ($content | str contains "sops") } # Get SOPS configuration path if it exists export def find-sops-config-path [] { let possible_paths = [ ($env.HOME | path join ".sops.yaml") ($env.PWD | path join ".sops.yaml") ($env.PWD | path join "sops" ".sops.yaml") ($env.PWD | path join ".decrypted" ".sops.yaml") ] let existing_paths = ($possible_paths | where ($it | path exists)) if ($existing_paths | length) > 0 { $existing_paths | first } else { null } } # Update workspace last-used timestamp (non-critical, safe to fail silently) export def update-workspace-last-used [workspace_name: string] { let user_config_dir = ([$env.HOME "Library" "Application Support" "provisioning"] | path join) if not ($user_config_dir | path exists) { return } let user_config_path = ($user_config_dir | path join "user_config.yaml") if not ($user_config_path | path exists) { return } # Safe fallback - if any part fails, silently continue # This is not critical path }