thot_cli/commands/project/
move.rs

1use crate::result::Result;
2use clap::Args;
3use std::env;
4use std::path::PathBuf;
5use thot_core::result::{Error as CoreError, ProjectError as CoreProjectError};
6use thot_local::common::canonicalize_path;
7use thot_local::project::project;
8use thot_local::system::projects;
9
10#[derive(Debug, Args)]
11pub struct MoveArgs {
12    #[clap(parse(from_os_str))]
13    to: PathBuf,
14
15    #[clap(long, parse(from_os_str))]
16    from: Option<PathBuf>,
17}
18
19/// Move a Thot project to a new location.
20pub fn main(args: MoveArgs, verbose: bool) -> Result {
21    // parse to and from args
22    let from = match args.from {
23        Some(path) => match canonicalize_path(path) {
24            Ok(path) => path,
25            Err(err) => return Err(err.into()),
26        },
27        None => match env::current_dir() {
28            Ok(dir) => match project::project_root_path(dir.as_path()) {
29                Ok(path) => path,
30                Err(err) => return Err(err.into()),
31            },
32            Err(err) => return Err(err.into()),
33        },
34    };
35
36    let prj = projects::project_by_path(from.as_path())?;
37    let rid = match prj {
38        None => {
39            return Err(
40                CoreError::ProjectError(CoreProjectError::NotRegistered(None, Some(from))).into(),
41            )
42        }
43        Some(p) => p.rid,
44    };
45
46    let to = args.to;
47
48    if verbose {
49        println!("Moving project located at {:?} to {:?}", from, to);
50    }
51
52    // move project
53    if let Err(err) = project::mv(&rid, to.as_path()) {
54        return Err(err.into());
55    }
56
57    Ok(())
58}
59
60#[cfg(test)]
61#[path = "./move_test.rs"]
62mod move_test;