Skip to content

Commit

Permalink
with network stats
Browse files Browse the repository at this point in the history
  • Loading branch information
kjuulh committed Dec 18, 2022
1 parent f8d2351 commit 94f2540
Show file tree
Hide file tree
Showing 7 changed files with 187 additions and 3 deletions.
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand All @@ -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
13 changes: 13 additions & 0 deletions crates/stats/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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
58 changes: 58 additions & 0 deletions crates/stats/src/code.rs
Original file line number Diff line number Diff line change
@@ -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<clap::Command> {
let cmd = clap::Command::new("code").subcommands(&[]);

Ok(cmd)
}

fn exec(args: &clap::ArgMatches) -> eyre::Result<()> {
match args.subcommand() {
_ => Code::run(),
}
}
}
28 changes: 28 additions & 0 deletions crates/stats/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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<clap::Command> {
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(),
}
}
}
66 changes: 66 additions & 0 deletions crates/stats/src/network.rs
Original file line number Diff line number Diff line change
@@ -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<clap::Command> {
let cmd = clap::Command::new("network").subcommands(&[]);

Ok(cmd)
}

fn exec(args: &clap::ArgMatches) -> eyre::Result<()> {
match args.subcommand() {
_ => Network::run(),
}
}
}
5 changes: 3 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use prereqs::prereqs_exec;
use util::Cmd;

mod prereqs;
Expand All @@ -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!")),
}
}

0 comments on commit 94f2540

Please sign in to comment.