Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mod essential;
mod install;
mod general;
mod uninstall;
mod service;

use clap::{ Error, Parser, Subcommand, Args };
use clap::command;
Expand All @@ -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;

Expand Down Expand Up @@ -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<String>,
},
}

fn args_parser() -> Result<(), Error> {
let args = Cli::parse();
let env = args.env;
Expand Down Expand Up @@ -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(())
Expand Down
3 changes: 2 additions & 1 deletion cli/src/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod essential;
pub mod install;
pub mod general;
pub mod uninstall;
pub mod uninstall;
pub mod service;
59 changes: 59 additions & 0 deletions cli/src/service.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
use std::process::Command;
use std::str;

pub fn list_services(namespace: Option<String>) {
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);
}
}
}