Add dynamic context update and prompt for missing values
parent
b423a72171
commit
38acebea6f
@ -0,0 +1,54 @@
|
||||
def before_apply_all [ctx] {
|
||||
let silo_ctx = (silo context | from json)
|
||||
let config_file = ($silo_ctx.dirs.config | path join "silo.toml")
|
||||
|
||||
mut config = open-config $config_file
|
||||
|
||||
$config.template_context = ($config.template_context
|
||||
| apply-defaults
|
||||
| default {}
|
||||
| merge (template-context $ctx $silo_ctx $config)
|
||||
)
|
||||
print $"Context updated to ($config.template_context)"
|
||||
$config | save -f $config_file
|
||||
}
|
||||
|
||||
def open-config [path: string] {
|
||||
if ($path | path exists) {
|
||||
open $path
|
||||
} else {
|
||||
{}
|
||||
}
|
||||
}
|
||||
|
||||
def template-context [ctx: any, silo_ctx: any, config: any] {
|
||||
{
|
||||
username: (whoami)
|
||||
}
|
||||
}
|
||||
|
||||
def apply-defaults [] {
|
||||
let cfg = $in
|
||||
{
|
||||
system-id: ($cfg | get-or-prompt system-id)
|
||||
environment: ($cfg | get-or-prompt environment)
|
||||
font-size: 12
|
||||
icons: false
|
||||
}
|
||||
}
|
||||
|
||||
def get-or-prompt [key: string] {
|
||||
$in | get -i $key | default-prompt $"Enter `($key)`: "
|
||||
}
|
||||
|
||||
def default-prompt [prompt: string] {
|
||||
default-with {|| input $prompt}
|
||||
}
|
||||
|
||||
def default-with [fn] {
|
||||
if $in == null {
|
||||
do $fn
|
||||
} else {
|
||||
$in
|
||||
}
|
||||
}
|
@ -1,23 +0,0 @@
|
||||
def before_apply_all [ctx] {
|
||||
let silo_ctx = (silo context | from json)
|
||||
let config_file = ($silo_ctx.dirs.config | path join "silo.toml")
|
||||
|
||||
mut config = open_config $config_file
|
||||
|
||||
$config.template_context = ($config.template_context | default {} | merge (template_context $ctx $silo_ctx $config))
|
||||
$config | save -f $config_file
|
||||
}
|
||||
|
||||
def open_config [path: string] {
|
||||
if ($path | path exists) {
|
||||
open $path
|
||||
} else {
|
||||
{}
|
||||
}
|
||||
}
|
||||
|
||||
def template_context [ctx: any, silo_ctx: any, config: any] {
|
||||
{
|
||||
username: (whoami)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue