Cross-platform method to get user's configuration home in Lua

Viewed 65

The major operating systems seem to have a designated place to store configuration files.

On Linux it would be something like $HOME/.myprogram/data or $HOME/.config/myprogram/data, or perhaps a better practice would be to read $XDG_CONFIG_HOME.

On macOS it would be $HOME/.myprogram/data or $HOME/Library/Preferences/myprogram/data.

On Windows it would be %appdata%/myprogram/data.

How to get such value in a portable manner in Lua?

1 Answers

Check the existence condition of a variable with...

if (os.getenv('XDG_CONFIG_HOME')) then
 os.execute('ls ' .. os.getenv('XDG_CONFIG_HOME'))
else -- Or: elseif (nextcondition) then
 print('XDG_CONFIG_HOME', 'Not exists')
 -- Maybe next check here? 
end

But be aware that a user could start Lua with only that environment that Lua needs. Like...

lua () 
{ 
    env -i DISPLAY=:0 LANG='de_DE.UTF-8' TERM='xterm-256color' LUA_PATH='./lua/?.lua' LUA_CPATH='./lua/?.so' /usr/local/bin/lua "${@}"
}

Then check for XDG_CONFIG_HOME will allways fail.
Because above environment knows nothing about HOME, USER and XDG_CONFIG_HOME.

PS: I know a place too where my linux system saves configs/savegames or whatever

${HOME}/.local/share/
Related