Skip to content

Commit edca74b

Browse files
committed
Extract get_var_if_exists into common code
And use it for MUVM_UDEVD_PATH Signed-off-by: Val Packett <[email protected]>
1 parent 3b2ce57 commit edca74b

File tree

3 files changed

+14
-11
lines changed

3 files changed

+14
-11
lines changed

crates/muvm/src/config.rs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,18 @@
11
use std::{
2-
env::{self, VarError},
3-
io,
2+
env, io,
43
path::{Path, PathBuf},
54
};
65

76
use anyhow::{Context, Result};
87
use serde::{Deserialize, Serialize};
98

9+
use crate::utils::env::get_var_if_exists;
10+
1011
#[derive(Deserialize, Serialize, Default)]
1112
pub struct Configuration {
1213
pub execute_pre: Option<PathBuf>,
1314
}
1415

15-
fn get_var_if_exists(name: &str) -> Option<Result<String>> {
16-
match env::var(name) {
17-
Ok(val) => Some(Ok(val)),
18-
Err(VarError::NotPresent) => None,
19-
Err(e) => Some(Err(e.into())),
20-
}
21-
}
22-
2316
fn get_user_config_path() -> Result<PathBuf> {
2417
let mut path = get_var_if_exists("XDG_CONFIG_DIR").map_or_else(
2518
|| {

crates/muvm/src/guest/bin/muvm-guest.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use muvm::guest::server::server_main;
1717
use muvm::guest::socket::setup_socket_proxy;
1818
use muvm::guest::user::setup_user;
1919
use muvm::guest::x11::setup_x11_forwarding;
20+
use muvm::utils::env::get_var_if_exists;
2021
use muvm::utils::launch::{Emulator, GuestConfiguration, PULSE_SOCKET};
2122
use nix::unistd::{Gid, Uid};
2223
use rustix::process::{getrlimit, setrlimit, Resource};
@@ -82,7 +83,8 @@ fn main() -> Result<ExitCode> {
8283
None => "/usr/lib/systemd/systemd-udevd",
8384
};
8485
Command::new(
85-
env::var("MUVM_UDEVD_PATH").unwrap_or_else(|_| DEFAULT_UDEVD_PATH.parse().unwrap()),
86+
get_var_if_exists("MUVM_UDEVD_PATH")
87+
.unwrap_or_else(|| Ok(DEFAULT_UDEVD_PATH.to_owned()))?,
8688
)
8789
.spawn()
8890
.context("Failed to execute `systemd-udevd` as a child process")?;

crates/muvm/src/utils/env.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,11 @@ where
3232

3333
Ok(None)
3434
}
35+
36+
pub fn get_var_if_exists(name: &str) -> Option<Result<String>> {
37+
match env::var(name) {
38+
Ok(val) => Some(Ok(val)),
39+
Err(env::VarError::NotPresent) => None,
40+
Err(e) => Some(Err(e.into())),
41+
}
42+
}

0 commit comments

Comments
 (0)