Compare commits
7 commits
e45c5f380a
...
5ae9885cfe
Author | SHA1 | Date | |
---|---|---|---|
Bruno BELANYI | 5ae9885cfe | ||
Bruno BELANYI | 99d368e665 | ||
Bruno BELANYI | b4f6735b31 | ||
Bruno BELANYI | 44ae16b948 | ||
Bruno BELANYI | a58f712181 | ||
Bruno BELANYI | 4ceeb5e4fe | ||
Bruno BELANYI | 21cd680b5d |
34
Cargo.lock
generated
34
Cargo.lock
generated
|
@ -231,12 +231,6 @@ dependencies = [
|
|||
"generic-array",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "dtoa"
|
||||
version = "0.4.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "56899898ce76aaf4a0f24d914c97ea6ed976d42fec6ad33fcbb0a1103e07b2b0"
|
||||
|
||||
[[package]]
|
||||
name = "filetime"
|
||||
version = "0.2.14"
|
||||
|
@ -465,12 +459,6 @@ version = "0.2.91"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8916b1f6ca17130ec6568feccee27c156ad12037880833a3b842a823236502e7"
|
||||
|
||||
[[package]]
|
||||
name = "linked-hash-map"
|
||||
version = "0.5.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7fb9b38af92608140b86b693604b9ffcc5824240a484d1ecd4795bacb2fe88f3"
|
||||
|
||||
[[package]]
|
||||
name = "log"
|
||||
version = "0.3.9"
|
||||
|
@ -498,7 +486,6 @@ dependencies = [
|
|||
"rocket",
|
||||
"rocket_contrib",
|
||||
"serde",
|
||||
"serde_yaml",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -864,18 +851,6 @@ dependencies = [
|
|||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_yaml"
|
||||
version = "0.8.17"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "15654ed4ab61726bf918a39cb8d98a2e2995b002387807fa6ba58fdf7f59bb23"
|
||||
dependencies = [
|
||||
"dtoa",
|
||||
"linked-hash-map",
|
||||
"serde",
|
||||
"yaml-rust",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "sha2"
|
||||
version = "0.9.3"
|
||||
|
@ -1129,15 +1104,6 @@ dependencies = [
|
|||
"winapi-build",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "yaml-rust"
|
||||
version = "0.4.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "56c1936c4cc7a1c9ab21a1ebb602eb942ba868cbd44a99cb7cdc5892335e1c85"
|
||||
dependencies = [
|
||||
"linked-hash-map",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "yansi"
|
||||
version = "0.5.0"
|
||||
|
|
|
@ -14,4 +14,3 @@ log = "0.4.14"
|
|||
rocket = "0.4.7"
|
||||
rocket_contrib = { version = "0.4.7", features = [ "json" ] }
|
||||
serde = { version = "1.0.125", features = [ "derive" ] }
|
||||
serde_yaml = "0.8.17"
|
||||
|
|
55
src/job.rs
55
src/job.rs
|
@ -8,7 +8,6 @@ use anyhow::bail;
|
|||
use log::info;
|
||||
|
||||
use crate::gitea::Repository;
|
||||
use crate::settings::{GlobalSettings, RepoUrl};
|
||||
|
||||
pub(crate) struct Job {
|
||||
repo: Repository,
|
||||
|
@ -92,9 +91,7 @@ impl Job {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
/// Can return Ok(None) if the .lohr file didn't exist, but no significant error occured
|
||||
fn read_remotes_from_lohr_file(&self) -> anyhow::Result<Option<Vec<RepoUrl>>> {
|
||||
// try to read .lohr file from bare repo (hence the git show sorcery)
|
||||
fn get_remotes(&self) -> anyhow::Result<String> {
|
||||
let output = Command::new("git")
|
||||
.arg("-C")
|
||||
.arg(format!("{}", self.local_path.as_ref().unwrap().display()))
|
||||
|
@ -104,11 +101,6 @@ impl Job {
|
|||
|
||||
if !output.status.success() {
|
||||
let error = str::from_utf8(&output.stderr)?;
|
||||
|
||||
// this error case is okay, .lohr just doesn't exist
|
||||
if error.contains("does not exist in 'HEAD'") {
|
||||
return Ok(None);
|
||||
} else {
|
||||
let code = output
|
||||
.status
|
||||
.code()
|
||||
|
@ -121,40 +113,13 @@ impl Job {
|
|||
error
|
||||
);
|
||||
}
|
||||
|
||||
Ok(String::from_utf8(output.stdout)?)
|
||||
}
|
||||
|
||||
let output = String::from_utf8(output.stdout)?;
|
||||
|
||||
Ok(Some(output.lines().map(String::from).collect()))
|
||||
}
|
||||
|
||||
fn get_remotes(&self, config: &GlobalSettings) -> anyhow::Result<Vec<RepoUrl>> {
|
||||
let local_path = self.local_path.as_ref().unwrap();
|
||||
|
||||
let stem_to_repo = |stem: &RepoUrl| -> RepoUrl {
|
||||
let mut res = stem.clone();
|
||||
if !res.ends_with('/') {
|
||||
res.push('/');
|
||||
};
|
||||
res.push_str(local_path.file_name().unwrap().to_str().unwrap());
|
||||
res
|
||||
};
|
||||
|
||||
// use either .lohr file or default remotes from config
|
||||
let mut remotes = match self.read_remotes_from_lohr_file()? {
|
||||
Some(remotes) if !remotes.is_empty() => remotes,
|
||||
_ => config.default_remotes.iter().map(stem_to_repo).collect(),
|
||||
};
|
||||
|
||||
// additional remotes
|
||||
remotes.append(&mut config.additional_remotes.iter().map(stem_to_repo).collect());
|
||||
|
||||
Ok(remotes)
|
||||
}
|
||||
|
||||
fn update_mirrors(&self, config: &GlobalSettings) -> anyhow::Result<()> {
|
||||
for remote in &self.get_remotes(config)? {
|
||||
info!("Updating mirror {}...", remote);
|
||||
fn update_mirrors(&self) -> anyhow::Result<()> {
|
||||
for remote in self.get_remotes()?.lines() {
|
||||
info!("Updating mirror {}:{}...", remote, self.repo.full_name);
|
||||
|
||||
let output = Command::new("git")
|
||||
.arg("-C")
|
||||
|
@ -183,7 +148,7 @@ impl Job {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn run(&mut self, homedir: &Path, config: &GlobalSettings) -> anyhow::Result<()> {
|
||||
pub(crate) fn run(&mut self, homedir: &Path) -> anyhow::Result<()> {
|
||||
let local_path = homedir.join(&self.repo.full_name);
|
||||
assert!(local_path.is_absolute());
|
||||
self.local_path = Some(local_path);
|
||||
|
@ -194,10 +159,6 @@ impl Job {
|
|||
self.update_repo()?;
|
||||
}
|
||||
|
||||
self.update_mirrors(config)?;
|
||||
|
||||
info!("Done processing job {}!", self.repo.full_name);
|
||||
|
||||
Ok(())
|
||||
self.update_mirrors()
|
||||
}
|
||||
}
|
||||
|
|
29
src/main.rs
29
src/main.rs
|
@ -1,7 +1,6 @@
|
|||
#![feature(proc_macro_hygiene, decl_macro)]
|
||||
|
||||
use std::env;
|
||||
use std::fs::File;
|
||||
use std::path::PathBuf;
|
||||
use std::sync::{
|
||||
mpsc::{channel, Receiver, Sender},
|
||||
|
@ -20,15 +19,10 @@ use gitea::GiteaWebHook;
|
|||
mod job;
|
||||
use job::Job;
|
||||
|
||||
mod settings;
|
||||
use settings::GlobalSettings;
|
||||
|
||||
struct JobSender(Mutex<Sender<Job>>);
|
||||
|
||||
#[post("/", data = "<payload>")]
|
||||
fn gitea_webhook(payload: Json<GiteaWebHook>, sender: State<JobSender>) -> Status {
|
||||
// TODO: validate Gitea signature
|
||||
|
||||
{
|
||||
let sender = sender.0.lock().unwrap();
|
||||
let repo = &payload.repository;
|
||||
|
@ -38,44 +32,29 @@ fn gitea_webhook(payload: Json<GiteaWebHook>, sender: State<JobSender>) -> Statu
|
|||
Status::Ok
|
||||
}
|
||||
|
||||
fn repo_updater(rx: Receiver<Job>, homedir: PathBuf, config: GlobalSettings) {
|
||||
fn repo_updater(rx: Receiver<Job>, homedir: PathBuf) {
|
||||
loop {
|
||||
let mut job = rx.recv().unwrap();
|
||||
|
||||
if let Err(err) = job.run(&homedir, &config) {
|
||||
if let Err(err) = job.run(&homedir) {
|
||||
error!("couldn't process job: {}", err);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_config(mut path: PathBuf) -> anyhow::Result<GlobalSettings> {
|
||||
path.push("lohr-config");
|
||||
path.set_extension("yaml");
|
||||
let config = if let Ok(file) = File::open(path.as_path()) {
|
||||
serde_yaml::from_reader(file)?
|
||||
} else {
|
||||
Default::default()
|
||||
};
|
||||
Ok(config)
|
||||
}
|
||||
|
||||
fn main() -> anyhow::Result<()> {
|
||||
fn main() {
|
||||
let (sender, receiver) = channel();
|
||||
|
||||
let homedir = env::var("LOHR_HOME").unwrap_or_else(|_| "./".to_string());
|
||||
let homedir: PathBuf = homedir.into();
|
||||
let homedir = homedir.canonicalize().expect("LOHR_HOME isn't valid!");
|
||||
|
||||
let config = parse_config(homedir.clone())?;
|
||||
|
||||
thread::spawn(move || {
|
||||
repo_updater(receiver, homedir, config);
|
||||
repo_updater(receiver, homedir);
|
||||
});
|
||||
|
||||
rocket::ignite()
|
||||
.mount("/", routes![gitea_webhook])
|
||||
.manage(JobSender(Mutex::new(sender)))
|
||||
.launch();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
@ -1,13 +0,0 @@
|
|||
use serde::Deserialize;
|
||||
|
||||
pub(crate) type RepoUrl = String; // FIXME: probably needs a better type than this
|
||||
|
||||
#[derive(Default, Deserialize)]
|
||||
pub(crate) struct GlobalSettings {
|
||||
/// List of remote stems to use when no `.lohr` file is found
|
||||
#[serde(default)]
|
||||
pub default_remotes: Vec<RepoUrl>,
|
||||
/// List of remote stems to use for every repository
|
||||
#[serde(default)]
|
||||
pub additional_remotes: Vec<RepoUrl>,
|
||||
}
|
Loading…
Reference in a new issue