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

85 lines
2.3 KiB
Text
Raw Permalink Normal View History

use platform/user/config.nu [load-user-config]
export def parse-workspace-infra-notation [spec: string] {
if ($spec | str contains ":") {
let parts = ($spec | split row ":")
let workspace_part = ($parts | get 0)
let infra_part = if ($parts | length) > 2 {
($parts | skip 1 | str join ":")
} else {
($parts | get 1?)
}
{
workspace: $workspace_part
infra: ($infra_part | default "")
}
} else {
{
workspace: $spec
infra: null
}
}
}
def workspace-exists? [workspace_name: string] {
let config = (load-user-config)
let workspace = ($config.workspaces | where name == $workspace_name | first)
($workspace | is-not-empty)
}
def infra-exists? [workspace_name: string, infra_name: string] {
let config = (load-user-config)
let workspace = ($config.workspaces | where name == $workspace_name | first)
if ($workspace | is-empty) {
return false
}
let infra_path = ([$workspace.path "infra" $infra_name] | path join)
let settings_file = ([$infra_path "settings.ncl"] | path join)
($settings_file | path exists)
}
export def validate-workspace-infra-spec [spec: string] {
let parsed = (parse-workspace-infra-notation $spec)
if not (workspace-exists? $parsed.workspace) {
return {
valid: false
workspace: $parsed.workspace
infra: $parsed.infra
error: $"Workspace '($parsed.workspace)' not found"
}
}
if ($parsed.infra | is-not-empty) {
if not (infra-exists? $parsed.workspace $parsed.infra) {
return {
valid: false
workspace: $parsed.workspace
infra: $parsed.infra
error: $"Infrastructure '($parsed.infra)' not found in workspace '($parsed.workspace)'"
}
}
}
{
valid: true
workspace: $parsed.workspace
infra: $parsed.infra
error: null
}
}
export def get-workspace-path [workspace_name: string] {
let config = (load-user-config)
let workspace = ($config.workspaces | where name == $workspace_name | first)
if ($workspace | is-empty) { return "" }
$workspace.path
}
export def list-workspaces [] {
let config = (load-user-config)
$config.workspaces
}