From fbea2a40c3b42ccc2405816e4d7bdb7b15f5f2a1 Mon Sep 17 00:00:00 2001 From: tahahawa Date: Fri, 11 Apr 2025 15:19:48 -0400 Subject: [PATCH] Get example working --- Cargo.lock | 16 ++++++++ examples/cli/Cargo.toml | 18 ++++++++ examples/cli/src/main.rs | 20 +++++++++ harmony_cli/Cargo.toml | 5 +++ harmony_cli/src/lib.rs | 88 ++++++++++++++++++++++++++++++++++++++++ harmony_cli/src/main.rs | 27 ------------ 6 files changed, 147 insertions(+), 27 deletions(-) create mode 100644 examples/cli/Cargo.toml create mode 100644 examples/cli/src/main.rs create mode 100644 harmony_cli/src/lib.rs delete mode 100644 harmony_cli/src/main.rs diff --git a/Cargo.lock b/Cargo.lock index a287efe..9619e3e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -838,6 +838,21 @@ dependencies = [ name = "example" version = "0.0.0" +[[package]] +name = "example-cli" +version = "0.1.0" +dependencies = [ + "cidr", + "env_logger", + "harmony", + "harmony_cli", + "harmony_macros", + "harmony_types", + "log", + "tokio", + "url", +] + [[package]] name = "example-kube-rs" version = "0.1.0" @@ -1212,6 +1227,7 @@ version = "0.1.0" dependencies = [ "clap", "harmony", + "harmony_tui", ] [[package]] diff --git a/examples/cli/Cargo.toml b/examples/cli/Cargo.toml new file mode 100644 index 0000000..0b46a64 --- /dev/null +++ b/examples/cli/Cargo.toml @@ -0,0 +1,18 @@ +[package] +name = "example-cli" +edition = "2024" +version.workspace = true +readme.workspace = true +license.workspace = true +publish = false + +[dependencies] +harmony = { path = "../../harmony" } +harmony_cli = { path = "../../harmony_cli" } +harmony_types = { path = "../../harmony_types" } +cidr = { workspace = true } +tokio = { workspace = true } +harmony_macros = { path = "../../harmony_macros" } +log = { workspace = true } +env_logger = { workspace = true } +url = { workspace = true } diff --git a/examples/cli/src/main.rs b/examples/cli/src/main.rs new file mode 100644 index 0000000..b7b58f4 --- /dev/null +++ b/examples/cli/src/main.rs @@ -0,0 +1,20 @@ +use harmony::{ + inventory::Inventory, + maestro::Maestro, + modules::dummy::{ErrorScore, PanicScore, SuccessScore}, + topology::HAClusterTopology, +}; + +#[tokio::main] +async fn main() { + let inventory = Inventory::autoload(); + let topology = HAClusterTopology::autoload(); + let mut maestro = Maestro::new(inventory, topology); + + maestro.register_all(vec![ + Box::new(SuccessScore {}), + Box::new(ErrorScore {}), + Box::new(PanicScore {}), + ]); + harmony_cli::init(maestro).await.unwrap(); +} diff --git a/harmony_cli/Cargo.toml b/harmony_cli/Cargo.toml index 04d4b66..ee8ccc2 100644 --- a/harmony_cli/Cargo.toml +++ b/harmony_cli/Cargo.toml @@ -8,3 +8,8 @@ license.workspace = true [dependencies] clap = { version = "4.5.35", features = ["derive"] } harmony = { path = "../harmony" } +harmony_tui = { path = "../harmony_tui", optional = true } + + +[features] +tui = ["dep:harmony_tui"] diff --git a/harmony_cli/src/lib.rs b/harmony_cli/src/lib.rs new file mode 100644 index 0000000..a470822 --- /dev/null +++ b/harmony_cli/src/lib.rs @@ -0,0 +1,88 @@ +use clap::Parser; +use clap::builder::ArgPredicate; +use harmony; +use harmony::{score::Score, topology::Topology}; + +#[cfg(feature = "tui")] +use harmony_tui; + +#[derive(Parser, Debug)] +#[command(version, about, long_about = None)] +struct Args { + /// Name of score to run + #[cfg(feature = "tui")] + #[arg( + short, + long, + default_value_if("interactive", ArgPredicate::IsPresent, Some("unused")) + )] + run: String, + + /// Name of score to run + #[cfg(not(feature = "tui"))] + #[arg(short, long)] + run: String, + + /// Run interactive or not + #[arg(short, long, default_value_t = false)] + interactive: bool, + + /// Run all or nth + #[arg(short, long, default_value_t = false)] + all: bool, + + /// Run nth matching, zero indexed + #[arg(short, long, default_value_t = 0)] + number: u8, +} + +pub async fn init( + maestro: harmony::maestro::Maestro, +) -> Result<(), Box> { + let args = Args::parse(); + + #[cfg(feature = "tui")] + if args.interactive { + return harmony_tui::init(maestro).await; + } + + #[cfg(not(feature = "tui"))] + if args.interactive { + return Err("Not compiled with interactive support".into()); + } + + let scores = maestro.scores(); + let scores_read = scores.read().expect("Should be able to read scores"); + let scores_read_vec: Vec>> = + scores_read.iter().map(|s| s.clone_box()).collect(); + + if args.all { + for s in scores_read_vec { + if s.name().contains(&args.run) { + println!("Running: {}", s.clone_box().name()); + } + } + } else { + let mut count = 0; + let mut found = false; + for s in scores_read_vec { + if s.name().contains(&args.run) { + found = true; + if count < args.number { + // Skip scores that aren't the nth specified + count = count + 1; + continue; + } else { + // Run nth score found + println!("Running: {}", s.clone_box().name()); + break; + } + } + } + if !found { + println!("Couldn't find score by that name") + } + } + + return Ok(()); +} diff --git a/harmony_cli/src/main.rs b/harmony_cli/src/main.rs deleted file mode 100644 index 3da1986..0000000 --- a/harmony_cli/src/main.rs +++ /dev/null @@ -1,27 +0,0 @@ -use clap::Parser; -use harmony; - -#[derive(Parser, Debug)] -#[command(version, about, long_about = None)] -struct Args { - /// Name of score to run - #[arg(short, long)] - run: String, - - /// Run interactive or not - #[arg(short, long, default_value_t = false)] - interactive: bool, - - /// Run all or first - #[arg(short, long, default_value_t = false)] - all: bool, - - /// Run nth matching - #[arg(short, long, default_value_t = 1)] - number: u8, -} - -#[harmony::main] -pub async fn main(maestro: harmony::maestro::Maestro) { - maestro. -} \ No newline at end of file