generated from PlexSheep/baserepo
merge new structure for alpha release #51
|
@ -17,16 +17,16 @@ jobs:
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v4
|
||||||
- name: install rust
|
- name: install rust
|
||||||
uses: dtolnay/rust-toolchain@stable
|
uses: dtolnay/rust-toolchain@stable
|
||||||
|
- name: install additional rust things
|
||||||
run: rustup component add rustfmt
|
run: rustup component add rustfmt
|
||||||
- name: config custom registry
|
- name: config custom registry
|
||||||
run: |
|
run: |
|
||||||
mkdir -p ~/.cargo/
|
mkdir -p ~/.cargo/
|
||||||
echo -e '[registry]
|
echo -e '[registry]\n\
|
||||||
cscherr= "cscherr"
|
cscherr= "cscherr"\n\
|
||||||
|
|
||||||
[registries.cscherr]
|
[registries.cscherr]\n\
|
||||||
index = "sparse+https://git.cscherr.de/api/packages/PlexSheep/cargo/"
|
index = "sparse+https://git.cscherr.de/api/packages/PlexSheep/cargo/"' > ~/.cargo/config.toml
|
||||||
' > ~/.cargo/config.toml
|
|
||||||
cat ~/.cargo/config.toml
|
cat ~/.cargo/config.toml
|
||||||
- name: cargo check
|
- name: cargo check
|
||||||
run: cargo check --all-features --all-targets
|
run: cargo check --all-features --all-targets
|
||||||
|
|
|
@ -69,6 +69,15 @@ crate-type = [
|
||||||
"rlib",
|
"rlib",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[bin]]
|
||||||
|
name = "ccc"
|
||||||
|
path = "src/ccc/mod.rs"
|
||||||
|
|
||||||
|
[[bin]]
|
||||||
|
name = "hedu"
|
||||||
|
path = "src/hedu/mod.rs"
|
||||||
|
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
libpt-core = { workspace = true }
|
libpt-core = { workspace = true }
|
||||||
libpt-bintols = { workspace = true }
|
libpt-bintols = { workspace = true }
|
||||||
|
|
124
src/main/args.rs
124
src/main/args.rs
|
@ -1,124 +0,0 @@
|
||||||
//! # args module
|
|
||||||
//!
|
|
||||||
//! The args module of libpt is used to parse commandline arguments. For this, it makes use of
|
|
||||||
//! [`clap`].
|
|
||||||
|
|
||||||
//// ATTRIBUTES ////////////////////////////////////////////////////////////////////////////////////
|
|
||||||
// we want docs
|
|
||||||
#![warn(missing_docs)]
|
|
||||||
#![warn(rustdoc::missing_crate_level_docs)]
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
||||||
// we want Debug everywhere.
|
|
||||||
#![warn(missing_debug_implementations)]
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
||||||
// enable clippy's extra lints, the pedantic version
|
|
||||||
#![warn(clippy::pedantic)]
|
|
||||||
|
|
||||||
//// IMPORTS ///////////////////////////////////////////////////////////////////////////////////////
|
|
||||||
use clap::{Parser, Subcommand};
|
|
||||||
|
|
||||||
use clap_num::number_range;
|
|
||||||
|
|
||||||
use clap_verbosity_flag::{InfoLevel, Verbosity};
|
|
||||||
|
|
||||||
//// CONSTANTS /////////////////////////////////////////////////////////////////////////////////////
|
|
||||||
/// short about section displayed in help
|
|
||||||
const ABOUT_ROOT: &'static str = r##"
|
|
||||||
Personal multi tool
|
|
||||||
|
|
||||||
A collection of tools made for personal use
|
|
||||||
"##;
|
|
||||||
/// longer about section displayed in help, is combined with [the short help](ABOUT_ROOT)
|
|
||||||
static LONG_ABOUT_ROOT: &'static str = r##"
|
|
||||||
|
|
||||||
libpt is a personal general purpose library, offering this executable, a python module and a
|
|
||||||
dynamic library.
|
|
||||||
"##;
|
|
||||||
|
|
||||||
//// STATICS ///////////////////////////////////////////////////////////////////////////////////////
|
|
||||||
/// ## Main struct for parsing CLI arguments
|
|
||||||
///
|
|
||||||
/// This struct describes the complete commandline options/arguments that [pt](crate) can take. It
|
|
||||||
/// makes use of composition to build a complex system of commands, subcommands, flags and options.
|
|
||||||
#[derive(Debug, Clone, Parser)]
|
|
||||||
#[command(
|
|
||||||
author,
|
|
||||||
version,
|
|
||||||
about = ABOUT_ROOT,
|
|
||||||
long_about = format!("{}{}", ABOUT_ROOT ,LONG_ABOUT_ROOT),
|
|
||||||
help_template =
|
|
||||||
r#"libpt: {version}{about-section}Author:
|
|
||||||
{author-with-newline}
|
|
||||||
{usage-heading} {usage}{all-args}{tab}"#
|
|
||||||
)]
|
|
||||||
pub struct Cli {
|
|
||||||
// clap_verbosity_flag seems to make this a global option implicitly
|
|
||||||
/// set a verbosity, multiple allowed (f.e. -vvv)
|
|
||||||
#[command(flatten)]
|
|
||||||
pub verbose: Verbosity<InfoLevel>,
|
|
||||||
|
|
||||||
/// show logger meta
|
|
||||||
#[arg(short, long, global = true)]
|
|
||||||
pub log_meta: bool,
|
|
||||||
|
|
||||||
/// choose a subcommand
|
|
||||||
#[command(subcommand)]
|
|
||||||
pub command: Commands,
|
|
||||||
}
|
|
||||||
|
|
||||||
//// ENUMS /////////////////////////////////////////////////////////////////////////////////////////
|
|
||||||
/// ## defines the top level commands
|
|
||||||
#[derive(Debug, Clone, Subcommand)]
|
|
||||||
#[non_exhaustive]
|
|
||||||
pub enum Commands {
|
|
||||||
/// networking commands
|
|
||||||
Net {
|
|
||||||
/// Networking subcommands
|
|
||||||
#[command(subcommand)]
|
|
||||||
command: NetCommands,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
||||||
/// ## defines the networking commands
|
|
||||||
#[derive(Debug, Clone, Subcommand)]
|
|
||||||
#[non_exhaustive]
|
|
||||||
pub enum NetCommands {
|
|
||||||
/// monitor your network
|
|
||||||
Monitor {
|
|
||||||
/// repeat every N seconds, 0 means no repeat
|
|
||||||
#[clap(short, long, default_value_t = 0, name = "N")]
|
|
||||||
repeat: u64,
|
|
||||||
|
|
||||||
/// At what percentage should the try be considered successful
|
|
||||||
#[clap(short, long, default_value_t = 100, value_parser=max100)]
|
|
||||||
success_ratio: u8,
|
|
||||||
|
|
||||||
/// extra URLs to check with
|
|
||||||
extra_urls: Vec<String>,
|
|
||||||
|
|
||||||
/// Don't check for default URLs
|
|
||||||
#[clap(short, long)]
|
|
||||||
no_default: bool,
|
|
||||||
|
|
||||||
/// set a timeout (in ms)
|
|
||||||
#[clap(short, long, default_value_t = 100)]
|
|
||||||
timeout: u64,
|
|
||||||
},
|
|
||||||
/// discover hosts in your network
|
|
||||||
Discover {},
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
||||||
|
|
||||||
//// STRUCTS ///////////////////////////////////////////////////////////////////////////////////////
|
|
||||||
|
|
||||||
//// IMPLEMENTATION ////////////////////////////////////////////////////////////////////////////////
|
|
||||||
|
|
||||||
//// PUBLIC FUNCTIONS //////////////////////////////////////////////////////////////////////////////
|
|
||||||
|
|
||||||
//// PRIVATE FUNCTIONS /////////////////////////////////////////////////////////////////////////////
|
|
||||||
/// custom value parser, only allow 0 to 100
|
|
||||||
fn max100(s: &str) -> Result<u8, String> {
|
|
||||||
number_range(s, 0, 100)
|
|
||||||
}
|
|
102
src/main/mod.rs
102
src/main/mod.rs
|
@ -1,102 +0,0 @@
|
||||||
//! # Main executable of libpt
|
|
||||||
//!
|
|
||||||
//! This module contains all code specific to the executable version of [`pt`]: [`pt`](crate).
|
|
||||||
//!
|
|
||||||
//!
|
|
||||||
|
|
||||||
//// ATTRIBUTES ////////////////////////////////////////////////////////////////////////////////////
|
|
||||||
// we want docs
|
|
||||||
#![warn(missing_docs)]
|
|
||||||
#![warn(rustdoc::missing_crate_level_docs)]
|
|
||||||
// we want Debug everywhere.
|
|
||||||
#![warn(missing_debug_implementations)]
|
|
||||||
// enable clippy's extra lints, the pedantic version
|
|
||||||
#![warn(clippy::pedantic)]
|
|
||||||
|
|
||||||
//// IMPORTS ///////////////////////////////////////////////////////////////////////////////////////
|
|
||||||
use libpt::{log::*, net::monitoring::uptime};
|
|
||||||
|
|
||||||
use clap::Parser;
|
|
||||||
|
|
||||||
pub mod args;
|
|
||||||
use args::*;
|
|
||||||
|
|
||||||
//// CONSTANTS /////////////////////////////////////////////////////////////////////////////////////
|
|
||||||
#[allow(dead_code)]
|
|
||||||
//// STATICS ///////////////////////////////////////////////////////////////////////////////////////
|
|
||||||
|
|
||||||
//// MACROS ////////////////////////////////////////////////////////////////////////////////////////
|
|
||||||
|
|
||||||
//// ENUMS /////////////////////////////////////////////////////////////////////////////////////////
|
|
||||||
|
|
||||||
//// STRUCTS ///////////////////////////////////////////////////////////////////////////////////////
|
|
||||||
|
|
||||||
//// IMPLEMENTATION ////////////////////////////////////////////////////////////////////////////////
|
|
||||||
|
|
||||||
//// PUBLIC FUNCTIONS //////////////////////////////////////////////////////////////////////////////
|
|
||||||
/// ## Main function of the [`pt`](crate) binary
|
|
||||||
pub fn main() {
|
|
||||||
let cli = Cli::parse();
|
|
||||||
let ll: Level = match cli.verbose.log_level().unwrap().as_str() {
|
|
||||||
"TRACE" => Level::TRACE,
|
|
||||||
"DEBUG" => Level::DEBUG,
|
|
||||||
"INFO" => Level::INFO,
|
|
||||||
"WARN" => Level::WARN,
|
|
||||||
"ERROR" => Level::ERROR,
|
|
||||||
_ => {
|
|
||||||
eprintln!("'{}' is not a valid loglevel", cli.verbose.to_string());
|
|
||||||
std::process::exit(1);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
if cli.log_meta {
|
|
||||||
Logger::init(None, Some(ll)).expect("could not initialize Logger");
|
|
||||||
} else {
|
|
||||||
// less verbose version
|
|
||||||
Logger::init_mini(Some(ll)).expect("could not initialize Logger");
|
|
||||||
}
|
|
||||||
|
|
||||||
trace!("started the main function");
|
|
||||||
trace!("{:?}", &cli);
|
|
||||||
|
|
||||||
match cli.clone().command {
|
|
||||||
Commands::Net { command } => net(&cli, command),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
||||||
/// ## Process [`Net`](args::NetCommands) subcommands
|
|
||||||
pub fn net(cli: &Cli, command: NetCommands) {
|
|
||||||
match command {
|
|
||||||
NetCommands::Monitor {
|
|
||||||
repeat,
|
|
||||||
success_ratio,
|
|
||||||
extra_urls,
|
|
||||||
no_default,
|
|
||||||
timeout,
|
|
||||||
} => {
|
|
||||||
let urls: Vec<String>;
|
|
||||||
if no_default {
|
|
||||||
urls = extra_urls;
|
|
||||||
} else {
|
|
||||||
let mut combined: Vec<String> = Vec::new();
|
|
||||||
for i in uptime::DEFAULT_CHECK_URLS {
|
|
||||||
combined.push(i.to_string());
|
|
||||||
}
|
|
||||||
combined.extend(extra_urls);
|
|
||||||
urls = combined;
|
|
||||||
}
|
|
||||||
let _verbose = cli.verbose.log_level().is_some();
|
|
||||||
if repeat > 0 {
|
|
||||||
uptime::continuous_uptime_monitor(success_ratio, urls, repeat * 1000, timeout);
|
|
||||||
} else {
|
|
||||||
let status = uptime::UptimeStatus::new(success_ratio, urls, timeout);
|
|
||||||
info!("status:\n{}", status);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
NetCommands::Discover {} => {
|
|
||||||
todo!()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//// PRIVATE FUNCTIONS /////////////////////////////////////////////////////////////////////////////
|
|
Reference in New Issue