Skip to content

Commit 7076663

Browse files
committed
refactor: simplify bombadil link
1 parent 9e11fe7 commit 7076663

File tree

1 file changed

+37
-32
lines changed

1 file changed

+37
-32
lines changed

src/lib.rs

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -65,40 +65,45 @@ impl Bombadil {
6565
/// Symlink `bombadil.toml` to `$XDG_CONFIG/bombadil.toml` so we can later read it from there.
6666
pub fn link_self_config(dotfiles_path: Option<PathBuf>) -> Result<()> {
6767
let xdg_config_dir = dirs::config_dir();
68-
if xdg_config_dir.is_none() {
69-
return Err(anyhow!("$XDG_CONFIG does not exist"));
70-
}
71-
72-
let xdg_config = Settings::bombadil_config_xdg_path()?;
68+
match xdg_config_dir {
69+
None => return Err(anyhow!("$XDG_CONFIG does not exist")),
70+
Some(config_dir) => {
71+
let bombadil_xdg_config = config_dir.join(BOMBADIL_CONFIG);
72+
73+
// Attempt to locate a previous '$HOME/.config/bombadil.toml' link and remove it
74+
if fs::symlink_metadata(&bombadil_xdg_config).is_ok() {
75+
fs::remove_file(&bombadil_xdg_config)?;
76+
}
7377

74-
if fs::symlink_metadata(&xdg_config).is_ok() {
75-
fs::remove_file(&xdg_config)?;
78+
// Get the provided path and attempt to resolve 'bombadil.toml' if it's a directory
79+
let dotfiles_path = dotfiles_path
80+
.map(|path| {
81+
if path.is_dir() {
82+
path.join(BOMBADIL_CONFIG)
83+
} else {
84+
path
85+
}
86+
})
87+
.unwrap_or_else(|| PathBuf::from(BOMBADIL_CONFIG))
88+
.canonicalize()?;
89+
90+
// Symlink to '$HOME/.config/bombadil.toml'
91+
unix::fs::symlink(&dotfiles_path, &bombadil_xdg_config)
92+
.map_err(|err| {
93+
anyhow!(
94+
"Unable to symlink {:?} to {:?} : {}",
95+
dotfiles_path,
96+
bombadil_xdg_config,
97+
err
98+
)
99+
})
100+
.map(|_result| {
101+
let source = format!("{:?}", &dotfiles_path).blue();
102+
let dest = format!("{:?}", &bombadil_xdg_config).green();
103+
println!("{} => {}", source, dest)
104+
})
105+
}
76106
}
77-
78-
let dotfiles_path = &dotfiles_path
79-
.unwrap_or_else(|| PathBuf::from(BOMBADIL_CONFIG))
80-
.canonicalize()?;
81-
82-
let dotfiles_path = if dotfiles_path.is_dir() {
83-
dotfiles_path.join(BOMBADIL_CONFIG)
84-
} else {
85-
return Err(anyhow!("Config not found"));
86-
};
87-
88-
unix::fs::symlink(&dotfiles_path, &xdg_config)
89-
.map_err(|err| {
90-
anyhow!(
91-
"Unable to symlink {:?} to {:?} : {}",
92-
dotfiles_path,
93-
xdg_config,
94-
err
95-
)
96-
})
97-
.map(|_result| {
98-
let source = format!("{:?}", &dotfiles_path).blue();
99-
let dest = format!("{:?}", &xdg_config).green();
100-
println!("{} => {}", source, dest)
101-
})
102107
}
103108

104109
/// The installation process is composed of the following steps :

0 commit comments

Comments
 (0)