Browse Source

feat: add command not found

main
fdai7381 2 years ago
committed by fdai7451
parent
commit
797742ddce
  1. 5
      src/error.rs
  2. 11
      src/execute.rs
  3. 7
      src/main.rs

5
src/error.rs

@ -1,4 +1,7 @@
use std::process::Command;
pub enum ShellError { pub enum ShellError {
EmptyLine, EmptyLine,
Execute(String),
NotFound(Command),
ExecuteFailure(String),
} }

11
src/execute.rs

@ -1,7 +1,7 @@
use std::io::ErrorKind::NotFound;
use std::process::Command; use std::process::Command;
use crate::error::ShellError; use crate::error::ShellError;
use crate::error::ShellError::Execute;
use crate::parse::parse_line; use crate::parse::parse_line;
pub fn interpret(line: &str) -> Result<(), ShellError> { pub fn interpret(line: &str) -> Result<(), ShellError> {
@ -22,10 +22,15 @@ fn execute(mut command: Command) -> Result<(), ShellError> {
match command.spawn() { match command.spawn() {
Ok(mut child) => { Ok(mut child) => {
if let Err(err) = child.wait() { if let Err(err) = child.wait() {
return Err(Execute(err.to_string()));
return Err(ShellError::ExecuteFailure(err.to_string()));
} }
Ok(()) 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()))
}
} }
} }

7
src/main.rs

@ -24,8 +24,11 @@ fn main() -> Result<()> {
Err(ShellError::EmptyLine) => { Err(ShellError::EmptyLine) => {
continue; 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) => { Err(ReadlineError::Interrupted) => {

Loading…
Cancel
Save