vlib: implement os.config_dir() like in Go's UserConfigDir
* Reference https://pkg.go.dev/os@go1.18#UserConfigDirpull/13893/head
parent
6987f2c087
commit
f4e54423c9
25
vlib/os/os.v
25
vlib/os/os.v
|
|
@ -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')
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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) }
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue