From 8c1666c1a64201f8a12e125e3c63a83f4dbf7069 Mon Sep 17 00:00:00 2001 From: Melody Horn Date: Thu, 22 Apr 2021 03:26:33 -0600 Subject: throw together a super basic repo list --- src/main.rs | 47 +++++++++++++++++++++++++++++++++++++++++------ src/state.rs | 24 ++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 6 deletions(-) create mode 100644 src/state.rs (limited to 'src') diff --git a/src/main.rs b/src/main.rs index cb18c22..52dcaee 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,18 +1,53 @@ use askama::Template; +use async_std::sync::{Arc, Mutex}; +use eyre::Result; use tide::prelude::*; +mod state; +use state::State; +use std::ops::Deref; + #[derive(Template)] #[template(path = "index.html")] -struct IndexTemplate; +struct IndexTemplate<'a> { + state: &'a State, +} #[async_std::main] -async fn main() -> tide::Result<()> { - let mut app = tide::new(); +async fn main() -> Result<()> { + let matches = clap::App::new("gityeet") + .arg(clap::Arg::with_name("root") + .help("sets the directory from which repositories will be found") + .required(true)) + .arg(clap::Arg::with_name("port") + .help("sets the port number") + .required(true)) + .get_matches(); + + let root = matches.value_of("root").expect("root is required"); + let port = matches.value_of("port").expect("port is required"); + let port: u16 = port.parse().expect("port was not valid"); + + let state = State::discover(root).await?; + // unfortunately, the State has to be both Send and Sync, but git2::Repository is not Sync + // and even an Arc> isn't Sync if State isn't Sync, it has to be a Mutex to get Sync + // TODO do something smarter for that + let mut app = tide::with_state(Arc::new(Mutex::new(state))); app.at("/").get(index); - app.listen("127.0.0.1:8000").await?; + app.listen(("127.0.0.1", port)).await?; Ok(()) } -async fn index(_: tide::Request<()>) -> tide::Result { - Ok(IndexTemplate.into()) +async fn index(req: tide::Request>>) -> tide::Result { + let state = req.state(); + let state = state.lock_arc().await; + for repo in &state.data { + println!("path: {:?}", repo.path()); + let head = repo.head(); + if let Ok(head) = head { + println!("head name: {:?}", head.name()); + println!("head shorthand: {:?}", head.shorthand()); + } + } + Ok(IndexTemplate { state: state.deref() }.into()) } diff --git a/src/state.rs b/src/state.rs new file mode 100644 index 0000000..1a88929 --- /dev/null +++ b/src/state.rs @@ -0,0 +1,24 @@ +use std::io; +use std::path::Path; + +use async_std::prelude::*; +use async_std::fs::read_dir; + +use git2::Repository; + +pub struct State { + pub data: Vec, +} + +impl State { + pub async fn discover(root: impl AsRef) -> io::Result { + let root = root.as_ref(); + let dir = read_dir(root).await?; + let data = dir + .filter_map(|subdir| { + subdir.ok().and_then(|subdir| Repository::open_bare(&subdir.path()).ok()) + }) + .collect().await; + Ok(Self { data }) + } +} -- cgit v1.2.3