Skip to content

Commit 3550645

Browse files
committed
avoid panics
1 parent d335054 commit 3550645

2 files changed

Lines changed: 26 additions & 19 deletions

File tree

crates/cli/src/docker_init.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ pub async fn handle_docker_init(config_path: PathBuf, output_dir: PathBuf) -> Re
257257

258258
if let Some(mux_config) = cb_config.muxes {
259259
for mux in mux_config.muxes.iter() {
260-
if let Some((env_name, actual_path, internal_path)) = mux.loader_env() {
260+
if let Some((env_name, actual_path, internal_path)) = mux.loader_env()? {
261261
let (key, val) = get_env_val(&env_name, &internal_path);
262262
pbs_envs.insert(key, val);
263263
pbs_volumes.push(Volumes::Simple(format!("{}:{}:ro", actual_path, internal_path)));

crates/common/src/config/mux.rs

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -120,29 +120,36 @@ pub struct MuxConfig {
120120
impl MuxConfig {
121121
/// Returns the env, actual path, and internal path to use for the file
122122
/// loader
123-
pub fn loader_env(&self) -> Option<(String, String, String)> {
124-
self.loader.as_ref().and_then(|loader| match loader {
125-
MuxKeysLoader::File(path_buf) => {
126-
if !path_buf.try_exists().is_ok_and(|exists| exists) {
127-
panic!("path doesn't exist: {:?}", path_buf);
128-
}
123+
pub fn loader_env(&self) -> eyre::Result<Option<(String, String, String)>> {
124+
let Some(loader) = self.loader.as_ref() else {
125+
return Ok(None);
126+
};
129127

130-
if !path_buf
131-
.file_name()
132-
.is_some_and(|name| name.to_string_lossy().to_lowercase().ends_with(".json"))
133-
{
134-
panic!("file doesn't have a .json extension");
135-
}
128+
match loader {
129+
MuxKeysLoader::File(path_buf) => {
130+
ensure!(
131+
path_buf.try_exists().is_ok_and(|exists| exists),
132+
"path doesn't exist: {:?}",
133+
path_buf
134+
);
135+
136+
ensure!(
137+
path_buf.extension().is_some_and(|ext| ext == "json"),
138+
"file doesn't have a .json extension: {:?}",
139+
path_buf
140+
);
141+
142+
let Some(path) = path_buf.to_str() else {
143+
bail!("invalid path: {:?}", path_buf);
144+
};
136145

137-
let path =
138-
path_buf.to_str().unwrap_or_else(|| panic!("invalid path: {:?}", path_buf));
139146
let internal_path = get_mux_path(&self.id);
140147

141-
Some((get_mux_env(&self.id), path.to_owned(), internal_path))
148+
Ok(Some((get_mux_env(&self.id), path.to_owned(), internal_path)))
142149
}
143-
MuxKeysLoader::HTTP { .. } => None,
144-
MuxKeysLoader::Registry { .. } => None,
145-
})
150+
MuxKeysLoader::HTTP { .. } => Ok(None),
151+
MuxKeysLoader::Registry { .. } => Ok(None),
152+
}
146153
}
147154
}
148155

0 commit comments

Comments
 (0)