diff options
Diffstat (limited to 'dotup_cli/src/commands/mv.rs')
| -rw-r--r-- | dotup_cli/src/commands/mv.rs | 53 |
1 files changed, 53 insertions, 0 deletions
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 @@ | |||
| 1 | use std::path::{Path, PathBuf}; | ||
| 2 | |||
| 3 | use super::prelude::*; | ||
| 4 | |||
| 5 | /// Install links. (Creates symlinks). | ||
| 6 | /// | ||
| 7 | /// Installing a link will create the necessary directories. | ||
| 8 | /// If a file or directory already exists at the location a link would be installed this command will fail. | ||
| 9 | #[derive(Parser)] | ||
| 10 | pub struct Opts { | ||
| 11 | /// The files/directories to move | ||
| 12 | #[clap(min_values = 2)] | ||
| 13 | paths: Vec<PathBuf>, | ||
| 14 | } | ||
| 15 | |||
| 16 | pub fn main(config: Config, opts: Opts) -> anyhow::Result<()> { | ||
| 17 | let mut depot = utils::read_depot(&config.archive_path)?; | ||
| 18 | |||
| 19 | let (sources, destination) = match opts.paths.as_slice() { | ||
| 20 | [source, destination] => {} | ||
| 21 | [sources @ .., destination] => { | ||
| 22 | let mut curr_destination = destination.to_owned(); | ||
| 23 | for source in sources { | ||
| 24 | let filename = match source.file_name() { | ||
| 25 | Some(filename) => filename, | ||
| 26 | None => { | ||
| 27 | log::warn!("Ignoring '{}', unknown file name", source.display()); | ||
| 28 | continue; | ||
| 29 | } | ||
| 30 | }; | ||
| 31 | curr_destination.push(filename); | ||
| 32 | std::fs::rename(source, &curr_destination)?; | ||
| 33 | if let Some(id) = depot.get_link_id_by_path(&source) { | ||
| 34 | depot.rename_link(id, &curr_destination); | ||
| 35 | } | ||
| 36 | curr_destination.pop(); | ||
| 37 | } | ||
| 38 | } | ||
| 39 | _ => unreachable!(), | ||
| 40 | }; | ||
| 41 | |||
| 42 | utils::write_depot(&depot)?; | ||
| 43 | |||
| 44 | Ok(()) | ||
| 45 | } | ||
| 46 | |||
| 47 | fn rename(depot: &mut Depot, source: &Path, destination: &Path) -> anyhow::Result<()> { | ||
| 48 | std::fs::rename(source, &destination)?; | ||
| 49 | if let Some(id) = depot.get_link_id_by_path(&source) { | ||
| 50 | depot.rename_link(id, &destination); | ||
| 51 | } | ||
| 52 | Ok(()) | ||
| 53 | } | ||
