This commit is contained in:
JT 2022-04-23 11:48:10 +12:00 committed by GitHub
parent 5ff2ae628b
commit cc78446ffd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 45 deletions

View File

@ -1,7 +1,9 @@
use nu_engine::{current_dir, CallExt};
use nu_protocol::ast::{Call, Expr, Expression};
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{Category, Example, PipelineData, ShellError, Signature, SyntaxShape, Value};
use nu_protocol::{
Category, Example, PipelineData, ShellError, Signature, Spanned, SyntaxShape, Value,
};
#[derive(Clone)]
pub struct Cd;
@ -28,17 +30,12 @@ impl Command for Cd {
call: &Call,
_input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
let raw_path = call.positional_nth(0);
let path_val: Option<Value> = call.opt(engine_state, stack, 0)?;
let path_val: Option<Spanned<String>> = call.opt(engine_state, stack, 0)?;
let cwd = current_dir(engine_state, stack)?;
let (path, span) = match raw_path {
Some(v) => match &v {
Expression {
expr: Expr::Filepath(val),
span,
..
} if val == "-" => {
let (path, span) = match path_val {
Some(v) => {
if v.item == "-" {
let oldpwd = stack.get_env_var(engine_state, "OLDPWD");
if let Some(oldpwd) = oldpwd {
@ -47,42 +44,34 @@ impl Command for Cd {
Ok(p) => p,
Err(e) => {
return Err(ShellError::DirectoryNotFound(
*span,
v.span,
Some(format!("IO Error: {:?}", e)),
))
}
};
(path.to_string_lossy().to_string(), *span)
(path.to_string_lossy().to_string(), v.span)
} else {
(cwd.to_string_lossy().to_string(), *span)
(cwd.to_string_lossy().to_string(), v.span)
}
}
_ => match path_val {
Some(v) => {
let path = v.as_path()?;
let path = match nu_path::canonicalize_with(path, &cwd) {
Ok(p) => {
if !p.is_dir() {
return Err(ShellError::NotADirectory(v.span()?));
}
p
} else {
let path = match nu_path::canonicalize_with(&v.item, &cwd) {
Ok(p) => {
if !p.is_dir() {
return Err(ShellError::NotADirectory(v.span));
}
p
}
Err(e) => {
return Err(ShellError::DirectoryNotFound(
v.span()?,
Some(format!("IO Error: {:?}", e)),
))
}
};
(path.to_string_lossy().to_string(), v.span()?)
}
None => {
let path = nu_path::expand_tilde("~");
(path.to_string_lossy().to_string(), call.head)
}
},
},
Err(e) => {
return Err(ShellError::DirectoryNotFound(
v.span,
Some(format!("IO Error: {:?}", e)),
))
}
};
(path.to_string_lossy().to_string(), v.span)
}
}
None => {
let path = nu_path::expand_tilde("~");
(path.to_string_lossy().to_string(), call.head)

View File

@ -531,13 +531,20 @@ pub fn eval_expression(
})
}
Expr::Directory(s) => {
let cwd = current_dir_str(engine_state, stack)?;
let path = expand_path_with(s, cwd);
if s == "-" {
Ok(Value::String {
val: "-".to_string(),
span: expr.span,
})
} else {
let cwd = current_dir_str(engine_state, stack)?;
let path = expand_path_with(s, cwd);
Ok(Value::String {
val: path.to_string_lossy().to_string(),
span: expr.span,
})
Ok(Value::String {
val: path.to_string_lossy().to_string(),
span: expr.span,
})
}
}
Expr::GlobPattern(s) => {
let cwd = current_dir_str(engine_state, stack)?;