Skip to content

Commit

Permalink
refactor: simplify bombadil link
Browse files Browse the repository at this point in the history
  • Loading branch information
oknozor committed Apr 5, 2022
1 parent 919e43e commit 0be91db
Showing 1 changed file with 37 additions and 32 deletions.
69 changes: 37 additions & 32 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,40 +65,45 @@ impl Bombadil {
/// Symlink `bombadil.toml` to `$XDG_CONFIG/bombadil.toml` so we can later read it from there.
pub fn link_self_config(dotfiles_path: Option<PathBuf>) -> Result<()> {
let xdg_config_dir = dirs::config_dir();
if xdg_config_dir.is_none() {
return Err(anyhow!("$XDG_CONFIG does not exist"));
}

let xdg_config = Settings::bombadil_config_xdg_path()?;
match xdg_config_dir {
None => return Err(anyhow!("$XDG_CONFIG does not exist")),
Some(config_dir) => {
let bombadil_xdg_config = config_dir.join(BOMBADIL_CONFIG);

// Attempt to locate a previous '$HOME/.config/bombadil.toml' link and remove it
if fs::symlink_metadata(&bombadil_xdg_config).is_ok() {
fs::remove_file(&bombadil_xdg_config)?;
}

if fs::symlink_metadata(&xdg_config).is_ok() {
fs::remove_file(&xdg_config)?;
// Get the provided path and attempt to resolve 'bombadil.toml' if it's a directory
let dotfiles_path = dotfiles_path
.map(|path| {
if path.is_dir() {
path.join(BOMBADIL_CONFIG)
} else {
path
}
})
.unwrap_or_else(|| PathBuf::from(BOMBADIL_CONFIG))
.canonicalize()?;

// Symlink to '$HOME/.config/bombadil.toml'
unix::fs::symlink(&dotfiles_path, &bombadil_xdg_config)
.map_err(|err| {
anyhow!(
"Unable to symlink {:?} to {:?} : {}",
dotfiles_path,
bombadil_xdg_config,
err
)
})
.map(|_result| {
let source = format!("{:?}", &dotfiles_path).blue();
let dest = format!("{:?}", &bombadil_xdg_config).green();
println!("{} => {}", source, dest)
})
}
}

let dotfiles_path = &dotfiles_path
.unwrap_or_else(|| PathBuf::from(BOMBADIL_CONFIG))
.canonicalize()?;

let dotfiles_path = if dotfiles_path.is_dir() {
dotfiles_path.join(BOMBADIL_CONFIG)
} else {
return Err(anyhow!("Config not found"));
};

unix::fs::symlink(&dotfiles_path, &xdg_config)
.map_err(|err| {
anyhow!(
"Unable to symlink {:?} to {:?} : {}",
dotfiles_path,
xdg_config,
err
)
})
.map(|_result| {
let source = format!("{:?}", &dotfiles_path).blue();
let dest = format!("{:?}", &xdg_config).green();
println!("{} => {}", source, dest)
})
}

/// The installation process is composed of the following steps :
Expand Down

0 comments on commit 0be91db

Please sign in to comment.