From 94f254047f79fce6c95ab707abf10a6c65133264 Mon Sep 17 00:00:00 2001 From: kjuulh Date: Sun, 18 Dec 2022 14:47:28 +0100 Subject: [PATCH] with network stats --- Cargo.lock | 11 +++++++ Cargo.toml | 9 ++++- crates/stats/Cargo.toml | 13 ++++++++ crates/stats/src/code.rs | 58 ++++++++++++++++++++++++++++++++ crates/stats/src/lib.rs | 28 ++++++++++++++++ crates/stats/src/network.rs | 66 +++++++++++++++++++++++++++++++++++++ src/main.rs | 5 +-- 7 files changed, 187 insertions(+), 3 deletions(-) create mode 100644 crates/stats/Cargo.toml create mode 100644 crates/stats/src/code.rs create mode 100644 crates/stats/src/lib.rs create mode 100644 crates/stats/src/network.rs diff --git a/Cargo.lock b/Cargo.lock index e25e09b..bf11324 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -247,6 +247,16 @@ dependencies = [ "util", ] +[[package]] +name = "stats" +version = "0.1.0" +dependencies = [ + "clap", + "dirs", + "eyre", + "util", +] + [[package]] name = "strsim" version = "0.10.0" @@ -312,6 +322,7 @@ dependencies = [ "eyre", "github", "sourcegraph", + "stats", "tldr", "util", ] diff --git a/Cargo.toml b/Cargo.toml index 6c90b50..e75e1b1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,7 +4,13 @@ version = "0.1.0" edition = "2021" [workspace] -members = ["crates/tldr", "crates/util", "crates/sourcegraph", "crates/github"] +members = [ + "crates/tldr", + "crates/util", + "crates/sourcegraph", + "crates/github", + "crates/stats", +] [workspace.dependencies] clap = { version = "4.0.29", features = ["cargo"] } @@ -19,6 +25,7 @@ github = { path = "crates/github" } tldr = { path = "crates/tldr" } sourcegraph = { path = "crates/sourcegraph" } util = { path = "crates/util" } +stats = { path = "crates/stats" } clap.workspace = true eyre.workspace = true diff --git a/crates/stats/Cargo.toml b/crates/stats/Cargo.toml new file mode 100644 index 0000000..8bdfbe3 --- /dev/null +++ b/crates/stats/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "stats" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +util = { path = "../util" } + +eyre.workspace = true +clap.workspace = true +dirs.workspace = true diff --git a/crates/stats/src/code.rs b/crates/stats/src/code.rs new file mode 100644 index 0000000..45baab3 --- /dev/null +++ b/crates/stats/src/code.rs @@ -0,0 +1,58 @@ +use eyre::Context; + +pub struct Code; + +struct Settings { + prefer_docker: bool, +} + +impl Settings { + fn new() -> Self { + Self { + prefer_docker: std::env::var("TOOLKIT_PREFER_DOCKER") + .unwrap_or("false".into()) + .parse() + .context("TOOLKIT_PREFER_DOCKER could not be parsed as a bool") + .unwrap(), + } + } +} + +impl Code { + fn run() -> eyre::Result<()> { + if Settings::new().prefer_docker { + let current_dir = std::env::current_dir()?; + let current_dir_str = current_dir + .to_str() + .ok_or(eyre::anyhow!("could not parse path as string"))?; + util::shell::run( + &[ + "docker", + "run", + "-v", + &format!("{current_dir_str}:/mnt"), + "kasperhermansen/tokei:12.1-amd64", + ], + None, + )?; + } else { + util::shell::run(&["tokei"], None)?; + } + + Ok(()) + } +} + +impl util::Cmd for Code { + fn cmd() -> eyre::Result { + let cmd = clap::Command::new("code").subcommands(&[]); + + Ok(cmd) + } + + fn exec(args: &clap::ArgMatches) -> eyre::Result<()> { + match args.subcommand() { + _ => Code::run(), + } + } +} diff --git a/crates/stats/src/lib.rs b/crates/stats/src/lib.rs new file mode 100644 index 0000000..3c411ba --- /dev/null +++ b/crates/stats/src/lib.rs @@ -0,0 +1,28 @@ +mod code; +mod network; + +pub struct Stats; + +impl Stats { + fn run() -> eyre::Result<()> { + Ok(()) + } +} + +impl util::Cmd for Stats { + fn cmd() -> eyre::Result { + let cmd = clap::Command::new("stats") + .subcommands(&[code::Code::cmd()?, network::Network::cmd()?]) + .subcommand_required(true); + + Ok(cmd) + } + + fn exec(args: &clap::ArgMatches) -> eyre::Result<()> { + match args.subcommand() { + Some(("code", args)) => code::Code::exec(args), + Some(("network", args)) => network::Network::exec(args), + _ => Stats::run(), + } + } +} diff --git a/crates/stats/src/network.rs b/crates/stats/src/network.rs new file mode 100644 index 0000000..4b0ecc5 --- /dev/null +++ b/crates/stats/src/network.rs @@ -0,0 +1,66 @@ +use eyre::Context; + +struct Settings { + prefer_docker: bool, +} + +impl Settings { + fn new() -> Self { + Self { + prefer_docker: std::env::var("TOOLKIT_PREFER_DOCKER") + .unwrap_or("false".into()) + .parse() + .context("TOOLKIT_PREFER_DOCKER could not be parsed as a bool") + .unwrap(), + } + } +} + +pub struct Network; + +impl Network { + fn run() -> eyre::Result<()> { + if Settings::new().prefer_docker { + // let current_dir = std::env::current_dir()?; + // let current_dir_str = current_dir + // .to_str() + // .ok_or(eyre::anyhow!("could not parse path as string"))?; + //util::shell::run( + // &[ + // "docker", + // "run", + // "-v", + // &format!("{current_dir_str}:/mnt"), + // "kasperhermansen/tokei:12.1-amd64", + // ], + // None, + //)?; + } else { + } + if let Err(_) = + util::shell::run_with_input_and_output(&["bandwhich", "--version"], "".into()) + { + return Err(eyre::anyhow!( + "could not find bandwhich, please install or add to PATH" + )); + } + + util::shell::run(&["bandwhich"], None)?; + + Ok(()) + } +} + +impl util::Cmd for Network { + fn cmd() -> eyre::Result { + let cmd = clap::Command::new("network").subcommands(&[]); + + Ok(cmd) + } + + fn exec(args: &clap::ArgMatches) -> eyre::Result<()> { + match args.subcommand() { + _ => Network::run(), + } + } +} diff --git a/src/main.rs b/src/main.rs index 1d88cc4..d761d58 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,3 @@ -use prereqs::prereqs_exec; use util::Cmd; mod prereqs; @@ -10,14 +9,16 @@ fn main() -> eyre::Result<()> { tldr::Tldr::cmd()?, sourcegraph::Sourcegraph::cmd()?, github::GitHub::cmd()?, + stats::Stats::cmd()?, ]) .get_matches(); match matches.subcommand() { - Some(("prereqs", subcmd)) => prereqs_exec(subcmd), + Some(("prereqs", subcmd)) => prereqs::prereqs_exec(subcmd), Some(("tldr", subcmd)) => tldr::Tldr::exec(subcmd), Some(("sourcegraph", subcmd)) => sourcegraph::Sourcegraph::exec(subcmd), Some(("github", subcmd)) => github::GitHub::exec(subcmd), + Some(("stats", subcmd)) => stats::Stats::exec(subcmd), _ => Err(eyre::anyhow!("no command selected!")), } }