Add hide-env to hide environment variables (#6313)

* Add hide-env to hide env vars; Cleanup tests

Also, there were some old unalias tests that I converted to hide.

* Add missing file

* Re-enable hide for env vars

* Fix test

* Rename did you mean error back

It was causing random tests to break
This commit is contained in:
Jakub Žádník
2022-08-13 12:55:06 +03:00
committed by GitHub
parent 3b6c4c1bb5
commit 20528e96c7
8 changed files with 139 additions and 80 deletions

View File

@ -19,11 +19,11 @@ impl Command for Hide {
}
fn usage(&self) -> &str {
"Hide symbols in the current scope"
"Hide definitions in the current scope"
}
fn extra_usage(&self) -> &str {
r#"Symbols are hidden by priority: First aliases, then custom commands, then environment variables.
r#"Definitions are hidden by priority: First aliases, then custom commands.
This command is a parser keyword. For details, check:
https://www.nushell.sh/book/thinking_in_nushell.html"#

View File

@ -0,0 +1,75 @@
use nu_engine::CallExt;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
did_you_mean, Category, Example, PipelineData, ShellError, Signature, Span, Spanned,
SyntaxShape, Value,
};
#[derive(Clone)]
pub struct HideEnv;
impl Command for HideEnv {
fn name(&self) -> &str {
"hide-env"
}
fn signature(&self) -> nu_protocol::Signature {
Signature::build("hide-env")
.rest(
"name",
SyntaxShape::String,
"environment variable names to hide",
)
.switch(
"ignore-errors",
"do not throw an error if an environment variable was not found",
Some('i'),
)
.category(Category::Core)
}
fn usage(&self) -> &str {
"Hide environment variables in the current scope"
}
fn run(
&self,
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
_input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
let env_var_names: Vec<Spanned<String>> = call.rest(engine_state, stack, 0)?;
let ignore_errors = call.has_flag("ignore-errors");
for name in env_var_names {
if stack.remove_env_var(engine_state, &name.item).is_none() && !ignore_errors {
let all_names: Vec<String> = stack
.get_env_var_names(engine_state)
.iter()
.cloned()
.collect();
if let Some(closest_match) = did_you_mean(&all_names, &name.item) {
return Err(ShellError::DidYouMeanCustom(
format!("Environment variable '{}' not found", name.item),
closest_match,
name.span,
));
} else {
return Err(ShellError::EnvVarNotFoundAtRuntime(name.item, name.span));
}
}
}
Ok(PipelineData::new(call.head))
}
fn examples(&self) -> Vec<Example> {
vec![Example {
description: "Hide an environment variable",
example: r#"let-env HZ_ENV_ABC = 1; hide-env HZ_ENV_ABC; 'HZ_ENV_ABC' in (env).name"#,
result: Some(Value::boolean(false, Span::test_data())),
}]
}
}

View File

@ -17,6 +17,7 @@ mod extern_;
mod for_;
pub mod help;
mod hide;
mod hide_env;
mod if_;
mod ignore;
mod let_;
@ -46,6 +47,7 @@ pub use extern_::Extern;
pub use for_::For;
pub use help::Help;
pub use hide::Hide;
pub use hide_env::HideEnv;
pub use if_::If;
pub use ignore::Ignore;
pub use let_::Let;

View File

@ -47,6 +47,7 @@ pub fn create_default_context() -> EngineState {
For,
Help,
Hide,
HideEnv,
If,
Ignore,
Overlay,

View File

@ -626,6 +626,15 @@ Either make sure {0} is a string, or add a 'to_string' entry for it in ENV_CONVE
#[diagnostic(code(nu::shell::name_not_found), url(docsrs))]
DidYouMean(String, #[label("did you mean '{0}'?")] Span),
/// A name was not found. Did you mean a different name?
///
/// ## Resolution
///
/// The error message will suggest a possible match for what you meant.
#[error("{0}")]
#[diagnostic(code(nu::shell::did_you_mean_custom), url(docsrs))]
DidYouMeanCustom(String, String, #[label("did you mean '{1}'?")] Span),
/// The given input must be valid UTF-8 for further processing.
///
/// ## Resolution