diff options
-rw-r--r-- | Cargo.lock | 179 | ||||
-rw-r--r-- | Cargo.toml | 3 | ||||
-rw-r--r-- | src/main.rs | 47 | ||||
-rw-r--r-- | src/state.rs | 24 | ||||
-rw-r--r-- | templates/index.html | 5 |
5 files changed, 252 insertions, 6 deletions
@@ -55,6 +55,15 @@ dependencies = [ ] [[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" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -349,6 +358,17 @@ 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" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -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" @@ -501,6 +524,21 @@ dependencies = [ ] [[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" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -648,6 +686,16 @@ 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" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -801,12 +849,30 @@ dependencies = [ ] [[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" dependencies = [ "askama", "askama_tide", "async-std", + "clap", + "eyre", + "git2", "tide", ] @@ -921,6 +987,12 @@ dependencies = [ ] [[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" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -942,6 +1014,15 @@ 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" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -985,6 +1066,46 @@ 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" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1061,6 +1182,25 @@ 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" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1111,6 +1251,12 @@ 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" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1482,6 +1628,12 @@ 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" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1511,6 +1663,15 @@ 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" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1651,6 +1812,12 @@ dependencies = [ ] [[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" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1690,12 +1857,24 @@ dependencies = [ ] [[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" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -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<RwLock<State>> 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<Arc<Mutex<State>>>) -> 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<Repository>, +} + +impl State { + pub async fn discover(root: impl AsRef<Path>) -> io::Result<Self> { + 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 @@ </head> <body> <h1>hi this is gityeet</h1> +<ul> + {% for repo in state.data %} + <li>{{ repo.path().display() }}</li> + {% endfor %} +</ul> </body> </html>
\ No newline at end of file |