pub mod matcher;
pub mod network;
pub use matcher::Env;
use crate::config::Config;
use std::collections::BTreeSet;
use std::path::PathBuf;
pub(crate) fn capture_stdout(label: &str, program: &str, args: &[&str]) -> Option<String> {
let out = match std::process::Command::new(program).args(args).output() {
Ok(out) => out,
Err(e) => {
tracing::debug!("{label}: spawning {program} failed: {e}");
return None;
}
};
if !out.status.success() {
tracing::debug!(
"{label}: {program} exited {}: {}",
out.status,
String::from_utf8_lossy(&out.stderr).trim()
);
return None;
}
String::from_utf8(out.stdout).ok()
}
#[derive(Debug, Clone)]
pub struct ActiveScope {
pub id: String,
pub kind: &'static str,
pub tags: Vec<String>,
pub project_root: Option<PathBuf>,
pub enable_bundles: Vec<String>,
pub name: Option<String>,
pub description: Option<String>,
pub unknown_fields: Vec<String>,
}
#[derive(Debug, Clone, Default)]
pub struct ActiveScopes {
pub scopes: Vec<ActiveScope>,
pub tags: BTreeSet<String>,
}
pub fn evaluate(cfg: &Config, env: &Env) -> ActiveScopes {
let mut scopes = Vec::new();
for s in &cfg.scope.network {
if matcher::matches_network(s, env) {
scopes.push(ActiveScope {
id: s.id.clone(),
kind: "network",
tags: s.tags.clone(),
project_root: None,
enable_bundles: Vec::new(),
name: None,
description: None,
unknown_fields: Vec::new(),
});
}
}
for s in &cfg.scope.host {
if matcher::matches_host(s, env) {
scopes.push(ActiveScope {
id: s.id.clone(),
kind: "host",
tags: s.tags.clone(),
project_root: None,
enable_bundles: Vec::new(),
name: None,
description: None,
unknown_fields: Vec::new(),
});
}
}
for s in &cfg.scope.user {
if matcher::matches_user(s, env) {
scopes.push(ActiveScope {
id: s.id.clone(),
kind: "user",
tags: s.tags.clone(),
project_root: None,
enable_bundles: Vec::new(),
name: None,
description: None,
unknown_fields: Vec::new(),
});
}
}
if let Some(p) = matcher::discover_project(env) {
scopes.push(ActiveScope {
id: p.id,
kind: "project",
tags: p.tags,
project_root: Some(p.root),
enable_bundles: p.enable_bundles,
name: Some(p.name),
description: p.description,
unknown_fields: p.unknown_fields,
});
}
let tags: BTreeSet<String> = scopes.iter().flat_map(|s| s.tags.iter().cloned()).collect();
ActiveScopes { scopes, tags }
}