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 --- Cargo.lock | 179 +++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 3 + src/main.rs | 47 ++++++++++++-- src/state.rs | 24 +++++++ templates/index.html | 5 ++ 5 files changed, 252 insertions(+), 6 deletions(-) create mode 100644 src/state.rs diff --git a/Cargo.lock b/Cargo.lock index 02edbcc..434044d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -54,6 +54,15 @@ dependencies = [ "opaque-debug", ] +[[package]] +name = "ansi_term" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b" +dependencies = [ + "winapi", +] + [[package]] name = "anyhow" version = "1.0.40" @@ -348,6 +357,17 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "065374052e7df7ee4047b1160cca5e1467a12351a40b3da123c870ba0b8eda2a" +[[package]] +name = "atty" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" +dependencies = [ + "hermit-abi", + "libc", + "winapi", +] + [[package]] name = "autocfg" version = "1.0.1" @@ -464,6 +484,9 @@ name = "cc" version = "1.0.67" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e3c69b077ad434294d3ce9f1f6143a2a4b89a8a2d54ef813d85003a4fd1137fd" +dependencies = [ + "jobserver", +] [[package]] name = "cfg-if" @@ -500,6 +523,21 @@ dependencies = [ "generic-array", ] +[[package]] +name = "clap" +version = "2.33.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "37e58ac78573c40708d45522f0d80fa2f01cc4f9b4e2bf749807255454312002" +dependencies = [ + "ansi_term", + "atty", + "bitflags", + "strsim", + "textwrap", + "unicode-width", + "vec_map", +] + [[package]] name = "concurrent-queue" version = "1.2.2" @@ -647,6 +685,16 @@ version = "2.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f7531096570974c3a9dcf9e4b8e1cede1ec26cf5046219fb3b9d897503b9be59" +[[package]] +name = "eyre" +version = "0.6.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "221239d1d5ea86bf5d6f91c9d6bc3646ffe471b08ff9b0f91c44f115ac969d2b" +dependencies = [ + "indenter", + "once_cell", +] + [[package]] name = "fastrand" version = "1.4.0" @@ -800,6 +848,21 @@ dependencies = [ "polyval", ] +[[package]] +name = "git2" +version = "0.13.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b483c6c2145421099df1b4efd50e0f6205479a072199460eff852fa15e5603c7" +dependencies = [ + "bitflags", + "libc", + "libgit2-sys", + "log", + "openssl-probe", + "openssl-sys", + "url", +] + [[package]] name = "gityeet" version = "0.1.0" @@ -807,6 +870,9 @@ dependencies = [ "askama", "askama_tide", "async-std", + "clap", + "eyre", + "git2", "tide", ] @@ -920,6 +986,12 @@ dependencies = [ "unicode-normalization", ] +[[package]] +name = "indenter" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce23b50ad8242c51a442f3ff322d56b02f08852c77e4c0b4d3fd684abc89c683" + [[package]] name = "infer" version = "0.2.3" @@ -941,6 +1013,15 @@ version = "0.4.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dd25036021b0de88a0aff6b850051563c6516d0bf53f8638938edbb9de732736" +[[package]] +name = "jobserver" +version = "0.1.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "972f5ae5d1cb9c6ae417789196c803205313edde988685da5e3aae0827b9e7fd" +dependencies = [ + "libc", +] + [[package]] name = "js-sys" version = "0.3.50" @@ -984,6 +1065,46 @@ version = "0.2.93" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9385f66bf6105b241aa65a61cb923ef20efc665cb9f9bb50ac2f0c4b7f378d41" +[[package]] +name = "libgit2-sys" +version = "0.12.19+1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f322155d574c8b9ebe991a04f6908bb49e68a79463338d24a43d6274cb6443e6" +dependencies = [ + "cc", + "libc", + "libssh2-sys", + "libz-sys", + "openssl-sys", + "pkg-config", +] + +[[package]] +name = "libssh2-sys" +version = "0.2.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e0186af0d8f171ae6b9c4c90ec51898bad5d08a2d5e470903a50d9ad8959cbee" +dependencies = [ + "cc", + "libc", + "libz-sys", + "openssl-sys", + "pkg-config", + "vcpkg", +] + +[[package]] +name = "libz-sys" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "602113192b08db8f38796c4e85c39e960c145965140e918018bcde1952429655" +dependencies = [ + "cc", + "libc", + "pkg-config", + "vcpkg", +] + [[package]] name = "log" version = "0.4.14" @@ -1060,6 +1181,25 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5" +[[package]] +name = "openssl-probe" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77af24da69f9d9341038eba93a073b1fdaaa1b788221b00a69bce9e762cb32de" + +[[package]] +name = "openssl-sys" +version = "0.9.61" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "313752393519e876837e09e1fa183ddef0be7735868dced3196f4472d536277f" +dependencies = [ + "autocfg", + "cc", + "libc", + "pkg-config", + "vcpkg", +] + [[package]] name = "parking" version = "2.0.0" @@ -1110,6 +1250,12 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" +[[package]] +name = "pkg-config" +version = "0.3.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3831453b3449ceb48b6d9c7ad7c96d5ea673e9b470a1dc578c2ce6521230884c" + [[package]] name = "polling" version = "2.0.3" @@ -1481,6 +1627,12 @@ version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "213701ba3370744dcd1a12960caa4843b3d68b4d1c0a5d575e0d65b2ee9d16c0" +[[package]] +name = "strsim" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" + [[package]] name = "subtle" version = "2.4.0" @@ -1510,6 +1662,15 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "55937e1799185b12863d447f42597ed69d9928686b8d88a1df17376a097d8369" +[[package]] +name = "textwrap" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060" +dependencies = [ + "unicode-width", +] + [[package]] name = "thiserror" version = "1.0.24" @@ -1650,6 +1811,12 @@ dependencies = [ "tinyvec", ] +[[package]] +name = "unicode-width" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9337591893a19b88d8d87f2cec1e73fad5cdfd10e5a6f349f498ad6ea2ffb1e3" + [[package]] name = "unicode-xid" version = "0.2.1" @@ -1689,12 +1856,24 @@ dependencies = [ "sval", ] +[[package]] +name = "vcpkg" +version = "0.2.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cbdbff6266a24120518560b5dc983096efb98462e51d0d68169895b237be3e5d" + [[package]] name = "vec-arena" version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "34b2f665b594b07095e3ac3f718e13c2197143416fae4c5706cffb7b1af8d7f1" +[[package]] +name = "vec_map" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191" + [[package]] name = "version_check" version = "0.9.3" diff --git a/Cargo.toml b/Cargo.toml index 32772ee..a0607dd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,4 +10,7 @@ edition = "2018" askama = { version = "0.10.5", features = ["with-tide"] } askama_tide = "0.13.0" async-std = { version = "1.8.0", features = ["attributes"] } +clap = "2.33.3" +eyre = "0.6.5" +git2 = "0.13.18" tide = "0.16.0" 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 }) + } +} diff --git a/templates/index.html b/templates/index.html index 5740f8b..00ccd55 100644 --- a/templates/index.html +++ b/templates/index.html @@ -9,5 +9,10 @@

hi this is gityeet

+
    + {% for repo in state.data %} +
  • {{ repo.path().display() }}
  • + {% endfor %} +
\ No newline at end of file -- cgit v1.2.3