forked from extern/nushell
Continuation of #8229 and #8326 # Description The `ShellError` enum at the moment is kind of messy. Many variants are basic tuple structs where you always have to reference the implementation with its macro invocation to know which field serves which purpose. Furthermore we have both variants that are kind of redundant or either overly broad to be useful for the user to match on or overly specific with few uses. So I set out to start fixing the lacking documentation and naming to make it feasible to critically review the individual usages and fix those. Furthermore we can decide to join or split up variants that don't seem to be fit for purpose. # Call to action **Everyone:** Feel free to add review comments if you spot inconsistent use of `ShellError` variants. # User-Facing Changes (None now, end goal more explicit and consistent error messages) # Tests + Formatting (No additional tests needed so far) # Commits (so far) - Remove `ShellError::FeatureNotEnabled` - Name fields on `SE::ExternalNotSupported` - Name field on `SE::InvalidProbability` - Name fields on `SE::NushellFailed` variants - Remove unused `SE::NushellFailedSpannedHelp` - Name field on `SE::VariableNotFoundAtRuntime` - Name fields on `SE::EnvVarNotFoundAtRuntime` - Name fields on `SE::ModuleNotFoundAtRuntime` - Remove usused `ModuleOrOverlayNotFoundAtRuntime` - Name fields on `SE::OverlayNotFoundAtRuntime` - Name field on `SE::NotFound`
73 lines
2.3 KiB
Rust
73 lines
2.3 KiB
Rust
use nu_engine::{eval_expression_with_input, CallExt};
|
|
use nu_protocol::ast::Call;
|
|
use nu_protocol::engine::{Command, EngineState, Stack};
|
|
use nu_protocol::{
|
|
Category, Example, PipelineData, ShellError, Signature, Spanned, SyntaxShape, Type, Value,
|
|
};
|
|
|
|
#[derive(Clone)]
|
|
pub struct LetEnv;
|
|
|
|
impl Command for LetEnv {
|
|
fn name(&self) -> &str {
|
|
"let-env"
|
|
}
|
|
|
|
fn usage(&self) -> &str {
|
|
"Create an environment variable and give it a value."
|
|
}
|
|
|
|
fn signature(&self) -> nu_protocol::Signature {
|
|
Signature::build("let-env")
|
|
.input_output_types(vec![(Type::Nothing, Type::Nothing)])
|
|
.allow_variants_without_examples(true)
|
|
.required("var_name", SyntaxShape::String, "variable name")
|
|
.required(
|
|
"initial_value",
|
|
SyntaxShape::Keyword(b"=".to_vec(), Box::new(SyntaxShape::Expression)),
|
|
"equals sign followed by value",
|
|
)
|
|
.category(Category::Env)
|
|
}
|
|
|
|
fn run(
|
|
&self,
|
|
engine_state: &EngineState,
|
|
stack: &mut Stack,
|
|
call: &Call,
|
|
input: PipelineData,
|
|
) -> Result<PipelineData, ShellError> {
|
|
// TODO: find and require the crossplatform restrictions on environment names
|
|
let env_var: Spanned<String> = call.req(engine_state, stack, 0)?;
|
|
|
|
let keyword_expr = call
|
|
.positional_nth(1)
|
|
.expect("checked through parser")
|
|
.as_keyword()
|
|
.expect("internal error: missing keyword");
|
|
|
|
let rhs =
|
|
eval_expression_with_input(engine_state, stack, keyword_expr, input, false, true)?
|
|
.0
|
|
.into_value(call.head);
|
|
|
|
if env_var.item == "FILE_PWD" || env_var.item == "PWD" {
|
|
return Err(ShellError::AutomaticEnvVarSetManually {
|
|
envvar_name: env_var.item,
|
|
span: env_var.span,
|
|
});
|
|
} else {
|
|
stack.add_env_var(env_var.item, rhs);
|
|
}
|
|
Ok(PipelineData::empty())
|
|
}
|
|
|
|
fn examples(&self) -> Vec<Example> {
|
|
vec![Example {
|
|
description: "Create an environment variable and display it",
|
|
example: "let-env MY_ENV_VAR = 1; $env.MY_ENV_VAR",
|
|
result: Some(Value::test_int(1)),
|
|
}]
|
|
}
|
|
}
|