Skip to content

Commit d8941b0

Browse files
committed
feat: 优化环境变量占位符替换逻辑,环境变量不存在时返回空字符串而非错误
1 parent 9e1c941 commit d8941b0

File tree

2 files changed

+23
-24
lines changed

2 files changed

+23
-24
lines changed

ostool/src/run/uboot.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -268,9 +268,11 @@ impl Runner {
268268

269269
async fn run(&mut self) -> anyhow::Result<()> {
270270
let res = self._run().await;
271-
if let Some(cmd) = self.config.board_power_off_cmd.clone() {
271+
if let Some(ref cmd) = self.config.board_power_off_cmd
272+
&& !cmd.trim().is_empty()
273+
{
272274
info!("Executing board power off command: {}", cmd);
273-
let _ = self.ctx.shell_run_cmd(&cmd);
275+
let _ = self.ctx.shell_run_cmd(cmd);
274276
info!("Board powered off");
275277
}
276278
res
@@ -285,21 +287,19 @@ impl Runner {
285287
info!("Starting U-Boot runner...");
286288

287289
info!("kernel from: {}", kernel.display());
288-
if self.config.net.is_some() {
289-
tftp::run_tftp_server(&self.ctx)?;
290-
}
291290

292291
let ip_string = self.detect_tftp_ip();
293292

293+
if let Some(ip) = ip_string.as_ref() {
294+
info!("TFTP server IP: {}", ip);
295+
tftp::run_tftp_server(&self.ctx)?;
296+
}
297+
294298
info!(
295299
"Opening serial port: {} @ {}",
296300
self.config.serial, self.baud_rate
297301
);
298302

299-
if let Some(ref ip) = ip_string {
300-
info!("TFTP server IP: {}", ip);
301-
}
302-
303303
let rx = serialport::new(&self.config.serial, self.baud_rate as _)
304304
.timeout(Duration::from_millis(200))
305305
.open()
@@ -314,7 +314,9 @@ impl Runner {
314314
Ok(uboot)
315315
});
316316

317-
if let Some(cmd) = self.config.board_reset_cmd.clone() {
317+
if let Some(cmd) = self.config.board_reset_cmd.clone()
318+
&& !cmd.trim().is_empty()
319+
{
318320
info!("Executing board reset command: {}", cmd);
319321
self.ctx.shell_run_cmd(&cmd)?;
320322
}
@@ -487,8 +489,8 @@ impl Runner {
487489
}
488490
}
489491

490-
if ip_string.is_empty() {
491-
panic!("Cannot detect IP address for interface: {}", net.interface);
492+
if ip_string.trim().is_empty() {
493+
return None;
492494
}
493495

494496
info!("TFTP : {}", ip_string);

ostool/src/utils.rs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -168,15 +168,12 @@ pub fn replace_env_placeholders(input: &str) -> anyhow::Result<String> {
168168
if found_closing_brace && placeholder.starts_with("env:") {
169169
let env_var_name = &placeholder[4..]; // 跳过 "env:"
170170

171-
// 获取环境变量值,如果不存在则返回错误
171+
// 获取环境变量值,如果不存在则替换为空字符串
172172
match env::var(env_var_name) {
173173
Ok(value) => result.push_str(&value),
174174
Err(_) => {
175-
return Err(anyhow::anyhow!(
176-
"环境变量 '{}' 未设置,无法替换占位符 ${{env:{}}}",
177-
env_var_name,
178-
env_var_name
179-
));
175+
// 环境变量不存在时替换为空字符串,不返回错误
176+
result.push_str("");
180177
}
181178
}
182179
} else {
@@ -220,10 +217,10 @@ mod tests {
220217
"/home/test:/usr/local/bin"
221218
);
222219

223-
// 测试不存在的环境变量
220+
// 测试不存在的环境变量 - 应该返回空字符串而不是错误
224221
let result = replace_env_placeholders("${env:NON_EXISTENT}");
225-
assert!(result.is_err());
226-
assert!(result.unwrap_err().to_string().contains("未设置"));
222+
assert!(result.is_ok());
223+
assert_eq!(result.unwrap(), "");
227224

228225
// 测试混合内容
229226
assert_eq!(
@@ -276,10 +273,10 @@ mod tests {
276273
assert_eq!(replace_env_placeholders("${env:").unwrap(), "${env:");
277274
assert_eq!(replace_env_placeholders("${env:VAR").unwrap(), "${env:VAR");
278275

279-
// 测试空的env变量名
276+
// 测试空的env变量名 - 应该返回空字符串而不是错误
280277
let result = replace_env_placeholders("${env:}");
281-
assert!(result.is_err());
282-
assert!(result.unwrap_err().to_string().contains("未设置"));
278+
assert!(result.is_ok());
279+
assert_eq!(result.unwrap(), "");
283280

284281
// 测试只包含$的字符串
285282
assert_eq!(replace_env_placeholders("$").unwrap(), "$");

0 commit comments

Comments
 (0)