Cleanup/refactoring
This commit is contained in:
parent
ebbac5aca0
commit
4d8f8aa075
|
@ -913,46 +913,42 @@ impl EngineState {
|
||||||
/// directory on the stack that have yet to be merged into the engine state.
|
/// directory on the stack that have yet to be merged into the engine state.
|
||||||
pub fn cwd(&self, stack: Option<&Stack>) -> Result<AbsolutePathBuf, ShellError> {
|
pub fn cwd(&self, stack: Option<&Stack>) -> Result<AbsolutePathBuf, ShellError> {
|
||||||
// Helper function to create a simple generic error.
|
// Helper function to create a simple generic error.
|
||||||
fn error(msg: &str, cwd: impl AsRef<nu_path::Path>) -> Result<AbsolutePathBuf, ShellError> {
|
fn error(msg: &str, cwd: impl AsRef<nu_path::Path>) -> ShellError {
|
||||||
Err(ShellError::GenericError {
|
ShellError::GenericError {
|
||||||
error: msg.into(),
|
error: msg.into(),
|
||||||
msg: format!("$env.PWD = {}", cwd.as_ref().display()),
|
msg: format!("$env.PWD = {}", cwd.as_ref().display()),
|
||||||
span: None,
|
span: None,
|
||||||
help: Some("Use `cd` to reset $env.PWD into a good state".into()),
|
help: Some("Use `cd` to reset $env.PWD into a good state".into()),
|
||||||
inner: vec![],
|
inner: vec![],
|
||||||
})
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Retrieve $env.PWD from the stack or the engine state.
|
// Retrieve $env.PWD from the stack or the engine state.
|
||||||
let pwd = if let Some(stack) = stack {
|
let pwd = if let Some(stack) = stack {
|
||||||
stack.get_env_var(self, "PWD")
|
stack.get_env_var(self, "PWD")
|
||||||
} else {
|
} else {
|
||||||
self.get_env_var("PWD").map(ToOwned::to_owned)
|
self.get_env_var("PWD").cloned()
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Some(pwd) = pwd {
|
let pwd = pwd.ok_or_else(|| error("$env.PWD not found", ""))?;
|
||||||
|
|
||||||
if let Value::String { val, .. } = pwd {
|
if let Value::String { val, .. } = pwd {
|
||||||
match AbsolutePathBuf::try_from(val) {
|
let path = AbsolutePathBuf::try_from(val)
|
||||||
Ok(path) => {
|
.map_err(|path| error("$env.PWD is not an absolute path", path))?;
|
||||||
|
|
||||||
// Technically, a root path counts as "having trailing slashes", but
|
// Technically, a root path counts as "having trailing slashes", but
|
||||||
// for the purpose of PWD, a root path is acceptable.
|
// for the purpose of PWD, a root path is acceptable.
|
||||||
if path.parent().is_none() && nu_path::has_trailing_slash(path.as_ref()) {
|
if path.parent().is_some() && nu_path::has_trailing_slash(path.as_ref()) {
|
||||||
error("$env.PWD contains trailing slashes", &path)
|
Err(error("$env.PWD contains trailing slashes", &path))
|
||||||
} else if !path.exists() {
|
} else if !path.exists() {
|
||||||
error("$env.PWD points to a non-existent directory", &path)
|
Err(error("$env.PWD points to a non-existent directory", &path))
|
||||||
} else if !path.is_dir() {
|
} else if !path.is_dir() {
|
||||||
error("$env.PWD points to a non-directory", &path)
|
Err(error("$env.PWD points to a non-directory", &path))
|
||||||
} else {
|
} else {
|
||||||
Ok(path)
|
Ok(path)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
Err(path) => error("$env.PWD is not an absolute path", path),
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
error("$env.PWD is not a string", format!("{pwd:?}"))
|
Err(error("$env.PWD is not a string", format!("{pwd:?}")))
|
||||||
}
|
|
||||||
} else {
|
|
||||||
error("$env.PWD not found", "")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user