diff --git a/cli/src/main.rs b/cli/src/main.rs index a53056f..b6cdd28 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -2,6 +2,7 @@ mod essential; mod install; mod general; mod uninstall; +mod service; use clap::{ Error, Parser, Subcommand, Args }; use clap::command; @@ -10,6 +11,7 @@ use tracing::debug; use crate::essential::{ info, update_cli }; use crate::install::install_cortexflow; use crate::uninstall::uninstall; +use crate::service::list_services; use crate::general::GeneralData; @@ -42,12 +44,29 @@ enum Commands { Update, #[command(name="info")] Info, + #[command(name="service")] + Service(ServiceArgs), } #[derive(Args, Debug, Clone)] struct SetArgs { val: String, } +#[derive(Args, Debug, Clone)] +struct ServiceArgs { + #[command(subcommand)] + service_cmd: ServiceCommands, +} + +#[derive(Subcommand, Debug, Clone)] +enum ServiceCommands { + #[command(name="list")] + List { + #[arg(long)] + namespace: Option, + }, +} + fn args_parser() -> Result<(), Error> { let args = Cli::parse(); let env = args.env; @@ -78,6 +97,14 @@ fn args_parser() -> Result<(), Error> { info(general_data); Ok(()) } + Some(Commands::Service(service_args)) => { + match service_args.service_cmd { + ServiceCommands::List { namespace } => { + list_services(namespace); + Ok(()) + } + } + } None => { eprintln!("CLI unknown argument. Cli arguments passed: {:?}", args.cmd); Ok(()) diff --git a/cli/src/mod.rs b/cli/src/mod.rs index 0c7c7f5..db6f1c3 100644 --- a/cli/src/mod.rs +++ b/cli/src/mod.rs @@ -1,4 +1,5 @@ pub mod essential; pub mod install; pub mod general; -pub mod uninstall; \ No newline at end of file +pub mod uninstall; +pub mod service; \ No newline at end of file diff --git a/cli/src/service.rs b/cli/src/service.rs new file mode 100644 index 0000000..86c9645 --- /dev/null +++ b/cli/src/service.rs @@ -0,0 +1,59 @@ +use std::process::Command; +use std::str; + +pub fn list_services(namespace: Option) { + let ns = namespace.unwrap_or_else(|| "cortexflow".to_string()); + + println!("Listing services in namespace: {}", ns); + + // kubectl command to get services + let output = Command::new("kubectl") + .args(["get", "pods", "-n", &ns, "--no-headers"]) + .output(); + + match output { + Ok(output) => { + if !output.status.success() { + let error = str::from_utf8(&output.stderr).unwrap_or("Unknown error"); + eprintln!("Error executing kubectl: {}", error); + std::process::exit(1); + } + + let stdout = str::from_utf8(&output.stdout).unwrap_or(""); + + if stdout.trim().is_empty() { + println!("No pods found in namespace '{}'", ns); + return; + } + + // header for Table + println!("{:<40} {:<20} {:<10} {:<10}", "NAME", "STATUS", "RESTARTS", "AGE"); + println!("{}", "-".repeat(80)); + + // Display Each Pod. + for line in stdout.lines() { + let parts: Vec<&str> = line.split_whitespace().collect(); + if parts.len() >= 5 { + let name = parts[0]; + let ready = parts[1]; + let status = parts[2]; + let restarts = parts[3]; + let age = parts[4]; + + let full_status = if ready.contains('/') { + format!("{} ({})", status, ready) + } else { + status.to_string() + }; + + println!("{:<40} {:<20} {:<10} {:<10}", name, full_status, restarts, age); + } + } + } + Err(err) => { + eprintln!("Failed to execute kubectl command: {}", err); + eprintln!("Make sure kubectl is installed and configured properly"); + std::process::exit(1); + } + } +} \ No newline at end of file