hare

The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

commit 76226b22c181557b49b1bf74bc95cd8dca3c5110
parent e322f5b544dc25c63320400c8251db7c865dacc6
Author: Drew DeVault <sir@cmpwn.com>
Date:   Tue,  2 Mar 2021 15:07:18 -0500

dirs: new module

Diffstat:
Adirs/xdg.ha | 60++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 60 insertions(+), 0 deletions(-)

diff --git a/dirs/xdg.ha b/dirs/xdg.ha @@ -0,0 +1,60 @@ +use fs; +use os; +use path; +use io; + +fn lookup(prog: str, var: str, default: path::path) path::path = { + match (os::getenv(var)) { + s: str => { + let path = path::join(s, prog); + match (os::stat(path)) { + err: fs::error => { + os::mkdirs(path) as void; + return path; + }, + st: fs::filestat => { + if (fs::is_dir(st.mode)) { + return path; + }; + }, + }; + }, + void => void, + }; + + let home = match (os::getenv("HOME")) { + s: str => s, + void => abort("$HOME unset"), // TODO: Try reading /etc/passwd + }; + + let path = path::join(home, default, prog); + os::mkdirs(path) as void; + return path; +}; + +// Returns a directory suitable for storing config files. If 'prog' is given, a +// unique path for this program to store data will be returned. +export fn config(prog: str) path::path = + lookup(prog, "XDG_CONFIG_HOME", ".config"); + +// Returns an [fs::fs] for storing config files. If 'prog' is given, a unique +// path for this program to store data will be returned. +export fn config_fs(prog: str) *fs::fs = os::diropen(config(prog)) as *fs::fs; + +// Returns a directory suitable for cache files. If 'prog' is given, a unique +// path for this program to store data will be returned. +export fn cache(prog: str) path::path = + lookup(prog, "XDG_CACHE_HOME", ".cache"); + +// Returns an [fs::fs] for cache files. If 'prog' is given, a unique path for +// this program to store data will be returned. +export fn cache_fs(prog: str) *fs::fs = os::diropen(cache(prog)) as *fs::fs; + +// Returns a directory suitable for cache files. If 'prog' is given, a unique +// path for this program to store data will be returned. +export fn data(prog: str) path::path = + lookup(prog, "XDG_DATA_HOME", path::join(".local", "share")); + +// Returns an [fs::fs] for cache files. If 'prog' is given, a unique path for +// this program to store data will be returned. +export fn data_fs(prog: str) *fs::fs = os::diropen(data(prog)) as *fs::fs;