From 0992c36733f58750da93921041424fd09f0158ed Mon Sep 17 00:00:00 2001 From: diogo464 Date: Tue, 8 Feb 2022 09:19:19 +0000 Subject: snapshot before removal --- dotup_cli/src/commands/mod.rs | 1 + dotup_cli/src/commands/mv.rs | 53 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 dotup_cli/src/commands/mv.rs (limited to 'dotup_cli/src/commands') diff --git a/dotup_cli/src/commands/mod.rs b/dotup_cli/src/commands/mod.rs index 94dc3fd..bd92599 100644 --- a/dotup_cli/src/commands/mod.rs +++ b/dotup_cli/src/commands/mod.rs @@ -1,6 +1,7 @@ pub mod init; pub mod install; pub mod link; +pub mod mv; pub mod status; pub mod uninstall; pub mod unlink; diff --git a/dotup_cli/src/commands/mv.rs b/dotup_cli/src/commands/mv.rs new file mode 100644 index 0000000..aae2715 --- /dev/null +++ b/dotup_cli/src/commands/mv.rs @@ -0,0 +1,53 @@ +use std::path::{Path, PathBuf}; + +use super::prelude::*; + +/// Install links. (Creates symlinks). +/// +/// Installing a link will create the necessary directories. +/// If a file or directory already exists at the location a link would be installed this command will fail. +#[derive(Parser)] +pub struct Opts { + /// The files/directories to move + #[clap(min_values = 2)] + paths: Vec, +} + +pub fn main(config: Config, opts: Opts) -> anyhow::Result<()> { + let mut depot = utils::read_depot(&config.archive_path)?; + + let (sources, destination) = match opts.paths.as_slice() { + [source, destination] => {} + [sources @ .., destination] => { + let mut curr_destination = destination.to_owned(); + for source in sources { + let filename = match source.file_name() { + Some(filename) => filename, + None => { + log::warn!("Ignoring '{}', unknown file name", source.display()); + continue; + } + }; + curr_destination.push(filename); + std::fs::rename(source, &curr_destination)?; + if let Some(id) = depot.get_link_id_by_path(&source) { + depot.rename_link(id, &curr_destination); + } + curr_destination.pop(); + } + } + _ => unreachable!(), + }; + + utils::write_depot(&depot)?; + + Ok(()) +} + +fn rename(depot: &mut Depot, source: &Path, destination: &Path) -> anyhow::Result<()> { + std::fs::rename(source, &destination)?; + if let Some(id) = depot.get_link_id_by_path(&source) { + depot.rename_link(id, &destination); + } + Ok(()) +} -- cgit