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
52 changes: 47 additions & 5 deletions src/klog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -132,6 +131,39 @@ fn get_pods(pod: &FoundPod) -> anyhow::Result<String> {
Ok(pods)
}

fn get_all(pod: &FoundPod) -> anyhow::Result<String> {
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<String> {
let output = {
Command::new("kubectl")
Expand Down Expand Up @@ -324,10 +356,19 @@ fn run_app<B: Backend>(
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();
Expand Down Expand Up @@ -384,9 +425,10 @@ fn run_app<B: Backend>(
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),
Expand All @@ -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))
Expand Down
26 changes: 23 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ struct Args {
#[derive(Default)]
struct FoundPod {
name: String,
namespace: String
namespace: String,
deployment: String
}

fn main() -> Result<()> {
Expand All @@ -41,17 +42,36 @@ fn find_matching_pod(matcher: &str) -> Result<FoundPod> {

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<Captures> = re.captures(&*pods) else { todo!() };

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<Captures> = 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)
Expand Down