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