From e22b70acffaea2614a829297d61276eb6ad287a1 Mon Sep 17 00:00:00 2001 From: JT <547158+jntrnr@users.noreply.github.com> Date: Wed, 1 Mar 2023 21:20:00 +1300 Subject: [PATCH] Remove the 'env' command, as we have the variable (#8185) # Description Removes the `env` command, as the `$env` is generally a much better experience. # User-Facing Changes Breaking change: Removes `env`. # Tests + Formatting Don't forget to add tests that cover your changes. Make sure you've run and fixed any issues with these commands: - `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes) - `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A clippy::needless_collect` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass # After Submitting If your PR had any user-facing changes, update [the documentation](https://github.com/nushell/nushell.github.io) after the PR is merged, if necessary. This will help us keep the docs up to date. --- crates/nu-command/src/default_context.rs | 1 - crates/nu-command/src/env/env_command.rs | 90 ------------------------ crates/nu-command/src/env/mod.rs | 2 - src/tests/test_env.rs | 8 --- src/tests/test_parser.rs | 2 +- 5 files changed, 1 insertion(+), 102 deletions(-) delete mode 100644 crates/nu-command/src/env/env_command.rs diff --git a/crates/nu-command/src/default_context.rs b/crates/nu-command/src/default_context.rs index f6fa8b5ac..521eb5efc 100644 --- a/crates/nu-command/src/default_context.rs +++ b/crates/nu-command/src/default_context.rs @@ -343,7 +343,6 @@ pub fn create_default_context() -> EngineState { // Env bind_command! { - Env, ExportEnv, LetEnv, LoadEnv, diff --git a/crates/nu-command/src/env/env_command.rs b/crates/nu-command/src/env/env_command.rs deleted file mode 100644 index 5235e3b3c..000000000 --- a/crates/nu-command/src/env/env_command.rs +++ /dev/null @@ -1,90 +0,0 @@ -use nu_engine::env_to_string; -use nu_protocol::ast::Call; -use nu_protocol::engine::{Command, EngineState, Stack}; -use nu_protocol::{ - Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, Type, Value, -}; - -#[derive(Clone)] -pub struct Env; - -impl Command for Env { - fn name(&self) -> &str { - "env" - } - - fn usage(&self) -> &str { - "Display current environment variables." - } - - fn signature(&self) -> nu_protocol::Signature { - Signature::build("env") - .category(Category::Env) - .input_output_types(vec![(Type::Nothing, Type::Table(vec![]))]) - } - - fn run( - &self, - engine_state: &EngineState, - stack: &mut Stack, - call: &Call, - _input: PipelineData, - ) -> Result { - let span = call.head; - - let mut env_vars: Vec<(String, Value)> = - stack.get_env_vars(engine_state).into_iter().collect(); - env_vars.sort_by(|(name1, _), (name2, _)| name1.cmp(name2)); - - let mut values = vec![]; - - for (name, val) in env_vars { - let mut cols = vec![]; - let mut vals = vec![]; - - let raw_val = match env_to_string(&name, &val, engine_state, stack) { - Ok(raw) => Value::string(raw, span), - Err(ShellError::EnvVarNotAString(..)) => Value::nothing(span), - Err(e) => return Err(e), - }; - - let val_type = val.get_type(); - - cols.push("name".into()); - vals.push(Value::string(name, span)); - - cols.push("type".into()); - vals.push(Value::string(format!("{val_type}"), span)); - - cols.push("value".into()); - vals.push(val); - - cols.push("raw".into()); - vals.push(raw_val); - - values.push(Value::Record { cols, vals, span }); - } - - Ok(Value::List { vals: values, span }.into_pipeline_data()) - } - - fn examples(&self) -> Vec { - vec![ - Example { - description: "Display current path environment variable", - example: "env | where name == PATH", - result: None, - }, - Example { - description: "Check whether the env variable `MY_ENV_ABC` exists", - example: r#"env | any { |e| $e.name == MY_ENV_ABC }"#, - result: Some(Value::test_bool(false)), - }, - Example { - description: "Another way to check whether the env variable `PATH` exists", - example: r#"'PATH' in (env).name"#, - result: Some(Value::test_bool(true)), - }, - ] - } -} diff --git a/crates/nu-command/src/env/mod.rs b/crates/nu-command/src/env/mod.rs index a6fad0f32..467038485 100644 --- a/crates/nu-command/src/env/mod.rs +++ b/crates/nu-command/src/env/mod.rs @@ -1,5 +1,4 @@ mod config; -mod env_command; mod export_env; mod let_env; mod load_env; @@ -10,7 +9,6 @@ pub use config::ConfigEnv; pub use config::ConfigMeta; pub use config::ConfigNu; pub use config::ConfigReset; -pub use env_command::Env; pub use export_env::ExportEnv; pub use let_env::LetEnv; pub use load_env::LoadEnv; diff --git a/src/tests/test_env.rs b/src/tests/test_env.rs index 23f2aaf42..5604ee7e9 100644 --- a/src/tests/test_env.rs +++ b/src/tests/test_env.rs @@ -14,11 +14,3 @@ fn shorthand_env_2() -> TestResult { fn shorthand_env_3() -> TestResult { run_test(r#"FOO=BAZ BAR=MOO $env.FOO"#, "BAZ") } - -#[test] -fn convert_non_string_env_var_to_nothing() -> TestResult { - run_test( - r#"let-env FOO = true; env | where name == FOO | get raw.0 | describe"#, - "nothing", - ) -} diff --git a/src/tests/test_parser.rs b/src/tests/test_parser.rs index 0db8b987e..eff81e960 100644 --- a/src/tests/test_parser.rs +++ b/src/tests/test_parser.rs @@ -234,7 +234,7 @@ fn equals_separates_long_flag() -> TestResult { fn let_env_expressions() -> TestResult { let env = HashMap::from([("VENV_OLD_PATH", "Foobar"), ("Path", "Quux")]); run_test_with_env( - r#"let-env Path = if (env | any {|x| $x.name == VENV_OLD_PATH}) { $env.VENV_OLD_PATH } else { $env.Path }; echo $env.Path"#, + r#"let-env Path = if ($env | columns | "VENV_OLD_PATH" in $in) { $env.VENV_OLD_PATH } else { $env.Path }; echo $env.Path"#, "Foobar", &env, )