diff --git a/src/klog.rs b/src/klog.rs index c115658..21e4063 100644 --- a/src/klog.rs +++ b/src/klog.rs @@ -4,7 +4,6 @@ use std::time::{Duration, Instant}; use crossterm::event::{DisableMouseCapture, Event, KeyCode}; use crossterm::{event, execute}; use crossterm::terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen}; -use rand::seq::IndexedRandom; use rand::Rng; use ratatui::backend::{Backend, CrosstermBackend}; use ratatui::text::Line; @@ -132,6 +131,39 @@ fn get_pods(pod: &FoundPod) -> anyhow::Result { Ok(pods) } +fn get_all(pod: &FoundPod) -> anyhow::Result { + let output = { + Command::new("kubectl") + .arg("get") + .arg("all") + .arg("-n") + .arg(&pod.namespace) + .arg("--no-headers") + .output() + }; + + let all = String::from_utf8(output.unwrap().stdout).unwrap().to_string(); + + Ok(all) +} + +fn edit_deployment(pod: &FoundPod) -> anyhow::Result<()> { + let output = { + Command::new("kubectl") + .arg("edit") + .arg("deployment") + .arg(&pod.deployment) + .arg("-n") + .arg(&pod.namespace) + .spawn() + .unwrap() + .wait() + .expect("failed to execute process") + }; + + Ok(()) +} + fn delete_pod(pod: &FoundPod) -> anyhow::Result { let output = { Command::new("kubectl") @@ -324,10 +356,19 @@ fn run_app( text = describe_pod(&app.target_pod).unwrap(); app.vertical_scroll = 0; }, + KeyCode::Char('E') => { + terminal.clear().unwrap(); + edit_deployment(&app.target_pod).unwrap(); + terminal.clear().unwrap(); + }, KeyCode::Char('w') => { text = get_pods(&app.target_pod).unwrap(); app.vertical_scroll = 0; }, + KeyCode::Char('W') => { + text = get_all(&app.target_pod).unwrap(); + app.vertical_scroll = 0; + }, KeyCode::Char('e') => { terminal.clear().unwrap(); exec_into_pod(&app.target_pod).unwrap(); @@ -384,9 +425,10 @@ fn run_app( fn ui(f: &mut Frame, app: &mut App, text: &str) { let size = f.size(); let pod_name = &app.target_pod.name; + let pod_deployment = &app.target_pod.deployment; let pod_ns = &app.target_pod.namespace; - let details_content = "πŸ“œ [f]etch logs πŸ“– [l]ast logs πŸ“ [v]im "; + let details_content = "πŸ“œ [f]etch logs πŸ“– [l]ast logs πŸ“ [v]im logs"; let chunks = Layout::vertical([ Constraint::Min(1), @@ -401,11 +443,11 @@ fn ui(f: &mut Frame, app: &mut App, text: &str) { .gray() .block( Block::bordered().white() - .title_top(Line::from(format!("{0} {pod_ns}/{pod_name}", app.emoji)).left_aligned().bold().white()) + .title_top(Line::from(format!("{0} {pod_ns}/{pod_deployment}/{pod_name}", app.emoji)).left_aligned().bold().white()) .title_top(Line::from(format!("[q]uit βœ–οΈ")).right_aligned().white()) - .title_top(Line::from(format!("πŸ”Ž [d]esc πŸ’» [e]xec 🐞 de[b]ug πŸ’€ [p]urge")).centered().white()) + .title_top(Line::from(format!("πŸ”Ž [d]esc πŸ’» [e]xec ✏️ [E]dit 🐞 de[b]ug πŸ’€ [p]urge")).centered().white()) .title_bottom(details_content).to_owned() - .title_bottom(Line::from(format!("[s][w]itch βš™οΈ").white()).right_aligned()) + .title_bottom(Line::from(format!("πŸ—ΊοΈ [W/w]orld [s]witch βš™οΈ").white()).right_aligned()) ) .style(Style::default().fg(Color::Rgb(186, 186, 186))) .scroll((app.vertical_scroll as u16, app.horizontal_scroll as u16)) diff --git a/src/main.rs b/src/main.rs index f60facd..58a0566 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,7 +17,8 @@ struct Args { #[derive(Default)] struct FoundPod { name: String, - namespace: String + namespace: String, + deployment: String } fn main() -> Result<()> { @@ -41,7 +42,7 @@ fn find_matching_pod(matcher: &str) -> Result { let pods = String::from_utf8(output.stdout).unwrap().to_string(); - let re = Regex::new(&format!(r"(\b.*\b)( .*{matcher}.*-[0-9A-Za-z-]+)")).unwrap(); + let mut re = Regex::new(&format!(r"(\b.*\b)( .*{matcher}.*-[0-9A-Za-z-]+)")).unwrap(); // First match will be namespace, second will be pod let Some(matches): Option = re.captures(&*pods) else { todo!() }; @@ -49,9 +50,28 @@ fn find_matching_pod(matcher: &str) -> Result { let pod: String = matches[2].replace(" ", ""); let ns: String = matches[1].to_string(); + let deployment_output = { + Command::new("kubectl") + .arg("get") + .arg("deployments") + .arg("-n") + .arg(&ns) + .output() + .expect("failed to execute process") + }; + + let deployments = String::from_utf8(deployment_output.stdout).unwrap().to_string(); + + re = Regex::new(&format!(r".*{matcher}.*[A-Za-z]+ ")).unwrap(); + + let Some(deployment_matches): Option = re.captures(&*deployments) else { todo!() }; + + let deployment: String = deployment_matches[0].to_string().replace(" ", ""); + let found_pod : FoundPod = FoundPod { name: pod, - namespace: ns + namespace: ns, + deployment: deployment }; Ok(found_pod)