Skip to content

Commit 63ba970

Browse files
committed
fix env spacing and " issues
1 parent 09c0fa7 commit 63ba970

File tree

6 files changed

+37
-9
lines changed

6 files changed

+37
-9
lines changed

Cargo.lock

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ default-members = ["compute"]
77

88
[workspace.package]
99
edition = "2021"
10-
version = "0.2.15"
10+
version = "0.2.16"
1111
license = "Apache-2.0"
1212
readme = "README.md"
1313

workflows/src/apis/jina.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ use eyre::{eyre, Context, Result};
22
use reqwest::Client;
33
use std::env;
44

5+
use crate::utils::safe_read_env;
6+
57
/// Makes a request for `example.com`.
68
const JINA_EXAMPLE_ENDPOINT: &str = "https://r.jina.ai/https://example.com";
79
const ENV_VAR_NAME: &str = "JINA_API_KEY";
@@ -17,7 +19,7 @@ impl JinaConfig {
1719
/// Looks at the environment variables for Jina API key.
1820
pub fn new() -> Self {
1921
Self {
20-
api_key: env::var(ENV_VAR_NAME).ok(),
22+
api_key: safe_read_env(env::var(ENV_VAR_NAME)),
2123
}
2224
}
2325

workflows/src/apis/serper.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ use eyre::{eyre, Context, Result};
22
use reqwest::Client;
33
use std::env;
44

5+
use crate::utils::safe_read_env;
6+
57
/// Makes a search request.
68
const SERPER_EXAMPLE_ENDPOINT: &str = "https://google.serper.dev/search";
79
const ENV_VAR_NAME: &str = "SERPER_API_KEY";
@@ -17,7 +19,7 @@ impl SerperConfig {
1719
/// Looks at the environment variables for Serper API key.
1820
pub fn new() -> Self {
1921
Self {
20-
api_key: env::var(ENV_VAR_NAME).ok(),
22+
api_key: safe_read_env(env::var(ENV_VAR_NAME)),
2123
}
2224
}
2325

@@ -46,6 +48,7 @@ impl SerperConfig {
4648
log::debug!("Serper API key not found, skipping Serper check");
4749
return Ok(());
4850
};
51+
println!("API KEY: {}", api_key);
4952
log::info!("Serper API key found, checking Serper service");
5053

5154
// make a dummy request

workflows/src/providers/openai.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ use ollama_workflows::Model;
33
use reqwest::Client;
44
use serde::Deserialize;
55

6+
use crate::utils::safe_read_env;
7+
68
const OPENAI_MODELS_API: &str = "https://api.openai.com/v1/models";
9+
const ENV_VAR_NAME: &str = "OPENAI_API_KEY";
710

811
/// [Model](https://platform.openai.com/docs/api-reference/models/object) API object.
912
#[derive(Debug, Clone, Deserialize)]
@@ -39,7 +42,7 @@ impl OpenAIConfig {
3942
/// Looks at the environment variables for OpenAI API key.
4043
pub fn new() -> Self {
4144
Self {
42-
api_key: std::env::var("OPENAI_API_KEY").ok(),
45+
api_key: safe_read_env(std::env::var(ENV_VAR_NAME)),
4346
}
4447
}
4548

workflows/src/utils.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,40 @@ pub fn split_csv_line(input: &str) -> Vec<String> {
1717
.collect::<Vec<_>>()
1818
}
1919

20+
/// Reads an environment variable and trims whitespace and `"` from both ends.
21+
/// If the trimmed value is empty, returns `None`.
22+
pub fn safe_read_env(var: Result<String, std::env::VarError>) -> Option<String> {
23+
var.map(|s| s.trim_matches('"').trim().to_string())
24+
.ok()
25+
.filter(|s| !s.is_empty())
26+
}
27+
2028
#[cfg(test)]
2129
mod tests {
2230
use super::*;
2331

2432
#[test]
25-
fn test_example() {
33+
fn test_abc_csv() {
2634
// should ignore whitespaces and `"` at both ends, and ignore empty items
2735
let input = "\"a, b , c ,, \"";
2836
let expected = vec!["a".to_string(), "b".to_string(), "c".to_string()];
2937
assert_eq!(split_csv_line(input), expected);
3038
}
3139

3240
#[test]
33-
fn test_empty() {
41+
fn test_empty_csv() {
3442
assert!(split_csv_line(Default::default()).is_empty());
3543
}
44+
45+
#[test]
46+
fn test_var_read() {
47+
let var = Ok("\" value \"".to_string());
48+
assert_eq!(safe_read_env(var), Some("value".to_string()));
49+
50+
let var = Ok("\" \"".to_string());
51+
assert!(safe_read_env(var).is_none());
52+
53+
let var = Err(std::env::VarError::NotPresent);
54+
assert!(safe_read_env(var).is_none());
55+
}
3656
}

0 commit comments

Comments
 (0)