From bf346cb53e2739658cd78a51ebada6ab308125d0 Mon Sep 17 00:00:00 2001 From: Joaquin Caro Date: Wed, 30 Sep 2020 16:23:26 +0200 Subject: [PATCH] add follow logs command --- README.md | 6 ++++-- src/commands/command.rs | 10 +++++----- src/commands/logs.rs | 8 +++++--- src/main.rs | 2 ++ 4 files changed, 16 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 6511fbb..a7fe726 100644 --- a/README.md +++ b/README.md @@ -29,8 +29,8 @@ $ adh --help --database_name Optional. Allows you to specify the name of a database to be created on image startup --root_password Optional. Specifies the password that will be set for the MySQL root superuser account If not set, a random password will be created and printed at the end - start Start container with given id, if container_id is not provider, user can select from a list - stop Stop container with given id, if container_id is not provider, user can select from a list + start Start container with id container_id, if container_id is not provider, user can select from a list + stop Stop container with id container_id, if container_id is not provider, user can select from a list ps Formatted ps for running dockers psa Formatted ps for all dockers rc Remove all containers @@ -40,6 +40,8 @@ $ adh --help rec Remove exited containers kc Kill all containers remove-volumes Remove all volumes + log Show container logs with id container_id, if container_id is not provider, user can select from a list + flog Show container logs in follow mode with id container_id, if container_id is not provider, user can select from a list Options: diff --git a/src/commands/command.rs b/src/commands/command.rs index 6c5d03e..1dbddd4 100644 --- a/src/commands/command.rs +++ b/src/commands/command.rs @@ -2,16 +2,15 @@ use async_trait::async_trait; use clap::ArgMatches; use crate::commands::create_local_registry::LocalRegistry; +use crate::commands::kc::KillContainers; +use crate::commands::logs::Logs; use crate::commands::mysql::Mysql; use crate::commands::nginx::Nginx; use crate::commands::ps::Ps; use crate::commands::psa::Psa; use crate::commands::rc::RemoveContainers; -use crate::commands::remove_none_images::RemoveNoneImages; - -use crate::commands::kc::KillContainers; -use crate::commands::logs::Logs; use crate::commands::remove_exited_containers::RemoveExitedContainers; +use crate::commands::remove_none_images::RemoveNoneImages; use crate::commands::remove_volumes::RemoveVolumes; use crate::commands::ri::RemoveImages; use crate::commands::start::Start; @@ -58,7 +57,8 @@ pub fn from(matches: ArgMatches) -> Box { Some("rec") => Box::new(RemoveExitedContainers), Some("kc") => Box::new(KillContainers), Some("remove-volumes") => Box::new(RemoveVolumes), - Some("log") => Box::new(Logs), + Some("flog") => Box::new(Logs { follow: true }), + Some("log") => Box::new(Logs { follow: false }), _ => Box::new(Noop), } } diff --git a/src/commands/logs.rs b/src/commands/logs.rs index 657afc4..ba6b2f6 100644 --- a/src/commands/logs.rs +++ b/src/commands/logs.rs @@ -1,13 +1,15 @@ use async_trait::async_trait; use futures::StreamExt; -use shiplift::tty::TtyChunk; use shiplift::{Docker, LogsOptions}; +use shiplift::tty::TtyChunk; use crate::commands::command::Command; use crate::infra::container_repository::get_all_containers; use crate::infra::container_selector::select_container; -pub struct Logs; +pub struct Logs { + pub(crate) follow: bool, +} #[async_trait] impl Command for Logs { @@ -17,7 +19,7 @@ impl Command for Logs { let docker = Docker::new(); let mut logs_stream = docker.containers().get(&selected).logs( &LogsOptions::builder() - .follow(true) + .follow(self.follow) .stdout(true) .stderr(true) .build(), diff --git a/src/main.rs b/src/main.rs index 453b226..c7113f9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -59,6 +59,8 @@ async fn main() { .about("Remove all volumes")) .subcommand(SubCommand::with_name("log") .about("Show docker logs")) + .subcommand(SubCommand::with_name("flog") + .about("Show docker logs and listen the changes")) .get_matches(); from(matches).execute().await;