summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZhongheng Liu <z.liu@outlook.com.gr>2024-12-25 01:02:53 +0200
committerZhongheng Liu <z.liu@outlook.com.gr>2024-12-25 01:02:53 +0200
commit59319c4a0127766028aaf6f8404e0d3e239beba2 (patch)
treec44411d71efcbb34849e17edbbeae5875b4f7be7
parent6590952d7716678b47a1bed823da03e62a0ea0fa (diff)
downloadgit_service-59319c4a0127766028aaf6f8404e0d3e239beba2.tar.gz
git_service-59319c4a0127766028aaf6f8404e0d3e239beba2.tar.bz2
git_service-59319c4a0127766028aaf6f8404e0d3e239beba2.zip
feat: git repo finalise and cgitrepo
cgit: added code for cgitrepos file parsing and adding git: added dynamic base path by reading config struct
-rw-r--r--src/cgit_helper.rs54
-rw-r--r--src/git_tools.rs43
-rw-r--r--src/main.rs38
3 files changed, 100 insertions, 35 deletions
diff --git a/src/cgit_helper.rs b/src/cgit_helper.rs
index f55c6d4..76de5b3 100644
--- a/src/cgit_helper.rs
+++ b/src/cgit_helper.rs
@@ -1,8 +1,50 @@
+use std::fs::{self, read_to_string};
+pub struct CGitRepoInfo {
+ pub section: String,
+ pub url: String,
+ pub path: String,
+ pub owner: String,
+ pub description: String,
+}
+pub fn cgit_add_repo(cgit_repos_file_path: String, info: CGitRepoInfo) {
+ let mut lines: Vec<String> = vec![];
+ read_to_string(&cgit_repos_file_path)
+ .expect("file read error.")
+ .lines()
+ .for_each(|line| {
+ if line == "\n" || line.is_empty() || line == " " || line.starts_with('#') {
+ return;
+ }
+ lines.push(String::from(line));
+ });
+ let mut target_line_index: Option<usize> = None;
+ for (i, l) in lines.iter().enumerate() {
+ // println!("DEBUG: PARSING: {}", l);
+ let (header, value) = l.split_once("=").expect("expected full line");
+ if header == "section" && value == info.section {
+ // we've found our desired section
+ target_line_index = Some(i + 1);
+ }
+ }
+ let mut new_lines = vec![
+ format!("repo.url={}", info.url),
+ format!("repo.path={}", info.path),
+ format!("repo.desc={}", info.description),
+ format!("repo.owner={}", info.owner),
+ ];
+ if Option::is_none(&target_line_index) {
+ // there is no section, shall create new
+ new_lines.insert(0, format!("section={}", info.section));
+ lines.append(&mut new_lines);
+ }
+ for (index, new_line) in new_lines.iter().enumerate() {
+ lines.insert(
+ target_line_index.expect("should expect target line index to exist") + index,
+ new_line.to_string(),
+ );
+ }
+ let final_text = lines.join("\n");
+ // println!("DEBUG:: Writing following to cgitrepos: \n{}", final_text);
-pub fn cgit_add_repo(cgit_repos_file_path: String, section_name: String) {
- /* TODO
- * Change configuration in /path/to/cgitrepos
- * to make visible the new repository
- * maybe let user choose if they want repo to be visible or not
- * */
+ fs::write(&cgit_repos_file_path, final_text).expect("cgit repo write err");
}
diff --git a/src/git_tools.rs b/src/git_tools.rs
index 311aa93..878cf7b 100644
--- a/src/git_tools.rs
+++ b/src/git_tools.rs
@@ -2,6 +2,8 @@ use core::panic;
use git2::{Repository, RepositoryInitOptions};
+use crate::{cgit_helper::cgit_add_repo, read_config::Config};
+
pub enum RepoCreateError {
Ownership,
Path,
@@ -9,11 +11,12 @@ pub enum RepoCreateError {
Internal,
}
-fn fmt_repo_name_dotgit(raw_name: &str) -> Result<String, RepoCreateError> {
+fn fmt_repo_name_dotgit(raw_name: &str) -> Result<(String, String), RepoCreateError> {
if raw_name.ends_with(".git") {
- Ok(raw_name.to_string())
+ let no_suffix_name = &raw_name.to_string()[..raw_name.chars().count() - 4];
+ Ok((no_suffix_name.to_string(), raw_name.to_string()))
} else {
- Ok(raw_name.to_owned() + ".git")
+ Ok((raw_name.to_string(), raw_name.to_string() + ".git"))
}
}
fn check_owner_against_config(_owner: &str) -> bool {
@@ -23,25 +26,25 @@ fn check_owner_against_config(_owner: &str) -> bool {
* to see if authenticated for managing git repos
*/
}
-pub fn make_repo(raw_repo_name: &str, owner_text: &str) -> Result<Repository, RepoCreateError> {
- let path = match home::home_dir() {
- Some(home_path) => home_path.display().to_string(),
-
- None => todo!(),
- };
- let git_base_path = path + "/libgit_test_repos";
+pub fn make_repo(
+ raw_repo_name: &str,
+ owner_text: &str,
+ section: &str,
+ description: &str,
+ config: Config,
+) -> Result<Repository, RepoCreateError> {
if !(check_owner_against_config(owner_text)) {
return Err(RepoCreateError::Ownership);
}
- let repo_name: Result<String, RepoCreateError> = fmt_repo_name_dotgit(raw_repo_name);
- let name = repo_name.unwrap_or_else(|error| {
+ let repo_name: Result<(String, String), RepoCreateError> = fmt_repo_name_dotgit(raw_repo_name);
+ let (no_suffix, with_suffix) = repo_name.unwrap_or_else(|error| {
if let RepoCreateError::RepoNameParse = error {
panic!("RepoNameParseError");
} else {
panic!("Unknown error");
}
});
- let git_repo_path: String = git_base_path + "/" + &name;
+ let git_repo_path: String = config.git_host_dir_path + "/" + &with_suffix;
let mut options = RepositoryInitOptions::new();
options.no_reinit(true).bare(true);
@@ -55,7 +58,17 @@ pub fn make_repo(raw_repo_name: &str, owner_text: &str) -> Result<Repository, Re
}
};
/* TODO
- * We want to change the config file to append http.receivepack = true
- * */
+ * We want to change the config file to append http.receivepack = true
+ * */
+ cgit_add_repo(
+ config.cgitrepos_file_path,
+ crate::cgit_helper::CGitRepoInfo {
+ section: String::from(section),
+ url: no_suffix,
+ path: git_repo_path,
+ owner: owner_text.to_string(),
+ description: description.to_string(),
+ },
+ );
Ok(repo)
}
diff --git a/src/main.rs b/src/main.rs
index baa8c8b..6852e55 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,31 +1,41 @@
-use core::panic;
mod cgit_helper;
mod git_tools;
+mod pgp_tools;
mod read_config;
-use cgit_helper::cgit_add_repo;
+
+#[cfg(test)]
+mod test;
+
+use core::panic;
use git_tools::make_repo;
use read_config::{get_config, Config};
use serde::Deserialize;
-use warp::Filter;
#[derive(Debug, Deserialize)]
struct MakeRepoRequest {
repo_name: String,
author: String,
- sig_text: String,
+ section: String,
+ description: String,
}
-fn make_repo_request_handler(config: Config) {
+fn make_repo_request_handler(config: Config, req: MakeRepoRequest) {
println!("Hello, world!");
- let _repository = match make_repo("any_test.git", "z.liu@outlook.com.gr") {
+ let _repository = match make_repo(
+ &req.repo_name,
+ &req.author,
+ &req.section,
+ &req.description,
+ config,
+ ) {
Ok(repo) => repo,
Err(_e) => panic!("Something went wrong during repo init"),
};
- let section_name = String::from("custom");
- cgit_add_repo(config.cgitrepos_file_path, section_name);
}
-#[tokio::main]
-async fn main() {
- let _config: Config = get_config("./config.local.json".to_string());
- let ping = warp::path!("ping").map(|| "pong");
- warp::serve(ping).run(([127, 0, 0, 1], 7070)).await;
- //make_repo_request_handler(config);
+fn main() {
+ let config: Config = get_config("./config.local.json".to_string());
+ make_repo_request_handler(config, MakeRepoRequest {
+ repo_name: "hello-world".to_string(),
+ author: "joe mama".to_string(),
+ section: "skibidi".to_string(),
+ description: "foo bar".to_string(),
+ });
}