Skip to content

Commit 2124b02

Browse files
committed
refactor: dirs -> etcetera
1 parent ada16ec commit 2124b02

File tree

4 files changed

+36
-90
lines changed

4 files changed

+36
-90
lines changed

Cargo.lock

Lines changed: 21 additions & 39 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ bzip2 = "0.4.4"
1616
clap = { version = "4.5.20", features = ["derive"] }
1717
clap_complete = "4.5.33"
1818
color-eyre = "0.6.3"
19-
dirs = "5.0.1"
2019
enum_dispatch = "0.3.13"
20+
etcetera = "0.8.0"
2121
eyre = "0.6.12"
2222
flate2 = "1.0.34"
2323
owo-colors = "4.1.0"

src/config.rs

Lines changed: 14 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
//
33
// SPDX-License-Identifier: GPL-3.0-or-later
44

5+
use etcetera::AppStrategy as _;
56
use eyre::{eyre, Result};
67
use std::path::{Path, PathBuf};
78
use tokio::fs;
@@ -28,68 +29,33 @@ pub struct Userchrome {
2829
pub configs: Vec<UserchromeConfig>,
2930
}
3031

31-
#[derive(Deserialize, Serialize, Debug)]
32+
#[derive(Deserialize, Serialize, Debug, Default)]
3233
pub struct Config {
3334
pub profile: Option<PathBuf>,
3435

3536
#[serde(default)]
3637
pub userchromes: Vec<Userchrome>,
3738
}
3839

39-
pub fn get_old_config_path() -> Result<PathBuf> {
40-
dirs::config_dir()
41-
.ok_or_else(|| eyre!("Unable to locate config dirs"))
42-
.map(|dir| dir.join("nyoom.toml"))
40+
fn strategy() -> Result<impl etcetera::AppStrategy> {
41+
etcetera::choose_app_strategy(etcetera::AppStrategyArgs {
42+
top_level_domain: "dev.ryanccn".to_owned(),
43+
author: "Ryan Cao".to_owned(),
44+
app_name: "nyoom".to_owned(),
45+
})
46+
.map_err(|e| e.into())
4347
}
4448

4549
pub fn get_default_config_path() -> Result<PathBuf> {
46-
dirs::config_dir()
47-
.ok_or_else(|| eyre!("Unable to locate config dirs"))
48-
.map(|dir| dir.join("nyoom").join("nyoom.toml"))
49-
}
50-
51-
pub async fn migrate_config() -> Result<()> {
52-
let old = get_old_config_path()?;
53-
let new = get_default_config_path()?;
54-
55-
if old.exists() && !new.exists() {
56-
fs::create_dir_all(
57-
new.parent()
58-
.ok_or_else(|| eyre!("Could not obtain parent directory of config"))?,
59-
)
60-
.await?;
61-
fs::copy(old, new).await?;
62-
}
63-
64-
Ok(())
50+
Ok(strategy()?.config_dir().join("nyoom.toml"))
6551
}
6652

6753
pub async fn get_config(path: &Path) -> Result<Config> {
68-
let f = if Path::new(path).exists() {
69-
fs::read_to_string(path).await?
70-
} else {
71-
String::new()
72-
};
73-
let mut config: Config = toml::from_str(&f)?;
74-
75-
let mut migrated = false;
76-
77-
for uc in &mut config.userchromes {
78-
if let Some(old_clone_url) = &uc.clone_url {
79-
uc.source = old_clone_url
80-
.replace("https://github.com/", "github:")
81-
.replace(".git", "");
82-
uc.clone_url = None;
83-
84-
migrated = true;
85-
}
54+
match fs::read_to_string(path).await {
55+
Ok(s) => toml::from_str(&s).map_err(Into::into),
56+
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(Config::default()),
57+
Err(e) => Err(e.into()),
8658
}
87-
88-
if migrated {
89-
set_config(path, &config).await?;
90-
}
91-
92-
Ok(config)
9359
}
9460

9561
pub async fn set_config(path: &Path, config: &Config) -> Result<()> {

src/main.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use clap::Parser;
66
use cmd::{Cli, Command};
7-
use config::migrate_config;
87
use eyre::Result;
98

109
mod cmd;
@@ -17,7 +16,6 @@ mod utils;
1716
async fn main() -> Result<()> {
1817
color_eyre::install()?;
1918

20-
migrate_config().await?;
2119
let cli = Cli::parse();
2220
cli.command.action(&cli).await?;
2321

0 commit comments

Comments
 (0)