aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorValentin Popov <valentin@popov.link>2024-01-11 21:03:42 +0300
committerValentin Popov <valentin@popov.link>2024-01-11 21:03:42 +0300
commitfdd3eb83229bfe8032f87b570d24d6b91d581b31 (patch)
tree140a844297b04711d77e9069415b2147a91156c9 /src
parent4cbb43c1e2db4c90293e55f2497ae0d0d841c9bd (diff)
downloadgitlab-duplicator-fdd3eb83229bfe8032f87b570d24d6b91d581b31.tar.xz
gitlab-duplicator-fdd3eb83229bfe8032f87b570d24d6b91d581b31.zip
Added a method for cloning a repository
Signed-off-by: Valentin Popov <valentin@popov.link>
Diffstat (limited to 'src')
-rw-r--r--src/main.rs55
1 files changed, 47 insertions, 8 deletions
diff --git a/src/main.rs b/src/main.rs
index 48d1772..aa2f233 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,7 +1,11 @@
#[macro_use]
extern crate dotenv_codegen;
+extern crate git2;
+
+use std::path::Path;
use clap::Parser;
+use git2::{Cred, RemoteCallbacks};
use gitlab::api::{projects, Query};
use gitlab::Gitlab;
use serde::Deserialize;
@@ -21,25 +25,60 @@ struct Args {
/// The URL of the repository to clone
#[arg(short, long)]
- url: String,
+ repo: String,
+
+ /// The username to use for authentication
+ #[arg(long)]
+ username: String,
+
+ /// The password to use for authentication
+ #[arg(long)]
+ password: String,
}
/* Get project info from Gitlab */
fn get_project(url: &str) -> Project {
- let gl_client = Gitlab::new(dotenv!("GITLAB_URL"), dotenv!("GITLAB_TOKEN")).unwrap();
+ let client = Gitlab::new(dotenv!("GITLAB_URL"), dotenv!("GITLAB_TOKEN")).unwrap();
+
+ let endpoint = projects::Project::builder().project(url).build().unwrap();
+ let project: Project = endpoint.query(&client).unwrap();
+
+ project
+}
+
+/* Clone project from Gitlab */
+fn clone_project(repo: &str, path: &str, username: &str, password: &str) {
+ // Callbacks for authentication
+ let mut cb = RemoteCallbacks::new();
+ cb.credentials(|_url, _username_from_url, _allowed_types| {
+ Cred::userpass_plaintext(username, password)
+ });
+
+ // Prepare fetch options
+ let mut fetch_options = git2::FetchOptions::new();
+ fetch_options.remote_callbacks(cb);
+
+ // Prepare builder
+ let mut builder = git2::build::RepoBuilder::new();
+ builder.remote_create(|repo, name, url| repo.remote_with_fetch(name, url, "+refs/*:refs/*"));
- let gl_endpoint = projects::Project::builder().project(url).build().unwrap();
- let gl_project: Project = gl_endpoint.query(&gl_client).unwrap();
+ builder.fetch_options(fetch_options);
+ builder.bare(true);
- gl_project
+ // Clone
+ builder.clone(repo, Path::new(&path)).unwrap();
}
fn main() {
dotenv::dotenv().ok();
let args = Args::parse();
- println!("{:?}", args);
+ let project = get_project(&args.repo);
- let project = get_project(&args.url);
- dbg!(project);
+ clone_project(
+ &project.http_url_to_repo,
+ &args.path,
+ &args.username,
+ &args.password,
+ );
}