Skip to content

Commit a214bd5

Browse files
Update: Added service Command to the CLI.
1 parent 4729a73 commit a214bd5

3 files changed

Lines changed: 88 additions & 1 deletion

File tree

cli/src/main.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ mod essential;
22
mod install;
33
mod general;
44
mod uninstall;
5+
mod service;
56

67
use clap::{ Error, Parser, Subcommand, Args };
78
use clap::command;
@@ -10,6 +11,7 @@ use tracing::debug;
1011
use crate::essential::{ info, update_cli };
1112
use crate::install::install_cortexflow;
1213
use crate::uninstall::uninstall;
14+
use crate::service::list_services;
1315

1416
use crate::general::GeneralData;
1517

@@ -42,12 +44,29 @@ enum Commands {
4244
Update,
4345
#[command(name="info")]
4446
Info,
47+
#[command(name="service")]
48+
Service(ServiceArgs),
4549
}
4650
#[derive(Args, Debug, Clone)]
4751
struct SetArgs {
4852
val: String,
4953
}
5054

55+
#[derive(Args, Debug, Clone)]
56+
struct ServiceArgs {
57+
#[command(subcommand)]
58+
service_cmd: ServiceCommands,
59+
}
60+
61+
#[derive(Subcommand, Debug, Clone)]
62+
enum ServiceCommands {
63+
#[command(name="list")]
64+
List {
65+
#[arg(long)]
66+
namespace: Option<String>,
67+
},
68+
}
69+
5170
fn args_parser() -> Result<(), Error> {
5271
let args = Cli::parse();
5372
let env = args.env;
@@ -78,6 +97,14 @@ fn args_parser() -> Result<(), Error> {
7897
info(general_data);
7998
Ok(())
8099
}
100+
Some(Commands::Service(service_args)) => {
101+
match service_args.service_cmd {
102+
ServiceCommands::List { namespace } => {
103+
list_services(namespace);
104+
Ok(())
105+
}
106+
}
107+
}
81108
None => {
82109
eprintln!("CLI unknown argument. Cli arguments passed: {:?}", args.cmd);
83110
Ok(())

cli/src/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
pub mod essential;
22
pub mod install;
33
pub mod general;
4-
pub mod uninstall;
4+
pub mod uninstall;
5+
pub mod service;

cli/src/service.rs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
use std::process::Command;
2+
use std::str;
3+
4+
pub fn list_services(namespace: Option<String>) {
5+
let ns = namespace.unwrap_or_else(|| "cortexflow".to_string());
6+
7+
println!("Listing services in namespace: {}", ns);
8+
9+
// kubectl command to get services
10+
let output = Command::new("kubectl")
11+
.args(["get", "pods", "-n", &ns, "--no-headers"])
12+
.output();
13+
14+
match output {
15+
Ok(output) => {
16+
if !output.status.success() {
17+
let error = str::from_utf8(&output.stderr).unwrap_or("Unknown error");
18+
eprintln!("Error executing kubectl: {}", error);
19+
std::process::exit(1);
20+
}
21+
22+
let stdout = str::from_utf8(&output.stdout).unwrap_or("");
23+
24+
if stdout.trim().is_empty() {
25+
println!("No pods found in namespace '{}'", ns);
26+
return;
27+
}
28+
29+
// header for Table
30+
println!("{:<40} {:<20} {:<10} {:<10}", "NAME", "STATUS", "RESTARTS", "AGE");
31+
println!("{}", "-".repeat(80));
32+
33+
// Display Each Pod.
34+
for line in stdout.lines() {
35+
let parts: Vec<&str> = line.split_whitespace().collect();
36+
if parts.len() >= 5 {
37+
let name = parts[0];
38+
let ready = parts[1];
39+
let status = parts[2];
40+
let restarts = parts[3];
41+
let age = parts[4];
42+
43+
let full_status = if ready.contains('/') {
44+
format!("{} ({})", status, ready)
45+
} else {
46+
status.to_string()
47+
};
48+
49+
println!("{:<40} {:<20} {:<10} {:<10}", name, full_status, restarts, age);
50+
}
51+
}
52+
}
53+
Err(err) => {
54+
eprintln!("Failed to execute kubectl command: {}", err);
55+
eprintln!("Make sure kubectl is installed and configured properly");
56+
std::process::exit(1);
57+
}
58+
}
59+
}

0 commit comments

Comments
 (0)