full_stop/
deploy.rs

1use std::path::PathBuf;
2
3pub fn get_env(name: &str) -> crate::Result<Option<String>> {
4    let path = match name {
5        "CONFIG_DIR" => dirs::config_dir(),
6        "DATA_DIR" => dirs::data_dir(),
7        "DATA_LOCAL_DIR" => dirs::data_local_dir(),
8        "EXECUTABLE_DIR" => dirs::executable_dir(),
9        _ => None,
10    }
11    .and_then(|path| path.to_str().map(ToString::to_string));
12
13    Ok(path)
14}
15
16#[derive(Debug)]
17pub struct Source {
18    pub path: PathBuf,
19    pub skip: usize,
20}
21
22#[derive(Debug)]
23pub struct Deploy {
24    pub sources: Vec<Source>,
25    pub destination: PathBuf,
26}
27
28impl Deploy {
29    pub fn from_config(module: crate::config::Module) -> crate::Result<Self> {
30        let mut sources = Vec::new();
31        for include in module.includes {
32            for path in glob::glob(&include.glob)? {
33                sources.push(Source {
34                    path: path?,
35                    skip: include.prefix_strip.unwrap_or(1),
36                })
37            }
38        }
39
40        let destination = PathBuf::from(
41            shellexpand::full_with_context(&module.destination, dirs::home_dir, get_env)?
42                .to_string(),
43        );
44        Ok(Self {
45            sources,
46            destination,
47        })
48    }
49    pub fn copy(&self) -> crate::Result<()> {
50        for source in &self.sources {
51            let from = &source.path;
52            let to = self
53                .destination
54                .join(from.components().skip(source.skip).collect::<PathBuf>());
55            println!("copy from {} to {}", from.display(), to.display())
56            // std::fs::copy(from, to)?;
57        }
58        Ok(())
59    }
60}