mirror of
https://github.com/nushell/nushell.git
synced 2025-06-30 22:50:14 +02:00
Move $nu.env to $env (#665)
* Move env from nu builtin to its own * update samples/tests
This commit is contained in:
8
crates/nu-command/src/env/with_env.rs
vendored
8
crates/nu-command/src/env/with_env.rs
vendored
@ -48,22 +48,22 @@ impl Command for WithEnv {
|
||||
vec![
|
||||
Example {
|
||||
description: "Set the MYENV environment variable",
|
||||
example: r#"with-env [MYENV "my env value"] { $nu.env.MYENV }"#,
|
||||
example: r#"with-env [MYENV "my env value"] { $env.MYENV }"#,
|
||||
result: Some(Value::test_string("my env value")),
|
||||
},
|
||||
Example {
|
||||
description: "Set by primitive value list",
|
||||
example: r#"with-env [X Y W Z] { $nu.env.X }"#,
|
||||
example: r#"with-env [X Y W Z] { $env.X }"#,
|
||||
result: Some(Value::test_string("Y")),
|
||||
},
|
||||
Example {
|
||||
description: "Set by single row table",
|
||||
example: r#"with-env [[X W]; [Y Z]] { $nu.env.W }"#,
|
||||
example: r#"with-env [[X W]; [Y Z]] { $env.W }"#,
|
||||
result: Some(Value::test_string("Z")),
|
||||
},
|
||||
Example {
|
||||
description: "Set by row(e.g. `open x.json` or `from json`)",
|
||||
example: r#"echo '{"X":"Y","W":"Z"}'|from json|with-env $it { echo $nu.env.X $nu.env.W }"#,
|
||||
example: r#"echo '{"X":"Y","W":"Z"}'|from json|with-env $it { echo $env.X $env.W }"#,
|
||||
result: None,
|
||||
},
|
||||
]
|
||||
|
@ -5,7 +5,7 @@ use nu_protocol::ast::{Block, Call, Expr, Expression, Operator, Statement};
|
||||
use nu_protocol::engine::{EngineState, Stack};
|
||||
use nu_protocol::{
|
||||
IntoInterruptiblePipelineData, IntoPipelineData, PipelineData, Range, ShellError, Span,
|
||||
Spanned, Type, Unit, Value, VarId,
|
||||
Spanned, Type, Unit, Value, VarId, ENV_VARIABLE_ID,
|
||||
};
|
||||
|
||||
use crate::get_full_help;
|
||||
@ -530,26 +530,6 @@ pub fn eval_variable(
|
||||
let mut output_cols = vec![];
|
||||
let mut output_vals = vec![];
|
||||
|
||||
let env_vars = stack.get_env_vars();
|
||||
let env_columns = env_vars.keys();
|
||||
let env_values = env_vars.values();
|
||||
|
||||
let mut pairs = env_columns
|
||||
.map(|x| x.to_string())
|
||||
.zip(env_values.cloned())
|
||||
.collect::<Vec<(String, Value)>>();
|
||||
|
||||
pairs.sort_by(|a, b| a.0.cmp(&b.0));
|
||||
|
||||
let (env_columns, env_values) = pairs.into_iter().unzip();
|
||||
|
||||
output_cols.push("env".into());
|
||||
output_vals.push(Value::Record {
|
||||
cols: env_columns,
|
||||
vals: env_values,
|
||||
span,
|
||||
});
|
||||
|
||||
if let Some(mut config_path) = nu_path::config_dir() {
|
||||
config_path.push("nushell");
|
||||
|
||||
@ -905,6 +885,25 @@ pub fn eval_variable(
|
||||
vals: output_vals,
|
||||
span,
|
||||
})
|
||||
} else if var_id == ENV_VARIABLE_ID {
|
||||
let env_vars = stack.get_env_vars();
|
||||
let env_columns = env_vars.keys();
|
||||
let env_values = env_vars.values();
|
||||
|
||||
let mut pairs = env_columns
|
||||
.map(|x| x.to_string())
|
||||
.zip(env_values.cloned())
|
||||
.collect::<Vec<(String, Value)>>();
|
||||
|
||||
pairs.sort_by(|a, b| a.0.cmp(&b.0));
|
||||
|
||||
let (env_columns, env_values) = pairs.into_iter().unzip();
|
||||
|
||||
Ok(Value::Record {
|
||||
cols: env_columns,
|
||||
vals: env_values,
|
||||
span,
|
||||
})
|
||||
} else {
|
||||
stack
|
||||
.get_var(var_id)
|
||||
|
@ -495,10 +495,10 @@ pub fn parse_module_block(
|
||||
// TODO: Exported env vars are usable iside the module only if correctly
|
||||
// exported by the user. For example:
|
||||
//
|
||||
// > module foo { export env a { "2" }; export def b [] { $nu.env.a } }
|
||||
// > module foo { export env a { "2" }; export def b [] { $env.a } }
|
||||
//
|
||||
// will work only if you call `use foo *; b` but not with `use foo; foo b`
|
||||
// since in the second case, the name of the env var would be $nu.env."foo a".
|
||||
// since in the second case, the name of the env var would be $env."foo a".
|
||||
b"export" => {
|
||||
let (stmt, exportable, err) =
|
||||
parse_export(working_set, &pipeline.commands[0].parts);
|
||||
|
@ -1331,6 +1331,16 @@ pub fn parse_variable_expr(
|
||||
},
|
||||
None,
|
||||
);
|
||||
} else if contents == b"$env" {
|
||||
return (
|
||||
Expression {
|
||||
expr: Expr::Var(nu_protocol::ENV_VARIABLE_ID),
|
||||
span,
|
||||
ty: Type::Unknown,
|
||||
custom_completion: None,
|
||||
},
|
||||
None,
|
||||
);
|
||||
}
|
||||
|
||||
let (id, err) = parse_variable(working_set, span);
|
||||
|
@ -148,13 +148,20 @@ pub const NU_VARIABLE_ID: usize = 0;
|
||||
pub const SCOPE_VARIABLE_ID: usize = 1;
|
||||
pub const IN_VARIABLE_ID: usize = 2;
|
||||
pub const CONFIG_VARIABLE_ID: usize = 3;
|
||||
pub const ENV_VARIABLE_ID: usize = 4;
|
||||
|
||||
impl EngineState {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
files: im::vector![],
|
||||
file_contents: im::vector![],
|
||||
vars: im::vector![Type::Unknown, Type::Unknown, Type::Unknown, Type::Unknown],
|
||||
vars: im::vector![
|
||||
Type::Unknown,
|
||||
Type::Unknown,
|
||||
Type::Unknown,
|
||||
Type::Unknown,
|
||||
Type::Unknown
|
||||
],
|
||||
decls: im::vector![],
|
||||
blocks: im::vector![],
|
||||
overlays: im::vector![],
|
||||
|
@ -15,7 +15,9 @@ mod value;
|
||||
pub use value::Value;
|
||||
|
||||
pub use config::*;
|
||||
pub use engine::{CONFIG_VARIABLE_ID, IN_VARIABLE_ID, NU_VARIABLE_ID, SCOPE_VARIABLE_ID};
|
||||
pub use engine::{
|
||||
CONFIG_VARIABLE_ID, ENV_VARIABLE_ID, IN_VARIABLE_ID, NU_VARIABLE_ID, SCOPE_VARIABLE_ID,
|
||||
};
|
||||
pub use example::*;
|
||||
pub use exportable::*;
|
||||
pub use id::*;
|
||||
|
Reference in New Issue
Block a user