Added export command

This commit is contained in:
Justine
2023-03-04 20:17:22 +01:00
parent 5743678e23
commit 09cea688b7
2 changed files with 24 additions and 10 deletions

View File

@ -73,6 +73,21 @@ pub mod shell {
}
}
fn export_var(command: String) -> Result<CmdOutput, CmdOutput> {
let cmd_split = command.split('=').collect::<Vec<&str>>();
if cmd_split.len() < 2 {
return Err(CmdOutput::from_values(
String::from("Please a format such as : VAR=value"),
255));
}
let key = String::from(cmd_split[0].to_uppercase());
let val = String::from(cmd_split[1]);
let mut map = HashMap::new();
map.insert(key, val);
set_envvars(&map);
return Ok(CmdOutput::new_empty());
}
fn change_dir(args: &Vec<String>, previous_command: &mut Option<Stdio>) -> Result<CmdOutput, CmdOutput> {
let homedir = dirs::home_dir().unwrap().into_os_string().into_string().unwrap();
//let new_dir = args.peekable().peek().map_or(homedir.as_str(), |x| *x);
@ -142,6 +157,10 @@ pub mod shell {
let next = commands.peek().is_some();
print_hist(&mut previous_command, next)?;
},
"export" => {
export_var(args.join(" "))?;
print!("\r\n");
},
command => {
if commands.peek().is_some() {
let stdin = match previous_command {
@ -643,8 +662,8 @@ pub mod shell {
}
//I smell horrible code in here
fn set_envvars(conf: &SqishConf) {
let vars = conf.env.clone();
fn set_envvars(vars: &HashMap<String, String>) {
let vars = vars.clone();
let re = Regex::new(r"\$[A-Za-z_]+").unwrap();
for (key, value) in vars {
@ -696,7 +715,7 @@ pub mod shell {
//Initializing
write!(elems.stdout, "\r\n ---Sqish initializing--- \r\n{}", elems.conf.promptline);
elems.stdout.flush();
set_envvars(&elems.conf);
set_envvars(&elems.conf.env);
if elems.conf.init != String::from("") {
run_cmd(&mut String::from(&elems.conf.init), &mut elems.current_number, &mut elems.conf, &mut elems.stdout);