nushell/src/commands/cd.rs

44 lines
1.1 KiB
Rust
Raw Normal View History

2019-05-11 09:00:33 +02:00
use crate::errors::ShellError;
2019-05-16 00:58:44 +02:00
use crate::object::Value;
use crate::prelude::*;
2019-05-11 09:00:33 +02:00
use derive_new::new;
2019-05-16 00:58:44 +02:00
use std::path::PathBuf;
2019-05-11 09:00:33 +02:00
#[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>,
2019-05-16 00:58:44 +02:00
_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 {
2019-05-16 00:58:44 +02:00
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
}
}