diff options
| author | Leonardo Bishop <me@leonardobishop.com> | 2024-12-25 15:17:12 +0000 |
|---|---|---|
| committer | Leonardo Bishop <me@leonardobishop.com> | 2024-12-25 15:17:12 +0000 |
| commit | de9c68b085034c897f26ce22fa05bd2601e79373 (patch) | |
| tree | 9996862988168ba7fa6e561bc2e5133072356c0e | |
| parent | ffa08735aee2d69e17bce5c29a7ef96cc7e2233f (diff) | |
Version 0.2.0
| -rw-r--r-- | Cargo.lock | 2 | ||||
| -rw-r--r-- | Cargo.toml | 2 | ||||
| -rw-r--r-- | PKGBUILD | 2 | ||||
| -rwxr-xr-x | install.sh | 8 | ||||
| -rw-r--r-- | src/cli.rs | 3 | ||||
| -rw-r--r-- | src/config.rs | 11 | ||||
| -rw-r--r-- | src/install.rs | 31 | ||||
| -rw-r--r-- | src/main.rs | 6 | ||||
| -rw-r--r-- | src/util.rs | 2 |
9 files changed, 49 insertions, 18 deletions
@@ -123,7 +123,7 @@ dependencies = [ [[package]] name = "dotfiles-installer" -version = "0.1.0" +version = "0.2.0" dependencies = [ "clap", "colored", @@ -1,6 +1,6 @@ [package] name = "dotfiles-installer" -version = "0.1.0" +version = "0.2.0" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html @@ -1,5 +1,5 @@ pkgname=dotfiles-installer -pkgver=0.1.0 +pkgver=0.2.0 pkgrel=1 makedepends=('rust' 'cargo') arch=('any') @@ -1,8 +1,8 @@ #!/bin/sh -if ! [ rustc --version > /dev/null 2>&1 ]; then - sudo pacman -S rustup || exit $? - rustup default stable || exit $? -fi +#if ! [ rustc --version > /dev/null 2>&1 ]; then +# sudo pacman -S rustup || exit $? +# rustup default stable || exit $? +#fi makepkg -f || exit $? sudo pacman -U dotfiles-installer*.pkg.tar* @@ -5,4 +5,7 @@ use clap::Parser; pub struct Args { #[arg(short, long)] pub file: String, + + #[arg(short, long, default_value_t = true)] + pub dry_run: bool, }
\ No newline at end of file diff --git a/src/config.rs b/src/config.rs index 2a0e38b..f5754eb 100644 --- a/src/config.rs +++ b/src/config.rs @@ -8,12 +8,19 @@ use serde::{Serialize, Deserialize}; pub struct CopyPath { pub from: String, pub to: String, - pub recursive: bool, + pub recursive: Option<bool>, +} + +#[derive(Debug, Serialize, Deserialize)] +pub struct Link { + pub from: String, + pub to: String, + pub symbolic: Option<bool>, } #[derive(Debug, Serialize, Deserialize)] pub enum Step { - Link(CopyPath), + Link(Link), Copy(CopyPath), Shell(String), } 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<dyn std::error::Error>> { - let expanded_home = &expand_home(&paths.to); +fn resolve_paths_and_mkdir(from: &String, to: &String, base_path: &PathBuf) -> Result<(PathBuf, PathBuf), Box<dyn std::error::Error>> { + 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<bool, Box<dyn std::error::Error>> { - let (source, destination) = resolve_paths_and_mkdir(paths, base_path)?; +fn ln(paths: &Link, base_path: &PathBuf) -> Result<bool, Box<dyn std::error::Error>> { + 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<bool, Box<dyn std::error: Ok(true) } +fn ln_sym(paths: &Link, base_path: &PathBuf) -> Result<bool, Box<dyn std::error::Error>> { + 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<bool, Box<dyn std::error::Error>> { - 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<bool, Box<dyn std::error::Error>> { pub fn run_step(step: &Step, base_path: &PathBuf) -> Result<bool, Box<dyn std::error::Error>> { 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), } diff --git a/src/main.rs b/src/main.rs index 08a4f51..7ad6d79 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,6 +12,7 @@ use util::*; fn main() { let args = cli::Args::parse(); let install_profile = &args.file; + let dry_run = &args.dry_run; let loaded_config: Config = Config::from_file(install_profile).unwrap_or_else(|err| { eprintln!("Cannot load '{install_profile}': {}", err.to_string()); @@ -26,6 +27,11 @@ fn main() { println!("{} {}", count.bold(), name.bold()); for step in stage.steps.as_ref().unwrap() { + if *dry_run { + println!("{}", fmt_step(step, &Ok(false))); + continue; + } + let step_result = install::run_step(&step, &stage.base_path); println!("{}", fmt_step(step, &step_result)); diff --git a/src/util.rs b/src/util.rs index 9c0ed1c..25b5c67 100644 --- a/src/util.rs +++ b/src/util.rs @@ -7,7 +7,7 @@ pub const EXIT_INSTALL_FAILED: i32 = 2; // todo: figure out a better way of doing this pub fn str_step(step: &Step) -> String { match step { - Step::Link(path) => format!("Link {} to {}", &path.from, &path.to), + Step::Link(path) => format!("Link {} to {} (symbolic: {})", &path.from, &path.to, &path.symbolic.unwrap_or(false)), Step::Copy(path) => format!("Copy {} to {}", &path.from, &path.to), Step::Shell(command) => format!("Run {}", command), } |
