1extern crate fs_extra;
2extern crate regex;
3extern crate tempfile;
4
5use std::{fs, vec};
6use std::path::{Path, PathBuf};
7
8pub mod errors;
9pub mod git;
10pub mod git_url_parser;
11
12pub struct DownloadOptions {
13 pub target_files: Option<Vec<String>>,
14 pub dest_path: String
15}
16
17pub fn download(url: &str, opts: DownloadOptions) -> Result<(), errors::GitDownError> {
18 let git_url = git_url_parser::parse_url(&url);
19 let git_dir = git::sparse_checkout(git_url.unwrap().clone(), opts.target_files.unwrap_or(vec!["./".to_string()]));
20
21 let dest_path = Path::new(&opts.dest_path);
22
23 move_files(&git_dir.unwrap().contents(), &dest_path);
24 Ok(())
25}
26
27
28fn move_files(source_paths: &Vec<PathBuf>, dest_path: &Path) {
29 let options = fs_extra::dir::CopyOptions::new();
30
31 if !dest_path.exists() {
32 fs::create_dir(dest_path).expect("Cannot create destination directory");
33 }
34
35 let dest = dest_path.to_str().unwrap().to_string();
36 let mut sources: Vec<String> = Vec::new();
37
38 for path in source_paths {
39 sources.push(path.to_str().unwrap().to_string());
40 }
41
42 fs_extra::move_items(&sources, &dest, &options)
43 .expect(&format!("Failed to move files to {}", dest));
44}