From 933dadc68bccc9e3fddd8030ee7c9fc68260ef7c Mon Sep 17 00:00:00 2001 From: haixuanTao Date: Wed, 15 Feb 2023 14:27:11 +0100 Subject: [PATCH] Expand `env` value when possible This commit enables users to pass environment variable as composition of env variable such as: ```yaml - id: yolov5 operator: outputs: - bbox - ready inputs: image: oasis_agent/image check: oasis_agent/yolov5_check python: ../../operators/yolov5_op.py env: YOLOV5_PATH: $HOME/Documents/dependencies/yolov5 YOLOV5_WEIGHT_PATH: $DORA_DEP_HOME/dependencies/yolov5/yolov5n.pt ``` Note that this is only for the env field. Expanding env variable for source path might conflict with using url as source path. --- Cargo.lock | 11 +++++++++++ libraries/core/Cargo.toml | 1 + libraries/core/src/descriptor/mod.rs | 4 ++++ 3 files changed, 16 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index bc54acde2..2f8cde29b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -931,6 +931,7 @@ dependencies = [ "eyre", "once_cell", "serde", + "serde-with-expand-env", "serde_yaml 0.9.11", "uuid 1.2.1", "which", @@ -3366,6 +3367,16 @@ dependencies = [ "serde_derive", ] +[[package]] +name = "serde-with-expand-env" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "888d884a3be3a209308d0b66f1918ff18f60e93db837259e53ea7d8dd14e7e98" +dependencies = [ + "serde", + "shellexpand", +] + [[package]] name = "serde_derive" version = "1.0.144" diff --git a/libraries/core/Cargo.toml b/libraries/core/Cargo.toml index 494e61604..42b0d368d 100644 --- a/libraries/core/Cargo.toml +++ b/libraries/core/Cargo.toml @@ -14,3 +14,4 @@ once_cell = "1.13.0" zenoh-config = { git = "https://github.com/eclipse-zenoh/zenoh.git", rev = "79a136e4fd90b11ff5d775ced981af53c4f1071b" } which = "4.3.0" uuid = { version = "1.2.1", features = ["serde"] } +serde-with-expand-env = "1.1.0" diff --git a/libraries/core/src/descriptor/mod.rs b/libraries/core/src/descriptor/mod.rs index d3ed04596..892c7a72f 100644 --- a/libraries/core/src/descriptor/mod.rs +++ b/libraries/core/src/descriptor/mod.rs @@ -1,6 +1,7 @@ use crate::config::{CommunicationConfig, DataId, InputMapping, NodeId, NodeRunConfig, OperatorId}; use eyre::{bail, Result}; use serde::{Deserialize, Serialize}; +use serde_with_expand_env::with_expand_envs; use std::{ collections::{BTreeMap, BTreeSet, HashMap}, env::consts::EXE_EXTENSION, @@ -222,8 +223,11 @@ pub struct CustomNode { #[derive(Debug, Clone, Serialize, Deserialize)] #[serde(untagged)] pub enum EnvValue { + #[serde(deserialize_with = "with_expand_envs")] Bool(bool), + #[serde(deserialize_with = "with_expand_envs")] Integer(u64), + #[serde(deserialize_with = "with_expand_envs")] String(String), }