From f5812d5e88dfa7d5e903017be81a36ced6292684 Mon Sep 17 00:00:00 2001 From: fdai7375 Date: Thu, 19 Jan 2023 00:58:18 +0100 Subject: [PATCH] feat: add cd builtin command --- src/builtins/cd.rs | 8 ++++++++ src/builtins/mod.rs | 7 ++++++- 2 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 src/builtins/cd.rs diff --git a/src/builtins/cd.rs b/src/builtins/cd.rs new file mode 100644 index 0000000..263b823 --- /dev/null +++ b/src/builtins/cd.rs @@ -0,0 +1,8 @@ +use std::env::set_current_dir; + +use crate::error::ShellError; + +pub fn run(keyword: &str, args: &[&str]) -> Result<(), ShellError> { + set_current_dir(args[0]); + Ok(()) +} diff --git a/src/builtins/mod.rs b/src/builtins/mod.rs index cd4a7d1..39735bd 100644 --- a/src/builtins/mod.rs +++ b/src/builtins/mod.rs @@ -1,11 +1,16 @@ use crate::error::ShellError; +mod cd; -const BUILTINS: &[&str] = &[]; +const BUILTINS: &[&str] = &["cd"]; pub fn is_builtin(keyword: &str) -> bool { BUILTINS.contains(&keyword) } pub fn execute_builtin(keyword: &str, args: &[&str]) -> Result<(), ShellError> { + match keyword { + "cd" => cd::run(keyword, args)?, + _ => {} + } Ok(()) }