Skip to content

Instantly share code, notes, and snippets.

@devyn
Created February 28, 2024 08:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devyn/24f6d412d087a0923acd80800da0b4ec to your computer and use it in GitHub Desktop.
Save devyn/24f6d412d087a0923acd80800da0b4ec to your computer and use it in GitHub Desktop.
Envfile utils
# Replace environment variables in a string. Accepts $VAR and ${VAR} syntax. Handles $PATH.
export def "str replace-env" [
--overlay: record = {} # Use this record as an overlay over the environment
] {
# Replace PATH first, from overlay if it exists
let in_string = $in | str replace --all --regex '\$PATH|\$\{PATH\}' (
$overlay.PATH? | default $env.PATH | str join (char esep)
)
# Parse env variables and look them up in $env
($in_string |
parse --regex '(?<match>\$(?<var0>\w+)|\$\{(?<var1>\w+)\})' |
uniq |
reduce --fold $in_string { |var, s|
# Env key has been parsed into either the var0 group or the var1 group
let env_key = [$var.var0 $var.var1] | filter { not ($in | is-empty) } | first
# Get from either the overlay or $env
let replacement = $overlay | get -i $env_key | default ($env | get $env_key)
$s | str replace --all $var.match $replacement
})
}
# Read an environment file / bash script. Will substitute $VAR, and ignores `export`
export def "from env" [] {
lines |
parse -r `^(?:export )?(?<name>\w+) *= *(?<quote>['"]?)(?<value>.*)\k<quote>$` |
# Handle environment variables that update previously set ones, and env substitutions
reduce --fold {} { |var, out|
$out | upsert $var.name ($var.value | str replace-env --overlay=$out)
}
}
# Write an environment file.
export def "to env" [
--export # Start definitions with `export`, for sourcing in Bourne-like shells
--quote # Quote the values
] {
transpose key value | each { |it|
$"(if $export { 'export ' })($it.key)=($it.value | if $quote { to json } else { $in })"
} | str join "\n"
}
# Load an environment file into the environment
export def --env load-envfile [path: string] {
open $path | from env | load-env
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment