Skip to content

Commit 60927a1

Browse files
refactor(server): prefer #[cfg] on fields over #[cfg_attr(not(...), allow(dead_code))]
Gate driver-specific fields on `ServerStartupConfig` and `DriverStartupContext` (and the `GuestTlsPaths` type they reference) with `#[cfg]` directly instead of double-negated `#[cfg_attr(not(any(...)), allow(dead_code))]`. The direct form makes the struct definition accurately reflect what exists per build and removes the drift risk where a new driver reading one of these fields would silently re-suppress the warning if the exclusion predicate went stale. Every construction site (`run_server`, `prepare_server_config`, `test_context_with_endpoint_overrides`, `test_driver_startup`) is updated to gate the matching field assignments with the same predicate, so the gates cannot drift apart. Split `apply_runtime_defaults` into a pure-side-effect `Result<()>` plus a separate `resolved_local_tls` accessor so the call site no longer needs a two-branch `#[cfg]` split around `let _ = ...` just to keep the return value from being unused under `--no-default-features`. The two are now clean concerns: one mutates args, one reads TLS paths off disk. Callers invoke whichever they need. Signed-off-by: Dimitar Mirchev <[email protected]>
1 parent 86a6880 commit 60927a1

3 files changed

Lines changed: 87 additions & 24 deletions

File tree

‎crates/openshell-server/src/cli.rs‎

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ use tracing::{info, warn};
1414
use tracing_subscriber::EnvFilter;
1515

1616
use crate::certgen;
17+
#[cfg(any(
18+
feature = "driver-docker",
19+
feature = "driver-podman",
20+
feature = "driver-vm"
21+
))]
1722
use crate::compute::driver_config::GuestTlsPaths;
1823
use crate::config_file::{self, ConfigFile, GatewayFileSection};
1924
use crate::defaults::{self, LocalTlsPaths};
@@ -247,8 +252,13 @@ fn prepare_server_config(args: &mut RunArgs, matches: &ArgMatches) -> Result<Ser
247252
}
248253
normalize_compute_driver_socket_args(args, matches)?;
249254

250-
let local_tls = apply_runtime_defaults(args)?;
251-
let guest_tls = local_tls.as_ref().map(GuestTlsPaths::from);
255+
apply_runtime_defaults(args)?;
256+
#[cfg(any(
257+
feature = "driver-docker",
258+
feature = "driver-podman",
259+
feature = "driver-vm"
260+
))]
261+
let guest_tls = resolved_local_tls(args)?.as_ref().map(GuestTlsPaths::from);
252262
let local_jwt = defaults::complete_local_jwt_config()?;
253263

254264
let bind = SocketAddr::new(args.bind_address, args.port);
@@ -421,6 +431,11 @@ fn prepare_server_config(args: &mut RunArgs, matches: &ArgMatches) -> Result<Ser
421431
Ok(ServerStartupConfig {
422432
config,
423433
config_file: file,
434+
#[cfg(any(
435+
feature = "driver-docker",
436+
feature = "driver-podman",
437+
feature = "driver-vm"
438+
))]
424439
guest_tls,
425440
})
426441
}
@@ -494,12 +509,8 @@ fn resolve_config_path(args: &RunArgs) -> Result<Option<PathBuf>> {
494509
Ok(default_path.is_file().then_some(default_path))
495510
}
496511

497-
fn apply_runtime_defaults(args: &mut RunArgs) -> Result<Option<LocalTlsPaths>> {
498-
let local_tls = if args.disable_tls {
499-
None
500-
} else {
501-
defaults::complete_local_tls_paths()?
502-
};
512+
fn apply_runtime_defaults(args: &mut RunArgs) -> Result<()> {
513+
let local_tls = resolved_local_tls(args)?;
503514

504515
if args.db_url.is_none() {
505516
args.db_url = Some(defaults::default_database_url()?);
@@ -516,7 +527,17 @@ fn apply_runtime_defaults(args: &mut RunArgs) -> Result<Option<LocalTlsPaths>> {
516527
args.tls_client_ca = Some(paths.ca.clone());
517528
}
518529

519-
Ok(local_tls)
530+
Ok(())
531+
}
532+
533+
/// Read the local TLS bundle if TLS is not disabled and a complete bundle
534+
/// exists on disk.
535+
fn resolved_local_tls(args: &RunArgs) -> Result<Option<LocalTlsPaths>> {
536+
if args.disable_tls {
537+
Ok(None)
538+
} else {
539+
defaults::complete_local_tls_paths()
540+
}
520541
}
521542

522543
/// Returns `true` when an argument's value came from clap's built-in default
@@ -1156,7 +1177,8 @@ mod tests {
11561177
let _g2 = EnvVarGuard::set("XDG_STATE_HOME", tmp.path().to_str().unwrap());
11571178

11581179
let (mut args, _) = parse_with_args(&["openshell-gateway", "--disable-tls"]);
1159-
let local_tls = super::apply_runtime_defaults(&mut args).unwrap();
1180+
super::apply_runtime_defaults(&mut args).unwrap();
1181+
let local_tls = super::resolved_local_tls(&args).unwrap();
11601182

11611183
let expected = format!(
11621184
"sqlite:{}",
@@ -1195,7 +1217,8 @@ mod tests {
11951217
}
11961218

11971219
let (mut args, _) = parse_with_args(&["openshell-gateway"]);
1198-
let local_tls = super::apply_runtime_defaults(&mut args)
1220+
super::apply_runtime_defaults(&mut args).unwrap();
1221+
let local_tls = super::resolved_local_tls(&args)
11991222
.unwrap()
12001223
.expect("complete bundle should be returned");
12011224

‎crates/openshell-server/src/compute/driver_config.rs‎

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88
//! It does not acquire, connect to, or start compute drivers.
99
1010
use crate::config_file;
11+
#[cfg(any(
12+
feature = "driver-docker",
13+
feature = "driver-podman",
14+
feature = "driver-vm"
15+
))]
1116
use crate::defaults::LocalTlsPaths;
1217
#[cfg(any(
1318
feature = "driver-kubernetes",
@@ -30,13 +35,23 @@ use std::path::PathBuf;
3035
#[cfg(feature = "driver-vm")]
3136
use super::VmComputeConfig;
3237

38+
#[cfg(any(
39+
feature = "driver-docker",
40+
feature = "driver-podman",
41+
feature = "driver-vm"
42+
))]
3343
#[derive(Debug, Clone, PartialEq, Eq)]
3444
pub struct GuestTlsPaths {
3545
ca: PathBuf,
3646
cert: PathBuf,
3747
key: PathBuf,
3848
}
3949

