# utility functions # TODO: move this into nushell modules # returs true, if the sequene has exactly one element export def has-one []: list -> bool { let size = $in | length $size == 1 } # Gets value of a column, or falls back to given alternative value. export def get-or-else [ column: string # name of the column to get its value from value: any # alternative value to return, in case `get` gets nothing ]: [ record -> any table -> any ] { select -i $column | update $column { if ( $in | is-empty ) { $value } else { $in } } | get $column } # checks if a file is executable export def is-executable []: path -> bool { match $nu.os-info.family { 'unix' => { $env | upsert __file_for_executbale_check ( $in | path expand ) run-external sh '-c' '[ -x "$__file_for_executbale_check" ] && echo true || echo false' | into bool } 'windows' => { # poor mans check on win32 $in | find --regex '(\.exe)|(\.bat)|(\.ps1)$' | is-not-empty } } } # creates a symbolic link export def slink [ source: path # the original file path target: path # the file of the symbolic link ] { match $nu.os-info.family { 'unix' => { run-external ln '--symbolic' $source $target } 'windows' => { # TODO: test on win32 run-external cmd '/c' mklink $target $source } } } # Checks any value for the expected type. # The expected type must be given EXACTLY as reported by `describe`. # Remember the whitespace! # If the type does not match, an error is thrown. export def type-check [ expected_type: string # Expected typ of piped in value ]: any -> nothing { let actual_type = ($in | describe) if ( $actual_type != $expected_type) { error make { msg: $'Type error' label: { text: $'Expected ($expected_type) type, but was ($actual_type)' span: (metadata $in).span } } } }