nushell/src/commands/cd.rs

47 lines
1.2 KiB
Rust
Raw Normal View History

2019-05-11 09:00:33 +02:00
use crate::errors::ShellError;
use crate::object::process::Process;
2019-05-16 00:23:36 +02:00
use crate::object::{dir_entry_dict, Value};
use crate::prelude::*;
2019-05-12 00:59:57 +02:00
use crate::Args;
2019-05-11 09:00:33 +02:00
use derive_new::new;
use std::path::{Path, PathBuf};
use sysinfo::SystemExt;
#[derive(new)]
2019-05-11 10:08:21 +02:00
pub struct CdBlueprint;
2019-05-11 09:00:33 +02:00
2019-05-11 10:08:21 +02:00
impl crate::CommandBlueprint for CdBlueprint {
fn create(
&self,
args: Vec<Value>,
host: &dyn Host,
env: &mut Environment,
) -> Result<Box<dyn Command>, ShellError> {
2019-05-12 00:59:57 +02:00
let target = match args.first() {
// TODO: This needs better infra
None => return Err(ShellError::string(format!("cd must take one arg"))),
2019-05-12 00:59:57 +02:00
Some(v) => v.as_string()?.clone(),
};
Ok(Box::new(Cd {
2019-05-11 10:08:21 +02:00
cwd: env.cwd().to_path_buf(),
2019-05-12 00:59:57 +02:00
target,
}))
2019-05-11 10:08:21 +02:00
}
}
#[derive(new)]
pub struct Cd {
cwd: PathBuf,
target: String,
}
impl crate::Command for Cd {
fn run(&mut self, stream: VecDeque<Value>) -> Result<VecDeque<ReturnValue>, ShellError> {
let mut stream = VecDeque::new();
let path = dunce::canonicalize(self.cwd.join(&self.target).as_path())?;
stream.push_back(ReturnValue::change_cwd(path));
Ok(stream)
2019-05-11 09:00:33 +02:00
}
}