swift-xdg

Swift 中 XDG 基本目录规范的实现。

此软件包在很大程度上受到了一个 Rust 实现的启发。

用法

let dirs = try BaseDirectories(prefixAll: "my-amazing-app")
/* Get existing configuration path. */
let confPath = try dirs.findConfigFile("conf.toml")
/* If all paths are needed, just add an “s”. */
let confPaths = try dirs.findConfigFiles("conf.toml")
/* Get path for writing new config.
 * The method makes sure the parent directory exists for the file 
 *  and return the path where the file should be. */
let newConfPath = try dirs.ensureParentsForConfigFilePath("conf.toml")

这个特定的基本目录规范实现确保返回的路径永远不会超出它们应该位于的 home 目录范围。

例如,如果 XDG_CONFIG_HOME=/home/frizlab/.config,我们确保此软件包中返回配置路径的任何函数都不会返回此文件夹之外的路径,无论需要什么前缀和文件名。

一个简单的例子: let confPath = try dirs.findConfigFile("../conf.toml") 将会抛出错误。