website-htmx-rustelo-code/scripts/content/lib/env.nu

91 lines
3.4 KiB
Text
Raw Normal View History

2026-07-10 03:44:13 +01:00
# Environment and Configuration Utilities
# Reusable functions for loading environment variables and configuration
# Load environment variables from .env file
export def load_env_from_file [] {
let env_file = ".env"
if ($env_file | path exists) {
let env_content = (open $env_file | lines)
for line in $env_content {
# Skip comments and empty lines
if not ($line | str trim | str starts-with "#") and not ($line | str trim | is-empty) {
# Parse KEY=VALUE format with variable expansion
if ($line | str contains "=") {
let parts = ($line | split column "=" key value)
if ($parts | length) >= 2 {
let key = ($parts | first | get key | str trim)
let value = ($parts | first | get value | str trim)
# Remove quotes and expand ${VARIABLE} patterns
let clean_value = ($value | str replace -a '"' '' | str replace -a "'" '')
let expanded_value = expand_env_variables $clean_value
# Set environment variable
load-env {($key): $expanded_value}
}
}
}
}
}
}
# Expand ${VARIABLE} patterns in environment values
export def expand_env_variables [value: string] {
mut result = $value
# Simple ${VAR} expansion
let var_pattern = '\\$\\{([A-Z_][A-Z0-9_]*)\\}'
let matches = ($result | parse --regex $var_pattern)
for match in $matches {
if "capture0" in ($match | columns) {
let var_name = ($match | get capture0)
if $var_name in $env {
let var_value = ($env | get $var_name)
$result = ($result | str replace $"\\${${var_name}}" $var_value)
}
}
}
$result
}
# Get environment variable with fallback
export def get_env_var [var_name: string, default_value: string] {
if $var_name in $env {
return ($env | get $var_name)
}
return $default_value
}
# Get enabled content types from content-kinds.toml
export def get_enabled_content_types [content_dir: string] {
let content_kinds_file = $"($content_dir)/content-kinds.toml"
if not ($content_kinds_file | path exists) {
print $"(ansi yellow)⚠️ Warning: content-kinds.toml not found, using defaults(ansi reset)"
return ["blog", "recipes"]
}
mut enabled_types = []
let toml_content = open $content_kinds_file
if "content_kinds" in $toml_content {
for content_kind in $toml_content.content_kinds {
if $content_kind.enabled {
let dir_path = $"($content_dir)/($content_kind.directory)"
if ($dir_path | path exists) {
$enabled_types = ($enabled_types | append $content_kind.directory)
print $"(ansi green)✅ Content type '($content_kind.name)' -> '($content_kind.directory)' enabled(ansi reset)"
} else {
print $"(ansi yellow)⚠️ WARNING: Content type '($content_kind.name)' enabled but directory missing - SKIPPING(ansi reset)"
}
}
}
}
if ($enabled_types | is-empty) {
print $"(ansi yellow)⚠️ No enabled content types, using defaults(ansi reset)"
return ["blog", "recipes"]
}
$enabled_types
}