use super::command::RunResult; use std::io::{Error, ErrorKind::InvalidInput}; use std::path::{Path, PathBuf}; pub(in crate::commandline) fn cd(args: &[String], home: &PathBuf) -> Result { 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 { 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 { 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) }