mirror of
https://github.com/nushell/nushell.git
synced 2024-12-12 10:10:51 +01:00
d06f457b2a
* move commands, futures.rs, script.rs, utils * move over maybe_print_errors * add nu_command crate references to nu_cli * in commands.rs open up to pub mod from pub(crate) * nu-cli, nu-command, and nu tests are now passing * cargo fmt * clean up nu-cli/src/prelude.rs * code cleanup * for some reason lex.rs was not formatted, may be causing my error * remove mod completion from lib.rs which was not being used along with quickcheck macros * add in allow unused imports * comment out one failing external test; comment out one failing internal test * revert commenting out failing tests; something else might be going on; someone with a windows machine should check and see what is going on with these failing windows tests * Update Cargo.toml Extend the optional features to nu-command Co-authored-by: Jonathan Turner <jonathandturner@users.noreply.github.com>
105 lines
2.9 KiB
Rust
105 lines
2.9 KiB
Rust
use crate::prelude::*;
|
|
use nu_engine::{evaluate_baseline_expr, WholeStreamCommand};
|
|
|
|
use nu_errors::ShellError;
|
|
use nu_protocol::{hir::CapturedBlock, hir::ClassifiedCommand, Signature, SyntaxShape};
|
|
use nu_source::Tagged;
|
|
|
|
pub struct LetEnv;
|
|
|
|
#[derive(Deserialize)]
|
|
pub struct LetEnvArgs {
|
|
pub name: Tagged<String>,
|
|
pub equals: Tagged<String>,
|
|
pub rhs: CapturedBlock,
|
|
}
|
|
|
|
#[async_trait]
|
|
impl WholeStreamCommand for LetEnv {
|
|
fn name(&self) -> &str {
|
|
"let-env"
|
|
}
|
|
|
|
fn signature(&self) -> Signature {
|
|
Signature::build("let-env")
|
|
.required(
|
|
"name",
|
|
SyntaxShape::String,
|
|
"the name of the environment variable",
|
|
)
|
|
.required("equals", SyntaxShape::String, "the equals sign")
|
|
.required(
|
|
"expr",
|
|
SyntaxShape::MathExpression,
|
|
"the value for the environment variable",
|
|
)
|
|
}
|
|
|
|
fn usage(&self) -> &str {
|
|
"Create an environment variable and give it a value."
|
|
}
|
|
|
|
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
|
|
set_env(args).await
|
|
}
|
|
|
|
fn examples(&self) -> Vec<Example> {
|
|
vec![]
|
|
}
|
|
}
|
|
|
|
pub async fn set_env(args: CommandArgs) -> Result<OutputStream, ShellError> {
|
|
let tag = args.call_info.name_tag.clone();
|
|
let ctx = EvaluationContext::from_args(&args);
|
|
|
|
let (LetEnvArgs { name, rhs, .. }, _) = args.process().await?;
|
|
|
|
let (expr, captured) = {
|
|
if rhs.block.block.len() != 1 {
|
|
return Err(ShellError::labeled_error(
|
|
"Expected a value",
|
|
"expected a value",
|
|
tag,
|
|
));
|
|
}
|
|
match rhs.block.block[0].pipelines.get(0) {
|
|
Some(item) => match item.list.get(0) {
|
|
Some(ClassifiedCommand::Expr(expr)) => (expr.clone(), rhs.captured.clone()),
|
|
_ => {
|
|
return Err(ShellError::labeled_error(
|
|
"Expected a value",
|
|
"expected a value",
|
|
tag,
|
|
));
|
|
}
|
|
},
|
|
None => {
|
|
return Err(ShellError::labeled_error(
|
|
"Expected a value",
|
|
"expected a value",
|
|
tag,
|
|
));
|
|
}
|
|
}
|
|
};
|
|
|
|
ctx.scope.enter_scope();
|
|
ctx.scope.add_vars(&captured.entries);
|
|
|
|
let value = evaluate_baseline_expr(&expr, &ctx).await;
|
|
|
|
ctx.scope.exit_scope();
|
|
|
|
let value = value?;
|
|
let value = value.as_string()?;
|
|
|
|
let name = name.item.clone();
|
|
|
|
// Note: this is a special case for setting the context from a command
|
|
// In this case, if we don't set it now, we'll lose the scope that this
|
|
// variable should be set into.
|
|
ctx.scope.add_env_var(name, value);
|
|
|
|
Ok(OutputStream::empty())
|
|
}
|