repositories / dotfiles
dotfiles
bugabingas dorkfiles
owned by admin
nushell/modules/wt-config.nu
Raw# Inert Nuon configuration, approval, and execution for `git wt` hooks.
const EVENTS = ["post-create" "pre-merge" "pre-remove"]
# Reject symlinks, directories, devices, and other non-regular files.
def assert-regular-or-missing [file: path, label: string] {
let kind = ($file | path type | default '')
if $kind not-in ['' 'file'] {
error make {msg: $"($label) must be a regular file: ($file)"}
}
}
def personal-config-path [] {
$env | get -o NU_WT_CONFIG | default ([$nu.default-config-dir "wt.nuon"] | path join)
}
def approvals-path [] {
$env | get -o NU_WT_APPROVALS | default ([$nu.data-dir "wt-approvals.nuon"] | path join)
}
def assert-context [context: record] {
for field in [root common_dir path branch] {
let value = ($context | get -o $field)
if $value == null or (($value | describe) != "string") or ($value | is-empty) {
error make {msg: $"hook context requires nonempty string `($field)`"}
}
}
for field in [target target_path] {
let value = ($context | get -o $field)
if $value != null and (($value | describe) != "string") {
error make {msg: $"hook context `($field)` must be string or null"}
}
}
}
def assert-event [event: string] {
if $event not-in $EVENTS {
error make {msg: $"unsupported wt hook event: ($event)"}
}
}
def config-value [file: path, label: string] {
assert-regular-or-missing $file $label
if not ($file | path exists) { return {} }
let parsed = try {
open --raw $file | from nuon
} catch {|err|
error make {msg: $"invalid ($label) Nuon: ($file): ($err.msg)"}
}
if not (($parsed | describe) | str starts-with "record") {
error make {msg: $"($label) must be a Nuon record: ($file)"}
}
$parsed
}
def assert-command [command: any, label: string] {
if not (($command | describe) | str starts-with "list<") or ($command | is-empty) {
error make {msg: $"($label) must be a nonempty list of strings"}
}
for arg in $command {
if ($arg | describe) != "string" {
error make {msg: $"($label) must be a list of strings"}
}
}
if ($command.0 | is-empty) { error make {msg: $"($label) requires an executable name"} }
}
def event-commands [config: record, event: string, label: string] {
let hooks = ($config | get -o hooks)
if $hooks == null { return [] }
if not (($hooks | describe) | str starts-with "record") {
error make {msg: $"($label) hooks must be a record"}
}
for configured_event in ($hooks | columns) {
if $configured_event not-in $EVENTS {
error make {msg: $"($label) has unsupported hook event: ($configured_event)"}
}
}
let commands = ($hooks | get -o $event)
if $commands == null { return [] }
if not (($commands | describe) | str starts-with "list<") {
error make {msg: $"($label) hooks.($event) must be a list of argv lists"}
}
for command in $commands {
assert-command $command $"($label) hooks.($event)"
}
$commands
}
def project-config-path [context: record] {
[$context.path ".config" "nu-wt.nuon"] | path join
}
def approval-hash [context: record, event: string, command: list<string>] {
{common_dir: $context.common_dir, event: $event, command: $command} | to nuon | hash sha256
}
def approval-value [file: path] {
assert-regular-or-missing $file "wt approval file"
if not ($file | path exists) { return {approvals: []} }
let parsed = try {
open --raw $file | from nuon
} catch {|err|
error make {msg: $"invalid wt approval Nuon: ($file): ($err.msg)"}
}
if not (($parsed | describe) | str starts-with "record") {
error make {msg: $"wt approval file must be a Nuon record: ($file)"}
}
let approvals = ($parsed | get -o approvals | default [])
let approval_type = ($approvals | describe)
if not (($approval_type | str starts-with "list<") or ($approval_type | str starts-with "table<")) {
error make {msg: "wt approval file approvals must be a list"}
}
for approval in $approvals {
if not (($approval | describe) | str starts-with "record") {
error make {msg: "wt approval file contains a non-record approval"}
}
for field in [common_dir event hash] {
let value = ($approval | get -o $field)
if $value == null or (($value | describe) != "string") or ($value | is-empty) {
error make {msg: $"wt approval file approval requires `($field)`"}
}
}
}
{approvals: $approvals}
}
def approved [approvals: any, context: record, event: string, command: list<string>] {
let hash = (approval-hash $context $event $command)
$approvals | any {|approval|
$approval.common_dir == $context.common_dir and $approval.event == $event and $approval.hash == $hash
}
}
def write-approvals [file: path, current: record, context: record, event: string, commands: list<list<string>>] {
let additions = ($commands | each {|command|
{common_dir: $context.common_dir, event: $event, hash: (approval-hash $context $event $command)}
})
let merged = ($current.approvals | append $additions | uniq-by common_dir event hash)
let parent = ($file | path dirname)
if not (($parent | path type) == "dir") {
error make {msg: $"wt approval parent must be a directory: ($parent)"}
}
{version: 1, approvals: $merged} | to nuon | save --force $file
}
def request-approval [event: string, commands: list<list<string>>] {
if not $nu.is-interactive {
error make {msg: $"unapproved project hooks for ($event); rerun with --approve-hooks"}
}
for command in $commands { print -e ($command | to nuon) }
print -e $"approve ($commands | length) project hook command(s) for ($event)? [y/N]"
let answer = (input | str trim | str lowercase)
if $answer not-in ["y" "yes"] {
error make {msg: "project hook approval declined"}
}
}
def hook-plan [event: string, context: record, approve: bool, skip: bool] {
assert-event $event
assert-context $context
if $skip { return {personal: [], project: []} }
let personal = (event-commands (config-value (personal-config-path) "personal wt config") $event "personal wt config")
let project = (event-commands (config-value (project-config-path $context) "project wt config") $event "project wt config")
let file = (approvals-path)
let current = (approval-value $file)
let missing = ($project | where {|command| not (approved $current.approvals $context $event $command)})
if not ($missing | is-empty) {
if not $approve { request-approval $event $missing }
write-approvals $file $current $context $event $missing
}
{personal: $personal, project: $project}
}
def run-command [command: list<string>, context: record] {
let executable = ($command | first)
let arguments = ($command | skip 1)
let hook_env = {
WT_REPO: $context.root
WT_WORKTREE: $context.path
WT_BRANCH: $context.branch
WT_TARGET: ($context | get -o target | default "")
WT_TARGET_PATH: ($context | get -o target_path | default "")
}
let result = (do {
cd $context.path
with-env $hook_env {|| run-external $executable ...$arguments | complete }
})
if not ($result.stdout | is-empty) { print -e $result.stdout }
if $result.exit_code == 0 and not ($result.stderr | is-empty) { print -e $result.stderr }
if $result.exit_code != 0 {
let detail = ($result.stderr | str trim)
let message = if ($detail | is-empty) { $"hook failed: ($executable)" } else { $"hook failed: ($executable): ($detail)" }
error make {msg: $message}
}
}
# Validate event configurations and project approvals without executing hooks.
export def preflight [
event: string
context: record
--approve # Approve current project-hook argv content.
--skip # Intentionally skip all configured hooks.
] {
hook-plan $event $context $approve $skip | ignore
}
# Validate, approve when requested, then execute hooks sequentially.
# `run` is a Nu parser keyword, so callers invoke this as `wt-config execute`.
export def execute [
event: string
context: record
--approve # Approve current project-hook argv content.
--skip # Intentionally skip all configured hooks.
] {
let plan = (hook-plan $event $context $approve $skip)
for command in ($plan.personal | append $plan.project) {
run-command $command $context
}
}