Skip to content

Commit 71a5caa

Browse files
authored
Fix - provide default (empty) client config when default user config dir does not exist. Create an inner function to inject the default config path easily (temporalio#1194)
1 parent b3d53ab commit 71a5caa

1 file changed

Lines changed: 33 additions & 10 deletions

File tree

crates/common/src/envconfig.rs

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,14 @@ fn read_path_bytes(path: &str) -> Result<Option<Vec<u8>>, ConfigError> {
255255
pub fn load_client_config(
256256
options: LoadClientConfigOptions,
257257
env_vars: Option<&HashMap<String, String>>,
258+
) -> Result<ClientConfig, ConfigError> {
259+
load_client_config_inner(options, env_vars, get_default_config_file_path())
260+
}
261+
262+
fn load_client_config_inner(
263+
options: LoadClientConfigOptions,
264+
env_vars: Option<&HashMap<String, String>>,
265+
default_config_file_path: Option<String>,
258266
) -> Result<ClientConfig, ConfigError> {
259267
let env_provider = match env_vars {
260268
Some(map) => EnvProvider::Map(map),
@@ -270,11 +278,14 @@ pub fn load_client_config(
270278
.get("TEMPORAL_CONFIG_FILE")?
271279
.filter(|p| !p.is_empty())
272280
{
273-
path
281+
Some(path)
274282
} else {
275-
get_default_config_file_path()?
283+
default_config_file_path
276284
};
277-
read_path_bytes(&file_path)?
285+
match file_path {
286+
Some(file_path) => read_path_bytes(&file_path)?,
287+
None => None,
288+
}
278289
}
279290
};
280291

@@ -594,13 +605,14 @@ fn normalize_grpc_meta_key(key: &str) -> String {
594605
}
595606

596607
/// Get the default configuration file path
597-
fn get_default_config_file_path() -> Result<String, ConfigError> {
598-
// Try to get user config directory
599-
let config_dir = dirs::config_dir()
600-
.ok_or_else(|| ConfigError::InvalidConfig("failed getting user config dir".to_string()))?;
601-
602-
let path = config_dir.join("temporalio").join(DEFAULT_CONFIG_FILE);
603-
Ok(path.to_string_lossy().to_string())
608+
fn get_default_config_file_path() -> Option<String> {
609+
dirs::config_dir().map(|config_dir| {
610+
config_dir
611+
.join("temporalio")
612+
.join(DEFAULT_CONFIG_FILE)
613+
.to_string_lossy()
614+
.to_string()
615+
})
604616
}
605617

606618
#[derive(Debug, Clone, Serialize, Deserialize)]
@@ -1750,4 +1762,15 @@ address = "some-address"
17501762
Some(true)
17511763
);
17521764
}
1765+
1766+
#[test]
1767+
fn test_load_default_config_path_not_exist() {
1768+
let config = load_client_config_inner(
1769+
LoadClientConfigOptions::default(),
1770+
Some(&HashMap::new()),
1771+
None,
1772+
)
1773+
.unwrap();
1774+
assert_eq!(config, ClientConfig::default());
1775+
}
17531776
}

0 commit comments

Comments
 (0)