aboutsummaryrefslogtreecommitdiff
path: root/src/commandline/builtins.rs
blob: 882a690bfa3f0331771586ebd0f5bc45645bfeda (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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<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)
}