diff options
Diffstat (limited to 'src/commandline/builtins.rs')
-rw-r--r-- | src/commandline/builtins.rs | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/src/commandline/builtins.rs b/src/commandline/builtins.rs new file mode 100644 index 0000000..c5999f2 --- /dev/null +++ b/src/commandline/builtins.rs @@ -0,0 +1,45 @@ +use std::io::{Error, ErrorKind::InvalidInput}; +use std::path::{Path, PathBuf}; +use super::command::RunResult; + +pub(in crate::commandline) fn cd(args: &[String], home: &PathBuf) -> Result<RunResult, Error> { + if args.len() > 1 { + return Err(Error::new( + InvalidInput, + "Too many arguments passed to cd", + )); + } + + let root = if args.len() == 0 { + home.to_path_buf() + } else { + std::fs::canonicalize(Path::new(args[0].as_str()))? + }; + + match std::env::set_current_dir(&root) { + Ok(_) => Ok(RunResult::Builtin), + Err(e) => Err(e), + } +} + +pub(in crate::commandline) fn set(args: &[String]) -> Result<RunResult, Error> { + if args.len() != 2 { + return Err(Error::new( + InvalidInput, + format!("set requires 2 arguments, got {}", args.len()), + )); + } + std::env::set_var(&args[0], &args[1]); + Ok(RunResult::Builtin) +} + +pub(in crate::commandline) fn unset(args: &[String]) -> Result<RunResult, Error> { + if args.len() != 1 { + return Err(Error::new( + InvalidInput, + format!("unset requires 1 argument, got {}", args.len()), + )); + } + std::env::remove_var(&args[0]); + Ok(RunResult::Builtin) +} |