provisioning-core/nulib/platform/workspace/detection.nu

86 lines
2.6 KiB
Text
Raw Normal View History

use platform/user/config.nu [get-active-workspace]
use platform/workspace/notation.nu [get-workspace-path list-workspaces]
export def infer-workspace-from-pwd [] {
let pwd = $env.PWD
let workspaces = (list-workspaces)
for ws in $workspaces {
if ($pwd | str starts-with $ws.path) {
return $ws.name
}
}
null
}
export def get-effective-workspace [] {
if ($env.TEMP_WORKSPACE? | is-not-empty) { return $env.TEMP_WORKSPACE }
let active = (get-active-workspace)
if ($active | is-not-empty) { return $active }
let inferred = (infer-workspace-from-pwd)
if ($inferred | is-not-empty) {
print $"⚠️ No active workspace, inferred from PWD: ($inferred)"
print "💡 Activate permanently: provisioning workspace activate <name>"
return $inferred
}
error make {msg: "No workspace active or inferable from PWD. Run: provisioning workspace activate <name>"}
}
export def detect-infra-from-pwd [] {
let pwd = $env.PWD
let settings_file = ([$pwd "settings.ncl"] | path join)
if ($settings_file | path exists) { return ($pwd | path basename) }
let workspace = (infer-workspace-from-pwd)
if ($workspace | is-empty) { return null }
let workspace_path = (get-workspace-path $workspace)
let infra_base = ([$workspace_path "infra"] | path join)
if ($pwd | str starts-with $infra_base) {
let relative = ($pwd | str replace $infra_base "")
let parts = ($relative | split row "/" | where {|x| ($x | is-not-empty)})
if ($parts | length) > 0 { return ($parts | get 0) }
}
null
}
export def get-effective-infra [workspace: string] {
let pwd_infra = (detect-infra-from-pwd)
if ($pwd_infra | is-not-empty) { return $pwd_infra }
if ($env.PROVISIONING_INFRA? | is-not-empty) { return $env.PROVISIONING_INFRA }
null
}
export def is-workspace-active [workspace_name: string] {
let active = (get-active-workspace)
$active == $workspace_name
}
export def is-in-workspace [workspace_name: string] {
let pwd = $env.PWD
let workspace_path = (get-workspace-path $workspace_name)
$pwd | str starts-with $workspace_path
}
export def is-in-infra [workspace_name: string, infra_name: string] {
let pwd = $env.PWD
let workspace_path = (get-workspace-path $workspace_name)
let infra_path = ([$workspace_path "infra" $infra_name] | path join)
$pwd | str starts-with $infra_path
}
export def get-inferred-context [] {
let workspace = (infer-workspace-from-pwd)
let infra = if ($workspace | is-not-empty) { detect-infra-from-pwd } else { null }
{workspace: $workspace, infra: $infra}
}