aboutsummaryrefslogtreecommitdiff
path: root/dotup_cli/src/commands
diff options
context:
space:
mode:
Diffstat (limited to 'dotup_cli/src/commands')
-rw-r--r--dotup_cli/src/commands/mod.rs1
-rw-r--r--dotup_cli/src/commands/mv.rs53
2 files changed, 54 insertions, 0 deletions
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 @@
1pub mod init; 1pub mod init;
2pub mod install; 2pub mod install;
3pub mod link; 3pub mod link;
4pub mod mv;
4pub mod status; 5pub mod status;
5pub mod uninstall; 6pub mod uninstall;
6pub mod unlink; 7pub 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 @@
1use std::path::{Path, PathBuf};
2
3use 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)]
10pub struct Opts {
11 /// The files/directories to move
12 #[clap(min_values = 2)]
13 paths: Vec<PathBuf>,
14}
15
16pub 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
47fn 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}