codeberg_cli/paths/
mod.rs

1use anyhow::Context;
2use std::path::PathBuf;
3
4pub fn berg_data_dir() -> anyhow::Result<PathBuf> {
5    let dir = dirs::data_dir().context("Couldn't find data directory for saving the token.")?;
6
7    let data_dir = dir.join("berg-cli");
8
9    // TODO: remove in a future version, added in 0.4.x
10    //
11    // This moves pre-existing data directories into the new "left" directory
12    let legacy_data_dir = dir.join(".berg-cli");
13    if legacy_data_dir.exists() {
14        std::fs::rename(legacy_data_dir, data_dir.as_path()).map_err(anyhow::Error::from)?;
15    }
16
17    Ok(data_dir)
18}
19
20pub fn berg_config_dir() -> anyhow::Result<PathBuf> {
21    let config_dir = dirs::config_dir()
22        .context("Couldn't find config directory for saving global config")
23        .map(|dir| dir.join("berg-cli"))?;
24
25    // TODO: remove in a future version, added in 0.4.x
26    //
27    // This moves pre-existing configs into the new "left" directory
28    dirs::data_dir()
29        .into_iter()
30        .flat_map(|data_dir| [data_dir.join("berg-cli"), data_dir.join(".berg-cli")])
31        .for_each(|dir| {
32            let file = dir.join("berg.toml");
33            _ = std::fs::create_dir_all(config_dir.as_path());
34            _ = std::fs::rename(file, config_dir.join("berg.toml"));
35        });
36
37    Ok(config_dir)
38}
39
40pub fn token_path() -> anyhow::Result<PathBuf> {
41    berg_data_dir().map(|token_dir| token_dir.join("TOKEN"))
42}
43
44pub fn config_path() -> anyhow::Result<PathBuf> {
45    berg_config_dir().map(|dir| dir.join("berg.toml"))
46}