with network stats
This commit is contained in:
13
crates/stats/Cargo.toml
Normal file
13
crates/stats/Cargo.toml
Normal file
@@ -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
crates/stats/src/code.rs
Normal file
58
crates/stats/src/code.rs
Normal file
@@ -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
crates/stats/src/lib.rs
Normal file
28
crates/stats/src/lib.rs
Normal file
@@ -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
crates/stats/src/network.rs
Normal file
66
crates/stats/src/network.rs
Normal file
@@ -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(),
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user