use serde::Serialize;
use crate::error::{DaloError, DaloResult};
use crate::inventory;
use crate::source::SourceKind;
use crate::store::{self, ApprovalRecord, ApprovalsFile, StorePaths};
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct ApprovalReport {
pub scope: String,
pub value: String,
pub action: String,
pub dry_run: bool,
}
pub fn grant(
paths: &StorePaths,
scope: &str,
value: &str,
dry_run: bool,
) -> DaloResult<ApprovalReport> {
let value = canonical_value(paths, scope, value)?;
let mut approvals = store::read_approvals(paths)?;
let record = ApprovalRecord {
scope: scope.to_owned(),
value: value.clone(),
};
let exists = approvals.approvals.contains(&record);
if !exists {
approvals.approvals.push(record);
approvals.approvals.sort_by(|left, right| {
left.scope
.cmp(&right.scope)
.then(left.value.cmp(&right.value))
});
if !dry_run {
store::write_approvals(paths, &approvals)?;
}
}
Ok(ApprovalReport {
scope: scope.to_owned(),
value,
action: if exists { "unchanged" } else { "granted" }.to_owned(),
dry_run,
})
}
pub fn revoke(
paths: &StorePaths,
scope: &str,
value: &str,
dry_run: bool,
) -> DaloResult<ApprovalReport> {
let canonical = match canonical_value(paths, scope, value) {
Ok(canonical) => Some(canonical),
Err(error @ DaloError::InvalidArgument { .. }) => return Err(error),
Err(_) => None,
};
let mut approvals = store::read_approvals(paths)?;
let before = approvals.approvals.len();
approvals.approvals.retain(|record| {
let matches = record.scope == scope
&& (record.value == value || canonical.as_deref() == Some(record.value.as_str()));
!matches
});
let changed = approvals.approvals.len() != before;
if changed && !dry_run {
store::write_approvals(paths, &approvals)?;
}
Ok(ApprovalReport {
scope: scope.to_owned(),
value: canonical.unwrap_or_else(|| value.to_owned()),
action: if changed { "revoked" } else { "unchanged" }.to_owned(),
dry_run,
})
}
pub fn list(paths: &StorePaths) -> DaloResult<ApprovalsFile> {
store::read_approvals(paths)
}
fn canonical_value(paths: &StorePaths, scope: &str, value: &str) -> DaloResult<String> {
match scope {
"source" => {
source_exists(paths, value)?;
Ok(value.to_owned())
}
"skill" => canonical_skill(paths, value),
"author" | "org" => {
let (source, owner) = source_qualified_owner(scope, value)?;
source_exists(paths, source)?;
if owner.trim().is_empty() {
return invalid("approval owner must not be empty");
}
Ok(format!("{source}:{owner}"))
}
_ => invalid("approval scope must be one of skill, source, author, or org"),
}
}
pub fn canonical_skill(paths: &StorePaths, value: &str) -> DaloResult<String> {
let (source_id, selector) = value
.split_once(':')
.filter(|(source, skill)| !source.is_empty() && !skill.is_empty())
.ok_or_else(|| DaloError::InvalidArgument {
reason: "skill approval values must use `<source>:<slot>`, for example `catalog:review-helper`"
.to_owned(),
})?;
let config = store::read_config(paths)?;
let source = config
.sources
.iter()
.find(|source| source.id == source_id)
.ok_or_else(|| {
DaloError::unknown_source(
source_id,
config
.sources
.iter()
.map(|candidate| candidate.id.clone())
.collect(),
)
})?;
let inventory = inventory::scan_source(source_id, &source.path)?;
let skill = inventory
.skills
.iter()
.find(|skill| skill.slot_name == selector || skill.id.as_deref() == Some(selector))
.ok_or_else(|| {
let next_command = match source.kind {
SourceKind::Catalog => format!("dalo source inspect {source_id}"),
_ => "dalo status".to_owned(),
};
DaloError::skill_not_found(
value,
inventory
.skills
.iter()
.map(|candidate| candidate.source_ref.clone())
.collect(),
next_command,
)
})?;
Ok(skill.source_ref.clone())
}
fn source_exists(paths: &StorePaths, source_id: &str) -> DaloResult<()> {
if store::read_config(paths)?
.sources
.iter()
.any(|source| source.id == source_id)
{
Ok(())
} else {
Err(DaloError::unknown_source(
source_id,
store::read_config(paths)?
.sources
.into_iter()
.map(|source| source.id)
.collect(),
))
}
}
fn source_qualified_owner<'a>(scope: &str, value: &'a str) -> DaloResult<(&'a str, &'a str)> {
value
.split_once(':')
.filter(|(source, value)| !source.is_empty() && !value.is_empty())
.ok_or_else(|| DaloError::InvalidArgument {
reason: format!(
"{scope} approval values must use `<source>:<owner>`, for example `catalog:{}`",
if scope == "author" {
"maintainers"
} else {
"example-org"
}
),
})
}
fn invalid<T>(reason: &str) -> DaloResult<T> {
Err(DaloError::InvalidArgument {
reason: reason.to_owned(),
})
}
#[cfg(test)]
mod tests {
use super::*;
fn init_paths() -> (tempfile::TempDir, StorePaths) {
let temp = tempfile::tempdir().expect("tempdir should be created");
let root = temp.path().join("store");
store::init_store(root.clone(), false).expect("store should initialize");
(temp, StorePaths::new(root))
}
#[test]
fn revoke_should_remove_approval_when_source_no_longer_resolves() {
let (_temp, paths) = init_paths();
let mut approvals = store::read_approvals(&paths).expect("approvals should read");
approvals.approvals.push(ApprovalRecord {
scope: "source".to_owned(),
value: "ghost".to_owned(),
});
store::write_approvals(&paths, &approvals).expect("approvals should write");
let report = revoke(&paths, "source", "ghost", false)
.expect("revoke should tolerate a source that no longer exists");
assert_eq!(report.action, "revoked");
assert!(
store::read_approvals(&paths)
.expect("approvals should read")
.approvals
.is_empty()
);
}
#[test]
fn revoke_should_remove_skill_approval_by_stored_value_when_source_is_gone() {
let (_temp, paths) = init_paths();
let mut approvals = store::read_approvals(&paths).expect("approvals should read");
approvals.approvals.push(ApprovalRecord {
scope: "skill".to_owned(),
value: "catalog:review-helper".to_owned(),
});
store::write_approvals(&paths, &approvals).expect("approvals should write");
let report = revoke(&paths, "skill", "catalog:review-helper", false)
.expect("revoke should tolerate a skill source that no longer exists");
assert_eq!(report.action, "revoked");
assert!(
store::read_approvals(&paths)
.expect("approvals should read")
.approvals
.is_empty()
);
}
#[test]
fn revoke_should_still_reject_an_invalid_scope() {
let (_temp, paths) = init_paths();
assert!(matches!(
revoke(&paths, "bogus", "x", false),
Err(DaloError::InvalidArgument { .. })
));
}
}