50+
#[cfg(any(
51+
feature = "driver-docker",
52+
feature = "driver-podman",
53+
feature = "driver-vm"
54+
))]
4055
impl From<&LocalTlsPaths> for GuestTlsPaths {
4156
fn from(paths: &LocalTlsPaths) -> Self {
4257
Self {
@@ -51,21 +66,15 @@ impl From<&LocalTlsPaths> for GuestTlsPaths {
5166
pub struct DriverStartupContext<'a> {
5267
pub file: Option<&'a config_file::ConfigFile>,
5368
// Consumed only by the docker/podman/vm runtime-defaults helpers below.
54-
#[cfg_attr(
55-
not(any(
56-
feature = "driver-docker",
57-
feature = "driver-podman",
58-
feature = "driver-vm"
59-
)),
60-
allow(dead_code)
61-
)]
69+
#[cfg(any(
70+
feature = "driver-docker",
71+
feature = "driver-podman",
72+
feature = "driver-vm"
73+
))]
6274
pub guest_tls: Option<&'a GuestTlsPaths>,
63-
#[cfg_attr(
64-
not(any(feature = "driver-podman", feature = "driver-vm")),
65-
allow(dead_code)
66-
)]
75+
#[cfg(any(feature = "driver-podman", feature = "driver-vm"))]
6776
pub gateway_port: u16,
68-
#[cfg_attr(not(feature = "driver-vm"), allow(dead_code))]
77+
#[cfg(feature = "driver-vm")]
6978
pub gateway_tls_enabled: bool,
7079
pub endpoint_overrides: &'a BTreeMap<String, PathBuf>,
7180
}
@@ -295,8 +304,15 @@ mod tests {
295304
) -> DriverStartupContext<'a> {
296305
DriverStartupContext {
297306
file,
307+
#[cfg(any(
308+
feature = "driver-docker",
309+
feature = "driver-podman",
310+
feature = "driver-vm"
311+
))]
298312
guest_tls: None,
313+
#[cfg(any(feature = "driver-podman", feature = "driver-vm"))]
299314
gateway_port: openshell_core::config::DEFAULT_SERVER_PORT,
315+
#[cfg(feature = "driver-vm")]
300316
gateway_tls_enabled: false,
301317
endpoint_overrides,
302318
}

‎crates/openshell-server/src/lib.rs‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ use tracing_bus::TracingLogBus;
8080
pub(crate) struct ServerStartupConfig {
8181
pub config: Config,
8282
pub config_file: Option<config_file::ConfigFile>,
83+
#[cfg(any(
84+
feature = "driver-docker",
85+
feature = "driver-podman",
86+
feature = "driver-vm"
87+
))]
8388
pub guest_tls: Option<compute::driver_config::GuestTlsPaths>,
8489
}
8590

@@ -217,6 +222,11 @@ pub(crate) async fn run_server(
217222
let ServerStartupConfig {
218223
config,
219224
config_file,
225+
#[cfg(any(
226+
feature = "driver-docker",
227+
feature = "driver-podman",
228+
feature = "driver-vm"
229+
))]
220230
guest_tls,
221231
} = startup;
222232

@@ -250,8 +260,15 @@ pub(crate) async fn run_server(
250260
let supervisor_sessions = Arc::new(supervisor_session::SupervisorSessionRegistry::new());
251261
let driver_startup = compute::driver_config::DriverStartupContext {
252262
file: config_file.as_ref(),
263+
#[cfg(any(
264+
feature = "driver-docker",
265+
feature = "driver-podman",
266+
feature = "driver-vm"
267+
))]
253268
guest_tls: guest_tls.as_ref(),
269+
#[cfg(any(feature = "driver-podman", feature = "driver-vm"))]
254270
gateway_port: config.bind_address.port(),
271+
#[cfg(feature = "driver-vm")]
255272
gateway_tls_enabled: config.tls.is_some(),
256273
endpoint_overrides: &config.compute_driver_endpoints,
257274
};
@@ -1014,8 +1031,15 @@ mod tests {
10141031
) -> crate::compute::driver_config::DriverStartupContext<'a> {
10151032
crate::compute::driver_config::DriverStartupContext {
10161033
file,
1034+
#[cfg(any(
1035+
feature = "driver-docker",
1036+
feature = "driver-podman",
1037+
feature = "driver-vm"
1038+
))]
10171039
guest_tls: None,
1040+
#[cfg(any(feature = "driver-podman", feature = "driver-vm"))]
10181041
gateway_port: openshell_core::config::DEFAULT_SERVER_PORT,
1042+
#[cfg(feature = "driver-vm")]
10191043
gateway_tls_enabled: false,
10201044
endpoint_overrides: &config.compute_driver_endpoints,
10211045
}

0 commit comments

Comments
 (0)