mirror of
https://github.com/nushell/nushell.git
synced 2025-04-15 16:58:19 +02:00
# Description Working on uniformizing the ending messages regarding methods usage() and extra_usage(). This is related to the issue https://github.com/nushell/nushell/issues/5066 after discussing it with @jntrnr # User-Facing Changes None. # 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.
91 lines
2.7 KiB
Rust
91 lines
2.7 KiB
Rust
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)),
|
|
},
|
|
]
|
|
}
|
|
}
|