diff options
Diffstat (limited to 'src/main.rs')
-rw-r--r-- | src/main.rs | 32 |
1 files changed, 20 insertions, 12 deletions
diff --git a/src/main.rs b/src/main.rs index f03c271..84ba250 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,37 +11,45 @@ use crate::ui::UI; use clap::Parser; use fork::{fork, Fork}; use nix::unistd::Pid; +use owo_colors::OwoColorize; use trace::attach; /// Create a fork of the program and execute the process in the child. Parent gets the pid /// value and trace it. fn main() -> anyhow::Result<()> { let args = Args::parse(); - let pid = if args.command.is_some() { + let process: Result<Pid, String> = if args.command.is_some() { match fork() { Ok(Fork::Child) => return exec(&args.command.unwrap()), - Ok(Fork::Parent(child)) => Pid::from_raw(child), - Err(err) => panic!("fork() failed: {err}"), + Ok(Fork::Parent(child)) => Ok(Pid::from_raw(child)), + Err(err) => Err(format!("fork() failed: {err}")), } } else if args.attach.is_some() { let pid = Pid::from_raw(args.attach.unwrap()); if attach(pid).is_ok() { - pid + Ok(pid) } else { - panic!("Unable to attach to process {pid}"); + Err(format!("Unable to attach to process `{pid}`")) } } else { - panic!("You must define a command or a PID to attach"); + Err(format!("You must define a command or a PID to attach")) }; - if !args.no_tui { - let mut ui = UI::new(); + match process { + Ok(pid) => { + if !args.no_tui { + let mut ui = UI::new(); - ui.start(pid, &args)?; - } else { - trace(pid, &args)?; - } + ui.start(pid, &args)?; + } else { + trace(pid, &args)?; + } + } + Err(e) => { + eprintln!("{}", e.red()); + } + }; Ok(()) } |