Skip to content

Commit

Permalink
remove meta.with_env: you can now just set an env variable in the han…
Browse files Browse the repository at this point in the history
…dler nushell script preamble
  • Loading branch information
cablehead committed Dec 31, 2024
1 parent 03ab134 commit f87adb9
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 88 deletions.
31 changes: 0 additions & 31 deletions src/handlers/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ pub struct Meta {
pub start: StartFrom,
pub return_options: Option<ReturnOptions>,
pub modules: Option<HashMap<String, String>>, // module_name -> frame_id
pub with_env: Option<HashMap<String, String>>, // env_var -> frame_id
}

#[derive(Clone, Debug, Serialize, Deserialize, Default)]
Expand Down Expand Up @@ -107,36 +106,6 @@ impl Handler {
}
}

// Handle environment variables if specified
if let Some(env_vars) = &meta.with_env {
for (var_name, frame_id) in env_vars {
// Parse frame ID
let id = Scru128Id::from_str(frame_id)
.map_err(|e| format!("Invalid env var frame ID '{}': {}", frame_id, e))?;

// Get frame
let env_frame = store
.get(&id)
.ok_or_else(|| format!("Env var frame '{}' not found", frame_id))?;

// Get content from CAS
let hash = env_frame
.hash
.as_ref()
.ok_or_else(|| format!("Env var frame '{}' has no content hash", frame_id))?;

let content = store
.cas_read(hash)
.await
.map_err(|e| format!("Failed to read env var content: {}", e))?;

let content = String::from_utf8(content)
.map_err(|e| format!("Env var content is not valid UTF-8: {}", e))?;

engine = engine.with_env_vars([(var_name.clone(), content)])?;
}
}

let closure = engine.parse_closure(&expression)?;
let block = engine.state.get_block(closure.block_id);

Expand Down
57 changes: 0 additions & 57 deletions src/handlers/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -581,63 +581,6 @@ async fn test_handler_with_module() -> Result<(), Error> {
Ok(())
}

#[tokio::test]
async fn test_handler_with_env() -> Result<(), Error> {
let (store, _temp_dir) = setup_test_environment().await;
let options = ReadOptions::builder().follow(FollowOption::On).build();
let mut recver = store.read(options).await;
assert_eq!(recver.recv().await.unwrap().topic, "xs.threshold");

// Create a frame with some content for the env var
let env_frame = store.append(
Frame::with_topic("env-content")
.hash(store.cas_insert("hello world").await?)
.build(),
);
assert_eq!(recver.recv().await.unwrap().topic, "env-content");

// Create handler that uses the env var
let frame_handler = store.append(
Frame::with_topic("test.register")
.hash(
store
.cas_insert(
r#"{|frame|
if $frame.topic != "trigger" { return }
$env.TEST_VAR
}"#,
)
.await?,
)
.meta(serde_json::json!({
"with_env": {
"TEST_VAR": env_frame.id.to_string()
}
}))
.build(),
);

// Wait for handler registration
assert_eq!(recver.recv().await.unwrap().topic, "test.register");
assert_eq!(recver.recv().await.unwrap().topic, "test.registered");

// Send trigger frame
let trigger = store.append(Frame::with_topic("trigger").build());
assert_eq!(recver.recv().await.unwrap().topic, "trigger");

// Get handler output
let output = recver.recv().await.unwrap();
validate_handler_output_frame!(&output, "test.out", frame_handler, trigger, None);

// Verify output content shows the env var value
let content = store.cas_read(&output.hash.unwrap()).await?;
let result = String::from_utf8(content)?;
assert_eq!(result, r#""hello world""#);

assert_no_more_frames(&mut recver).await;
Ok(())
}

#[tokio::test]
async fn test_handler_preserve_env() -> Result<(), Error> {
let (store, _temp_dir) = setup_test_environment().await;
Expand Down

0 comments on commit f87adb9

Please sign in to comment.