From 797742ddce6d7d30d25f3a2784001673f5c58cf6 Mon Sep 17 00:00:00 2001 From: fdai7381 Date: Tue, 17 Jan 2023 21:05:43 +0100 Subject: [PATCH] feat: add command not found --- src/error.rs | 5 ++++- src/execute.rs | 11 ++++++++--- src/main.rs | 7 +++++-- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/error.rs b/src/error.rs index 49740d9..46bbb99 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,4 +1,7 @@ +use std::process::Command; + pub enum ShellError { EmptyLine, - Execute(String), + NotFound(Command), + ExecuteFailure(String), } diff --git a/src/execute.rs b/src/execute.rs index 362d217..3a54865 100644 --- a/src/execute.rs +++ b/src/execute.rs @@ -1,7 +1,7 @@ +use std::io::ErrorKind::NotFound; use std::process::Command; use crate::error::ShellError; -use crate::error::ShellError::Execute; use crate::parse::parse_line; pub fn interpret(line: &str) -> Result<(), ShellError> { @@ -22,10 +22,15 @@ fn execute(mut command: Command) -> Result<(), ShellError> { match command.spawn() { Ok(mut child) => { if let Err(err) = child.wait() { - return Err(Execute(err.to_string())); + return Err(ShellError::ExecuteFailure(err.to_string())); } Ok(()) } - Err(err) => Err(Execute(err.to_string())), + Err(err) => { + if err.kind() == NotFound { + return Err(ShellError::NotFound(command)); + } + Err(ShellError::ExecuteFailure(err.to_string())) + } } } diff --git a/src/main.rs b/src/main.rs index 75728d3..237c641 100644 --- a/src/main.rs +++ b/src/main.rs @@ -24,8 +24,11 @@ fn main() -> Result<()> { Err(ShellError::EmptyLine) => { continue; } - Err(ShellError::Execute(err)) => { - eprintln!("{}", err) + Err(ShellError::NotFound(cmd)) => { + println!("{}: command not found", cmd.get_program().to_string_lossy()) + } + Err(ShellError::ExecuteFailure(msg)) => { + eprintln!("{}", msg) } }, Err(ReadlineError::Interrupted) => {