forked from extern/nushell
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.
This commit is contained in:
parent
a5c604c283
commit
e22b70acff
@ -343,7 +343,6 @@ pub fn create_default_context() -> EngineState {
|
|||||||
|
|
||||||
// Env
|
// Env
|
||||||
bind_command! {
|
bind_command! {
|
||||||
Env,
|
|
||||||
ExportEnv,
|
ExportEnv,
|
||||||
LetEnv,
|
LetEnv,
|
||||||
LoadEnv,
|
LoadEnv,
|
||||||
|
90
crates/nu-command/src/env/env_command.rs
vendored
90
crates/nu-command/src/env/env_command.rs
vendored
@ -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<PipelineData, ShellError> {
|
|
||||||
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<Example> {
|
|
||||||
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)),
|
|
||||||
},
|
|
||||||
]
|
|
||||||
}
|
|
||||||
}
|
|
2
crates/nu-command/src/env/mod.rs
vendored
2
crates/nu-command/src/env/mod.rs
vendored
@ -1,5 +1,4 @@
|
|||||||
mod config;
|
mod config;
|
||||||
mod env_command;
|
|
||||||
mod export_env;
|
mod export_env;
|
||||||
mod let_env;
|
mod let_env;
|
||||||
mod load_env;
|
mod load_env;
|
||||||
@ -10,7 +9,6 @@ pub use config::ConfigEnv;
|
|||||||
pub use config::ConfigMeta;
|
pub use config::ConfigMeta;
|
||||||
pub use config::ConfigNu;
|
pub use config::ConfigNu;
|
||||||
pub use config::ConfigReset;
|
pub use config::ConfigReset;
|
||||||
pub use env_command::Env;
|
|
||||||
pub use export_env::ExportEnv;
|
pub use export_env::ExportEnv;
|
||||||
pub use let_env::LetEnv;
|
pub use let_env::LetEnv;
|
||||||
pub use load_env::LoadEnv;
|
pub use load_env::LoadEnv;
|
||||||
|
@ -14,11 +14,3 @@ fn shorthand_env_2() -> TestResult {
|
|||||||
fn shorthand_env_3() -> TestResult {
|
fn shorthand_env_3() -> TestResult {
|
||||||
run_test(r#"FOO=BAZ BAR=MOO $env.FOO"#, "BAZ")
|
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",
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
@ -234,7 +234,7 @@ fn equals_separates_long_flag() -> TestResult {
|
|||||||
fn let_env_expressions() -> TestResult {
|
fn let_env_expressions() -> TestResult {
|
||||||
let env = HashMap::from([("VENV_OLD_PATH", "Foobar"), ("Path", "Quux")]);
|
let env = HashMap::from([("VENV_OLD_PATH", "Foobar"), ("Path", "Quux")]);
|
||||||
run_test_with_env(
|
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",
|
"Foobar",
|
||||||
&env,
|
&env,
|
||||||
)
|
)
|
||||||
|
Loading…
Reference in New Issue
Block a user