89 lines
2.0 KiB
Plaintext
89 lines
2.0 KiB
Plaintext
# global_conf.nu -- 管理全局 dotfile 配置
|
|
#
|
|
# Copyright (C) 2022-2023 KAAAsS
|
|
|
|
use ../constraints.nu
|
|
|
|
export-env {
|
|
# 全局配置
|
|
$env.modules_config = { "_":
|
|
{ "constraints": (constraints default_constraints) }
|
|
}
|
|
}
|
|
|
|
# 获取模块的专有配置
|
|
export def get_config [
|
|
module: string,
|
|
config: string,
|
|
default?
|
|
] {
|
|
let modules_config = $env.modules_config
|
|
|
|
# 获取模块配置
|
|
let modules = ($modules_config | transpose key).key
|
|
let module_config = if ($module in $modules) {
|
|
$modules_config | get $module
|
|
} else {
|
|
{"_": null}
|
|
}
|
|
|
|
# 获取配置项
|
|
let configs = ($module_config | transpose key).key
|
|
if ($config in $configs) {
|
|
$module_config | get $config
|
|
} else {
|
|
$default
|
|
}
|
|
}
|
|
|
|
# 设置模块的专有配置
|
|
export def-env set_config [
|
|
module: string, # 模块名
|
|
config: string, # 配置项
|
|
value # 值
|
|
] {
|
|
let modules_config = $env.modules_config
|
|
|
|
# 检查模块配置是否存在
|
|
let modules = ($modules_config | transpose key).key
|
|
let old_config = if ($module in $modules) {
|
|
$modules_config | get $module
|
|
} else {
|
|
{}
|
|
}
|
|
|
|
# 设置新配置
|
|
let config = (
|
|
$old_config
|
|
| merge { $config : $value }
|
|
)
|
|
$env.modules_config = (
|
|
$modules_config
|
|
| merge { $module : $config }
|
|
)
|
|
}
|
|
|
|
# 获得模块列表
|
|
export def modules [] {
|
|
$env.modules_config
|
|
| transpose name config | where name != "_"
|
|
}
|
|
|
|
# 运行
|
|
export def test [] {
|
|
# TODO: assert
|
|
set_config "test" "foo" "2333"
|
|
print "test.foo:" (get_config "test" "foo")
|
|
set_config "test" "bar" [1, 2, 3]
|
|
print "test.bar:" (get_config "test" "bar")
|
|
print "test.foobar:" (get_config "test" "foobar" "default")
|
|
print "foobar.foobar:" (get_config "foobar" "foobar" "default")
|
|
|
|
set_config "test2" "bar" [1, 2, 3]
|
|
print "modules:" (modules)
|
|
|
|
# 结果
|
|
print "modules_config:" $env.modules_config
|
|
print "test:" $env.modules_config.test
|
|
}
|