Skip to content

Commit

Permalink
fix: add line method to the Command
Browse files Browse the repository at this point in the history
  • Loading branch information
therustmonk committed Mar 4, 2022
1 parent 3e8f749 commit 0bb2089
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 58 deletions.
8 changes: 4 additions & 4 deletions applications/tari_base_node/src/commands/command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ use tari_core::{
use tari_p2p::{auto_update::SoftwareUpdaterHandle, services::liveness::LivenessHandle};
use tari_shutdown::Shutdown;
use tokio::{sync::watch, time};
use watch_command::WatchCommand;

use crate::{builder::BaseNodeContext, commands::parser::FromHex};

Expand Down Expand Up @@ -161,11 +162,10 @@ impl CommandContext {
}
}

pub async fn handle_command_str(&mut self, line: &str) -> Result<Option<String>, Error> {
pub async fn handle_command_str(&mut self, line: &str) -> Result<Option<WatchCommand>, Error> {
let args: Args = line.parse()?;
if let Command::Watch(args) = args.command {
// TODO: Add interval as well
Ok(Some(args.command))
if let Command::Watch(command) = args.command {
Ok(Some(command))
} else {
let fut = self.handle_command(args.command);
time::timeout(Duration::from_secs(70), fut).await??;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,30 @@
use std::fmt;

use anyhow::Error;
use async_trait::async_trait;
use clap::Parser;

use super::{CommandContext, HandleCommand};

pub type WatchCommand = Args;

impl fmt::Display for WatchCommand {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
self.line().fmt(f)
}
}

#[derive(Debug, Parser)]
pub struct Args {
pub command: String,
#[clap(short, long)]
pub interval: Option<u64>,
pub command: Option<String>,
}

impl Args {
pub fn line(&self) -> &str {
self.command.as_ref().map(String::as_ref).unwrap_or("status")
}
}

#[async_trait]
Expand Down
113 changes: 60 additions & 53 deletions applications/tari_base_node/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ use tari_core::chain_storage::ChainStorageError;
#[cfg(all(unix, feature = "libtor"))]
use tari_libtor::tor::Tor;
use tari_shutdown::{Shutdown, ShutdownSignal};
use tokio::{task, time};
use tokio::{signal, task, time};
use tonic::transport::Server;
use tracing_subscriber::{layer::SubscriberExt, Registry};

Expand Down Expand Up @@ -391,7 +391,7 @@ async fn cli_loop(mut context: CommandContext) {
let parser = Parser::new();
commands::cli::print_banner(parser.get_commands(), 3);

// let mut performer = Performer::new(command_handler);
// TODO: Check new version
let cli_config = Config::builder()
.history_ignore_space(true)
.completion_type(CompletionType::List)
Expand All @@ -413,71 +413,78 @@ async fn cli_loop(mut context: CommandContext) {
let config = context.config.clone();
loop {
let mut watch_task = None;
tokio::select! {
res = reader.next_command() => {
if let Some(event) = res {
match event {
CommandEvent::Command(line) => {
first_signal = false;
if !line.is_empty() {
match context.handle_command_str(&line).await {
Err(err) => {
println!("Command `{}` failed: {}", line, err);
}
Ok(command) => {
watch_task = command;
loop {
tokio::select! {
res = reader.next_command() => {
if let Some(event) = res {
match event {
CommandEvent::Command(line) => {
first_signal = false;
if !line.is_empty() {
match context.handle_command_str(&line).await {
Err(err) => {
println!("Command `{}` failed: {}", line, err);
}
Ok(command) => {
watch_task = command;
}
}
}
}
}
CommandEvent::Interrupt => {
if !first_signal {
println!("Are you leaving already? Press Ctrl-C again to terminate the node.");
first_signal = true;
} else {
CommandEvent::Interrupt => {
if !first_signal {
println!("Are you leaving already? Press Ctrl-C again to terminate the node.");
first_signal = true;
} else {
break;
}
}
CommandEvent::Error(err) => {
// TODO: Not sure we have to break here
// This happens when the node is shutting down.
debug!(target: LOG_TARGET, "Could not read line from rustyline:{}", err);
break;
}
}
CommandEvent::Error(err) => {
// TODO: Not sure we have to break here
// This happens when the node is shutting down.
debug!(target: LOG_TARGET, "Could not read line from rustyline:{}", err);
break;
}
} else {
break;
}
} else {
},
_ = shutdown_signal.wait() => {
break;
}
},
_ = shutdown_signal.wait() => {
break;
}
}
if let Some(line) = watch_task {
let interval = get_status_interval(start_time, config.base_node_status_line_interval);
tokio::select! {
_ = interval => {
if let Err(err) = context.handle_command_str(&line).await {
println!("Watched command `{}` failed: {}", line, err);
if let Some(command) = watch_task.as_ref() {
let line = command.line();
loop {
let interval = get_status_interval(start_time, config.base_node_status_line_interval);
tokio::select! {
_ = interval => {
if let Err(err) = context.handle_command_str(line).await {
println!("Watched command `{}` failed: {}", line, err);
}
// TODO: Execute `watch` command here + use the result
// let _ = performer.status(StatusLineOutput::StdOutAndLog).await;
},
_ = signal::ctrl_c() => {
break;
}
// TODO: Execute `watch` command here + use the result
// let _ = performer.status(StatusLineOutput::StdOutAndLog).await;
},
// TODO: How about to check it on start as well?
/*
Ok(_) = software_update_notif.changed() => {
if let Some(ref update) = *software_update_notif.borrow() {
println!(
"Version {} of the {} is available: {} (sha: {})",
update.version(),
update.app(),
update.download_url(),
update.to_hash_hex()
);
// TODO: How about to check it on start as well?
/*
Ok(_) = software_update_notif.changed() => {
if let Some(ref update) = *software_update_notif.borrow() {
println!(
"Version {} of the {} is available: {} (sha: {})",
update.version(),
update.app(),
update.download_url(),
update.to_hash_hex()
);
}
}
*/
}
*/
// TODO: Process async ctrl-c here
}
}
}
Expand Down

0 comments on commit 0bb2089

Please sign in to comment.