nushell/src/commands/cd.rs

48 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;
use crate::object::{DirEntry, ShellObject, Value};
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,
2019-05-12 00:59:57 +02:00
args: Args,
2019-05-11 10:08:21 +02:00
host: &dyn crate::Host,
2019-05-11 09:00:33 +02:00
env: &mut crate::Environment,
2019-05-12 00:59:57 +02:00
) -> Result<Box<dyn crate::Command>, ShellError> {
let target = match args.first() {
// TODO: This needs better infra
None => return Err(ShellError::new(format!("cd must take one arg"))),
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) -> Result<crate::CommandSuccess, ShellError> {
Ok(crate::CommandSuccess {
value: Value::nothing(),
action: vec![crate::CommandAction::ChangeCwd(dunce::canonicalize(
self.cwd.join(&self.target).as_path(),
)?)],
})
2019-05-11 09:00:33 +02:00
}
}