From de9c68b085034c897f26ce22fa05bd2601e79373 Mon Sep 17 00:00:00 2001 From: Leonardo Bishop Date: Wed, 25 Dec 2024 15:17:12 +0000 Subject: Version 0.2.0 --- src/install.rs | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) (limited to 'src/install.rs') diff --git a/src/install.rs b/src/install.rs index 57da431..70338ca 100644 --- a/src/install.rs +++ b/src/install.rs @@ -1,20 +1,20 @@ -use crate::config::{Step, CopyPath}; +use crate::config::{CopyPath, Link, Step}; use std::{fs, path::PathBuf}; use crate::util::expand_home; -fn resolve_paths_and_mkdir(paths: &CopyPath, base_path: &PathBuf) -> Result<(PathBuf, PathBuf), Box> { - let expanded_home = &expand_home(&paths.to); +fn resolve_paths_and_mkdir(from: &String, to: &String, base_path: &PathBuf) -> Result<(PathBuf, PathBuf), Box> { + let expanded_home = &expand_home(&to); let destination = PathBuf::from(expanded_home); let mut source = base_path.clone(); - source.push(&paths.from); + source.push(&from); let dest_parent = destination.parent().unwrap(); fs::create_dir_all(dest_parent)?; Ok((source, destination)) } -fn ln(paths: &CopyPath, base_path: &PathBuf) -> Result> { - let (source, destination) = resolve_paths_and_mkdir(paths, base_path)?; +fn ln(paths: &Link, base_path: &PathBuf) -> Result> { + let (source, destination) = resolve_paths_and_mkdir(&paths.from, &paths.to, base_path)?; let source = source.as_path(); let destination = destination.as_path(); @@ -23,8 +23,18 @@ fn ln(paths: &CopyPath, base_path: &PathBuf) -> Result Result> { + let (source, destination) = resolve_paths_and_mkdir(&paths.from, &paths.to, base_path)?; + let source = source.as_path(); + let destination = destination.as_path(); + + let _ = fs::remove_file(destination); + fs::soft_link(source, destination)?; + Ok(true) +} + fn cp(paths: &CopyPath, base_path: &PathBuf) -> Result> { - let (source, destination) = resolve_paths_and_mkdir(paths, base_path)?; + let (source, destination) = resolve_paths_and_mkdir(&paths.from, &paths.to, base_path)?; let source = source.as_path(); let destination = destination.as_path(); @@ -42,7 +52,12 @@ fn run_shell(command: &String) -> Result> { pub fn run_step(step: &Step, base_path: &PathBuf) -> Result> { match step { - Step::Link(path) => { ln(path, base_path) }, + Step::Link(path) => { + match path { + Link { symbolic: Some(true), .. } => ln_sym(path, base_path), + _ => ln(path, base_path), + } + }, Step::Copy(path) => { cp(path, base_path) }, Step::Shell(command) => run_shell(command), } -- cgit v1.2.3-70-g09d2