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())
15            .context("Failed to migrate config!")?;
16    }
17
18    Ok(data_dir)
19}
20
21pub fn berg_config_dir() -> anyhow::Result<PathBuf> {
22    let config_dir = dirs::config_dir()
23        .context("Couldn't find config directory for saving global config")
24        .map(|dir| dir.join("berg-cli"))?;
25
26    // TODO: remove in a future version, added in 0.4.x
27    //
28    // This moves pre-existing configs into the new "left" directory
29    dirs::data_dir()
30        .into_iter()
31        .flat_map(|data_dir| [data_dir.join("berg-cli"), data_dir.join(".berg-cli")])
32        .for_each(|dir| {
33            let file = dir.join("berg.toml");
34            _ = std::fs::create_dir_all(config_dir.as_path());
35            _ = std::fs::rename(file, config_dir.join("berg.toml"));
36        });
37
38    Ok(config_dir)
39}
40
41pub fn token_path() -> anyhow::Result<PathBuf> {
42    berg_data_dir()
43        .map(|token_dir| token_dir.join("TOKEN"))
44        .context("token path not found")
45}
46
47pub fn config_path() -> anyhow::Result<PathBuf> {
48    berg_config_dir()
49        .map(|dir| dir.join("berg.toml"))
50        .context("config path not found")
51}