2020-12-19 07:25:03 +01:00
|
|
|
use crate::prelude::*;
|
2021-01-10 03:50:49 +01:00
|
|
|
use nu_engine::{evaluate_baseline_expr, WholeStreamCommand};
|
2020-12-19 07:25:03 +01:00
|
|
|
|
|
|
|
use nu_errors::ShellError;
|
2021-01-03 07:44:21 +01:00
|
|
|
use nu_protocol::{hir::CapturedBlock, hir::ClassifiedCommand, Signature, SyntaxShape};
|
2020-12-19 07:25:03 +01:00
|
|
|
use nu_source::Tagged;
|
|
|
|
|
2021-01-05 00:30:55 +01:00
|
|
|
pub struct LetEnv;
|
2020-12-19 07:25:03 +01:00
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
2021-01-05 00:30:55 +01:00
|
|
|
pub struct LetEnvArgs {
|
2020-12-19 07:25:03 +01:00
|
|
|
pub name: Tagged<String>,
|
|
|
|
pub equals: Tagged<String>,
|
|
|
|
pub rhs: CapturedBlock,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait]
|
2021-01-05 00:30:55 +01:00
|
|
|
impl WholeStreamCommand for LetEnv {
|
2020-12-19 07:25:03 +01:00
|
|
|
fn name(&self) -> &str {
|
2021-01-05 00:30:55 +01:00
|
|
|
"let-env"
|
2020-12-19 07:25:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> Signature {
|
2021-01-05 00:30:55 +01:00
|
|
|
Signature::build("let-env")
|
2020-12-19 07:25:03 +01:00
|
|
|
.required(
|
|
|
|
"name",
|
|
|
|
SyntaxShape::String,
|
|
|
|
"the name of the environment variable",
|
|
|
|
)
|
|
|
|
.required("equals", SyntaxShape::String, "the equals sign")
|
|
|
|
.required(
|
|
|
|
"expr",
|
2020-12-21 05:32:06 +01:00
|
|
|
SyntaxShape::MathExpression,
|
2021-01-05 00:30:55 +01:00
|
|
|
"the value for the environment variable",
|
2020-12-19 07:25:03 +01:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn usage(&self) -> &str {
|
2021-01-05 00:30:55 +01:00
|
|
|
"Create an environment variable and give it a value."
|
2020-12-19 07:25:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2021-01-05 00:30:55 +01:00
|
|
|
let (LetEnvArgs { name, rhs, .. }, _) = args.process().await?;
|
2020-12-19 07:25:03 +01:00
|
|
|
|
|
|
|
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();
|
|
|
|
|
2021-01-03 07:44:21 +01:00
|
|
|
// 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())
|
2020-12-19 07:25:03 +01:00
|
|
|
}
|