aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorLeonardo Bishop <me@leonardobishop.com>2024-12-25 15:17:12 +0000
committerLeonardo Bishop <me@leonardobishop.com>2024-12-25 15:17:12 +0000
commitde9c68b085034c897f26ce22fa05bd2601e79373 (patch)
tree9996862988168ba7fa6e561bc2e5133072356c0e /src
parentffa08735aee2d69e17bce5c29a7ef96cc7e2233f (diff)
Version 0.2.0
Diffstat (limited to 'src')
-rw-r--r--src/cli.rs3
-rw-r--r--src/config.rs11
-rw-r--r--src/install.rs31
-rw-r--r--src/main.rs6
-rw-r--r--src/util.rs2
5 files changed, 42 insertions, 11 deletions
diff --git a/src/cli.rs b/src/cli.rs
index dc53357..0b4962a 100644
--- a/src/cli.rs
+++ b/src/cli.rs
@@ -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),
}