iroh_node_util/config.rs
1//! Utilities to get default paths for configuration, data, and cache directories.
2use std::{env, path::PathBuf};
3
4use anyhow::{anyhow, Result};
5
6/// Returns the path to the user's config directory for the given binary.
7///
8/// This is determined by the following steps:
9/// - If the environment variable `<BIN>_CONFIG_DIR` is set, return that.
10/// - If the operating environment provides a config directory, return `$CONFIG_DIR/<bin>`.
11/// - Otherwise, return an error.
12///
13/// The default directories are as follows:
14///
15/// | Platform | Value | Example |
16/// | -------- | ------------------------------------- | -------------------------------- |
17/// | Linux | `$XDG_CONFIG_HOME` or `$HOME`/.config/iroh | /home/alice/.config/iroh |
18/// | macOS | `$HOME`/Library/Application Support/iroh | /Users/Alice/Library/Application Support/iroh |
19/// | Windows | `{FOLDERID_RoamingAppData}`/iroh | C:\Users\Alice\AppData\Roaming\iroh |
20pub fn config_root(bin: &'static str) -> Result<PathBuf> {
21 let env_config_dir = format!("{}_CONFIG_DIR", bin.to_uppercase());
22 if let Some(val) = env::var_os(env_config_dir) {
23 return Ok(PathBuf::from(val));
24 }
25 let cfg = dirs_next::config_dir()
26 .ok_or_else(|| anyhow!("operating environment provides no directory for configuration"))?;
27 Ok(cfg.join(bin))
28}
29
30/// Returns the path to the user's data directory for the given binary.
31///
32/// This is determined by the following steps:
33/// - If the environment variable `<BIN>_DATA_DIR` is set, return that.
34/// - If the operating environment provides a data directory, return `$DATA_DIR/<bin>`.
35/// - Otherwise, return an error.
36///
37/// The default directories are as follows:
38///
39/// | Platform | Value | Example |
40/// | -------- | --------------------------------------------- | ---------------------------------------- |
41/// | Linux | `$XDG_DATA_HOME`/iroh or `$HOME`/.local/share/iroh | /home/alice/.local/share/iroh |
42/// | macOS | `$HOME`/Library/Application Support/iroh | /Users/Alice/Library/Application Support/iroh |
43/// | Windows | `{FOLDERID_RoamingAppData}/iroh` | C:\Users\Alice\AppData\Roaming\iroh |
44pub fn data_root(bin: &'static str) -> Result<PathBuf> {
45 let env_data_dir = format!("{}_DATA_DIR", bin.to_uppercase());
46 let path = if let Some(val) = env::var_os(env_data_dir) {
47 PathBuf::from(val)
48 } else {
49 let path = dirs_next::data_dir().ok_or_else(|| {
50 anyhow!("operating environment provides no directory for application data")
51 })?;
52 path.join(bin)
53 };
54 let path = if !path.is_absolute() {
55 std::env::current_dir()?.join(path)
56 } else {
57 path
58 };
59 Ok(path)
60}
61
62/// Returns the path to the user's cache directory for the given binary.
63///
64/// This is determined by the following steps:
65/// - If the environment variable `<BIN>_CACHE_DIR` is set, return that.
66/// - If the operating environment provides a cache directory, return `$CACHE_DIR/<bin>`.
67/// - Otherwise, return an error.
68///
69/// The default directories are as follows:
70///
71/// | Platform | Value | Example |
72/// | -------- | --------------------------------------------- | ---------------------------------------- |
73/// | Linux | `$XDG_CACHE_HOME`/iroh or `$HOME`/.cache/iroh | /home/.cache/iroh |
74/// | macOS | `$HOME`/Library/Caches/iroh | /Users/Alice/Library/Caches/iroh |
75/// | Windows | `{FOLDERID_LocalAppData}/iroh` | C:\Users\Alice\AppData\Roaming\iroh |
76pub fn cache_root(bin: &'static str) -> Result<PathBuf> {
77 let env_cache_dir = format!("{}_CACHE_DIR", bin.to_uppercase());
78 if let Some(val) = env::var_os(env_cache_dir) {
79 return Ok(PathBuf::from(val));
80 }
81 let path = dirs_next::cache_dir().ok_or_else(|| {
82 anyhow!("operating environment provides no directory for application data")
83 })?;
84 Ok(path.join(bin))
85}