From 88d9d8ae827e1c2ca66c699bae25ce9485a4687b Mon Sep 17 00:00:00 2001 From: nicolas <48695862+merklefruit@users.noreply.github.com> Date: Sun, 5 Nov 2023 20:07:48 +0100 Subject: [PATCH] feat: watch command --- bin/opup/src/cli.rs | 7 +++++-- bin/opup/src/lib.rs | 1 + bin/opup/src/up.rs | 2 ++ bin/opup/src/watch.rs | 37 +++++++++++++++++++++++++++++++++++++ 4 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 bin/opup/src/watch.rs diff --git a/bin/opup/src/cli.rs b/bin/opup/src/cli.rs index 3982ff6..82f3f68 100644 --- a/bin/opup/src/cli.rs +++ b/bin/opup/src/cli.rs @@ -29,6 +29,10 @@ pub enum Command { Clean, /// List op-up docker containers List, + /// Watch the devnet stack components. + /// This will output a refreshed view of the stack components + /// as they come online. + Watch, /// Install Dependencies Deps, } @@ -38,8 +42,6 @@ pub fn run() -> Result<()> { crate::telemetry::init_tracing_subscriber(v)?; - crate::banners::banner()?; - // Dispatch on the specified subcommand, // running the `up` subcommand by default. match command { @@ -47,6 +49,7 @@ pub fn run() -> Result<()> { Some(command) => match command { Command::Up(up_command) => up_command.run(), Command::List => crate::list::run(), + Command::Watch => crate::watch::run(), Command::Down => unimplemented!("down command not yet implemented"), Command::Nuke => unimplemented!("nuke command not yet implemented"), Command::Clean => unimplemented!("clean command not yet implemented"), diff --git a/bin/opup/src/lib.rs b/bin/opup/src/lib.rs index e11d25f..057226b 100644 --- a/bin/opup/src/lib.rs +++ b/bin/opup/src/lib.rs @@ -10,3 +10,4 @@ pub(crate) mod list; pub(crate) mod runner; pub(crate) mod telemetry; pub(crate) mod up; +pub(crate) mod watch; diff --git a/bin/opup/src/up.rs b/bin/opup/src/up.rs index f0b1944..4a36b0c 100644 --- a/bin/opup/src/up.rs +++ b/bin/opup/src/up.rs @@ -61,6 +61,8 @@ impl UpCommand { /// Entrypoint #[instrument(name = "up", target = "run", skip(self))] pub fn run(&self) -> Result<()> { + crate::banners::banner()?; + crate::runner::run_until_ctrl_c(async { self.execute().await }) } } diff --git a/bin/opup/src/watch.rs b/bin/opup/src/watch.rs new file mode 100644 index 0000000..6417f95 --- /dev/null +++ b/bin/opup/src/watch.rs @@ -0,0 +1,37 @@ +use eyre::Result; +use std::time::Duration; + +pub(crate) fn run() -> Result<()> { + crate::runner::run_until_ctrl_c(async { + loop { + // clear the terminal and reset the cursor to the top left + print!("\x1B[2J\x1B[1;1H"); + + let containers = op_composer::Composer::new()? + .list_containers(Some("running")) + .await?; + + let mut table = prettytable::Table::new(); + table.set_titles(prettytable::row!["Name", "Image", "Status", "Up Time"]); + for container in containers { + table.add_row(prettytable::row![ + container + .names + .map(|n| n.join(", ")) + .unwrap_or_else(|| "none".to_string()), + container.image.unwrap_or_else(|| "none".to_string()), + container.status.unwrap_or_else(|| "none".to_string()), + container + .created + .map(|created| humantime::format_duration(Duration::from_secs( + created as u64 + )) + .to_string()) + .unwrap_or_else(|| "unknown".to_string()) + ]); + } + table.printstd(); + tokio::time::sleep(Duration::from_secs(2)).await; + } + }) +}