Get example working

This commit is contained in:
tahahawa 2025-04-11 15:19:48 -04:00 committed by taha
parent 20a8f12160
commit fbea2a40c3
6 changed files with 147 additions and 27 deletions

16
Cargo.lock generated
View File

@ -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]]

18
examples/cli/Cargo.toml Normal file
View File

@ -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 }

20
examples/cli/src/main.rs Normal file
View File

@ -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();
}

View File

@ -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"]

88
harmony_cli/src/lib.rs Normal file
View File

@ -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<T: Topology + std::fmt::Debug + Send + Sync + 'static>(
maestro: harmony::maestro::Maestro<T>,
) -> Result<(), Box<dyn std::error::Error>> {
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<Box<dyn Score<T>>> =
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(());
}

View File

@ -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.
}