use std::path::Path;
use crate::bridge::{Bridge, ShiftOutcome};
use crate::error::{Error, Result};
use crate::manifest;
use crate::scope::QosPhase;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum QosRequest {
Show,
Shift(QosPhase),
}
impl QosRequest {
pub fn from_flags(steady: bool, startup: bool, show: bool) -> Result<Self> {
if steady && startup {
return Err(Error::ConfigValidation(
"--steady and --startup are mutually exclusive".to_string(),
));
}
Ok(if show || !(steady || startup) {
Self::Show
} else if steady {
Self::Shift(QosPhase::Steady)
} else {
Self::Shift(QosPhase::Startup)
})
}
}
pub fn handle_qos(
config_path: &Path,
verbose: bool,
jail: &str,
request: QosRequest,
) -> Result<()> {
let config = manifest::load(config_path)?;
let bridge = Bridge::open(config, verbose)?;
let target = match request {
QosRequest::Show => {
let phase = bridge.qos_phase(jail)?;
println!("{}: {}", jail, phase.as_str());
return Ok(());
}
QosRequest::Shift(target) => target,
};
match bridge.shift_qos(jail, target)? {
ShiftOutcome::Shifted => {
println!("{}: shifted to {} profile", jail, target.as_str());
}
ShiftOutcome::AlreadyInPhase => {
println!("{}: already in {} profile", jail, target.as_str());
}
ShiftOutcome::NotConfigured => {
println!("{jail}: no [jails.qos] block configured; resource limits are static");
}
}
Ok(())
}