mod config;
mod doctor;
mod export;
mod isolation;
mod keys;
mod measure;
mod models;
mod ports;
mod profile;
mod reclaim;
mod stale;
mod theme;
mod tui;
use crate::engine::Engine;
use crate::{Cli, Cmd, GlobalOpts};
use anyhow::{anyhow, Result};
use oomtop_config::layered::{load_layered, LoadOptions, Loaded};
use oomtop_config::paths::ConfigPaths;
use oomtop_core::units::{format_bytes, UnitSystem};
use std::io::{BufRead, IsTerminal, Write};
#[derive(Debug)]
pub struct UsageError(pub String);
impl std::fmt::Display for UsageError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(&self.0)
}
}
impl std::error::Error for UsageError {}
pub fn usage<T>(msg: impl Into<String>) -> Result<T> {
Err(anyhow::Error::new(UsageError(msg.into())))
}
pub struct Ctx {
pub global: GlobalOpts,
pub opts: LoadOptions,
pub loaded: Loaded,
pub no_learn: bool,
}
impl Ctx {
pub fn new(global: GlobalOpts) -> Result<Self> {
let mut flags: Vec<(String, String)> = Vec::new();
for kv in &global.set {
let (k, v) = kv
.split_once('=')
.ok_or_else(|| UsageError(format!("--set expects KEY=VALUE, got {kv:?}")))?;
let k = k.trim();
if k.is_empty() {
return usage(format!("--set expects KEY=VALUE, got {kv:?}"));
}
flags.push((k.to_string(), v.trim().to_string()));
}
let quote = |s: &str| toml::Value::String(s.to_string()).to_string();
if let Some(t) = &global.theme {
flags.push(("appearance.theme".into(), quote(t)));
}
if let Some(c) = global.color {
flags.push(("appearance.color".into(), quote(c.as_str())));
}
if global.ascii {
flags.push(("appearance.glyphs".into(), quote("ascii")));
}
if global.no_learn {
flags.push(("personalization.learn".into(), "false".into()));
}
let opts = LoadOptions {
config_path: global.config.clone(),
flags,
..Default::default()
};
let loaded = load_layered(&opts);
let no_learn = global.no_learn || !loaded.config.personalization.learn;
Ok(Ctx {
global,
opts,
loaded,
no_learn,
})
}
fn warn_config_errors(&self, creating_config: bool) {
let missing = self.missing_explicit_config();
for e in &self.loaded.errors {
if creating_config && missing.is_some() && e.path.as_deref() == missing {
continue;
}
eprintln!("oomtop: config: {e}");
}
}
fn missing_explicit_config(&self) -> Option<&std::path::Path> {
self.opts.explicit_config().filter(|p| !p.exists())
}
pub fn config(&self) -> &oomtop_config::Config {
&self.loaded.config
}
pub fn paths(&self) -> ConfigPaths {
self.opts.paths()
}
pub fn units(&self) -> UnitSystem {
match self.loaded.config.format.memory_units {
oomtop_config::model::MemoryUnits::Si => UnitSystem::Si,
oomtop_config::model::MemoryUnits::Iec => UnitSystem::Iec,
}
}
pub fn fmt(&self, b: u64) -> String {
format_bytes(b, self.units(), self.loaded.config.format.decimals as usize)
}
pub fn fmt_opt(&self, b: Option<u64>) -> String {
b.map(|b| self.fmt(b)).unwrap_or_else(|| "n/a".into())
}
pub fn replaying(&self) -> bool {
self.global.replay.is_some()
}
pub fn state(&self) -> Option<oomtop_state::StateDb> {
if self.replaying() {
return None;
}
match oomtop_state::StateDb::open_default() {
Ok(mut db) => {
db.set_learning(!self.no_learn);
let p = &self.loaded.config.personalization;
db.set_limits(p.retention_days, p.log_max_mb);
Some(db)
}
Err(e) => {
eprintln!("oomtop: state: {e} (continuing without the lineage journal)");
None
}
}
}
pub fn engine(&self, on_demand: bool) -> Result<Engine> {
self.build_engine(on_demand, false)
}
pub fn engine_read_only(&self, on_demand: bool) -> Result<Engine> {
self.build_engine(on_demand, true)
}
fn build_engine(&self, on_demand: bool, read_only: bool) -> Result<Engine> {
let state = if read_only && !oomtop_state::default_path().is_file() {
None
} else {
self.state()
};
let dir = self.opts.user_dir();
let mut e = match &self.global.replay {
Some(p) => Engine::replay_in(&self.loaded.config, p, Some(&dir)).map_err(|e| anyhow!(e))?,
None => Engine::live_in(
&self.loaded.config,
state,
on_demand,
self.global.offline || !self.loaded.config.adapters.enabled,
Some(&dir),
),
};
e.set_learning(!self.no_learn);
if read_only {
e.set_state_read_only();
}
for err in &e.rule_errors {
eprintln!("oomtop: rules: {err}");
}
Ok(e)
}
}
pub fn out(s: &str) {
let mut o = std::io::stdout().lock();
let _ = o.write_all(s.as_bytes());
let _ = o.flush();
}
pub fn json_pretty<T: serde::Serialize>(v: &T) -> Result<String> {
Ok(serde_json::to_string_pretty(v)? + "\n")
}
pub fn err_out(s: &str) {
let mut o = std::io::stderr().lock();
let _ = o.write_all(s.as_bytes());
let _ = o.flush();
}
pub fn confirm(question: &str) -> Result<Option<bool>> {
if !std::io::stdin().is_terminal() {
return Ok(None);
}
eprint!("{question} [y/N] ");
let _ = std::io::stderr().flush();
let mut line = String::new();
std::io::stdin().lock().read_line(&mut line)?;
Ok(Some(matches!(
line.trim().to_ascii_lowercase().as_str(),
"y" | "yes"
)))
}
pub fn dispatch(cli: Cli) -> Result<i32> {
let ctx = Ctx::new(cli.global)?;
let reports_errors = matches!(
cli.command,
Some(Cmd::Config {
cmd: crate::ConfigCmd::Validate
}) | Some(Cmd::Doctor { .. })
);
let creating_config = matches!(
cli.command,
Some(Cmd::Config {
cmd: crate::ConfigCmd::Init { .. } | crate::ConfigCmd::Edit { .. } | crate::ConfigCmd::Set { .. }
})
);
if !creating_config {
if let Some(p) = ctx.missing_explicit_config() {
return usage(format!(
"config file not found: {} (named by --config / OOMTOP_CONFIG; create it with `oomtop --config {} config init`)",
p.display(),
p.display()
));
}
}
if !reports_errors {
ctx.warn_config_errors(creating_config);
}
match cli.command {
None => tui::run(&ctx),
Some(Cmd::Headroom(a)) => measure::headroom(&ctx, a),
Some(Cmd::Why { json }) => measure::why(&ctx, json),
Some(Cmd::Models { json, no_hash }) => models::run(&ctx, json, no_hash),
Some(Cmd::Ports { json, exposed, links }) => ports::run(&ctx, json, exposed, links),
Some(Cmd::Reclaim(a)) => reclaim::run(&ctx, a),
Some(Cmd::Sandboxes { json }) => isolation::sandboxes(&ctx, json),
Some(Cmd::Ns { json }) => isolation::ns(&ctx, json),
Some(Cmd::Stale { json, limit }) => stale::run(&ctx, json, limit),
Some(Cmd::Json { compact }) => export::json(&ctx, compact),
Some(Cmd::Ndjson {
interval,
count,
discard,
}) => export::ndjson(&ctx, interval, count, discard),
Some(Cmd::Serve {
listen,
allow_host,
max_requests,
}) => export::serve(&ctx, listen, allow_host, max_requests),
Some(Cmd::Mcp { allow_actions }) => export::mcp(&ctx, allow_actions),
Some(Cmd::Doctor { json, no_probe }) => doctor::run(&ctx, json, no_probe),
Some(Cmd::Config { cmd }) => config::run(&ctx, cmd),
Some(Cmd::Theme { cmd }) => theme::run(&ctx, cmd),
Some(Cmd::Keys { cmd }) => keys::run(&ctx, cmd),
Some(Cmd::Profile { cmd }) => profile::run(&ctx, cmd),
}
}