crates-enum 0.0.10

Process crates.io metadata CSV
Documentation
use anyhow::Result;
use git2::{Cred, RemoteCallbacks, Repository, FetchOptions, build::RepoBuilder};
use std::path::PathBuf;
use url::Url;

pub struct Git<'a> {
    builder: RepoBuilder<'a>,
}

impl Git<'_> {
    pub fn init(username: &str) -> Git {
        let mut callbacks = RemoteCallbacks::new();
        let mut fo = FetchOptions::new();
        let mut builder = RepoBuilder::new();

        callbacks.credentials(|_url, _username_from_url, _allowed_types| {
            Cred::ssh_key_from_agent(username)
        });
        fo.remote_callbacks(callbacks);
        builder.fetch_options(fo);

        Git { builder }
    }

    pub fn is_repo(path: &PathBuf) -> bool {
        Repository::open(path).is_ok()
    }

    pub fn clone(&mut self, url: &Url, path: &PathBuf) -> Result<()> {
        println!("Cloning {}", url.as_str());
        // Clone the project.
        self.builder.clone( &url.as_str(), path,)?;
        Ok(())
    }
}