vlib: implement os.config_dir() like in Go's UserConfigDir

* Reference https://pkg.go.dev/os@go1.18#UserConfigDir
pull/13893/head
pancake 2022-04-01 16:40:51 +02:00
parent 6987f2c087
commit f4e54423c9
2 changed files with 32 additions and 0 deletions

View File

@ -766,3 +766,28 @@ pub fn quoted_path(path string) string {
return "'$path'"
}
}
// returns the path to the user configuration directory or error in case the environment is missing
pub fn config_dir() ?string {
$if windows {
app_data := getenv('AppData')
if app_data != '' {
return app_data
}
} $else $if macos || darwin || ios {
home := home_dir()
if home != '' {
return home + '/Library/Application Support'
}
} $else {
xdg_home := getenv('XDG_CONFIG_HOME')
if xdg_home != '' {
return xdg_home
}
home := home_dir()
if home != '' {
return home + '/.config'
}
}
return error('Cannot find config directory')
}

View File

@ -894,3 +894,10 @@ fn test_command() {
// dump( cmd_to_fail )
assert cmd_to_fail.exit_code != 0 // 2 on linux, 1 on macos
}
fn test_config_dir() {
cdir := os.config_dir() or { panic(err) }
adir := '$cdir/test-v-config'
os.mkdir_all(adir) or { panic(err) }
os.rmdir(adir) or { panic(err) }
}