aboutsummaryrefslogtreecommitdiff
path: root/dotup_cli/src/commands/mv.rs
blob: aae2715a44c6199b2ec1306fe952936b21cb395f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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<PathBuf>,
}

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(())
}