Add -l option for dumping existing allow/block lists

This commit is contained in:
Maxime Augier 2022-11-11 10:00:41 +01:00
parent 1f87c0f913
commit 3b0e9fddc0
3 changed files with 29 additions and 0 deletions

View file

@ -11,6 +11,9 @@ pub(crate) struct Args {
#[arg(short, long, help = "Undo allowing or blocking domains")]
undo: bool,
#[arg(short, long, help = "List allowed and blocked domains")]
list: bool,
}
impl Args {
@ -29,4 +32,8 @@ impl Args {
pub(crate) fn undo(&self) -> bool {
self.undo
}
pub(crate) fn list(&self) -> bool {
self.list
}
}

View file

@ -142,6 +142,14 @@ impl Inner {
.map(|s| String::from_utf8_lossy(&s).to_string())
}
fn allows(&self) -> impl DoubleEndedIterator<Item = String> {
self.allowed_domains
.iter()
.values()
.filter_map(|res| res.ok())
.map(|s| String::from_utf8_lossy(&s).to_string())
}
fn connected(&self) -> impl DoubleEndedIterator<Item = IriString> {
self.connected_actor_ids
.iter()
@ -452,6 +460,10 @@ impl Db {
self.unblock(|inner| Ok(inner.blocks().collect())).await
}
pub(crate) async fn allows(&self) -> Result<Vec<String>, Error> {
self.unblock(|inner| Ok(inner.allows().collect())).await
}
pub(crate) async fn inboxes(&self) -> Result<Vec<IriString>, Error> {
self.unblock(|inner| Ok(inner.connected_actors().map(|actor| actor.inbox).collect()))
.await

View file

@ -101,6 +101,16 @@ async fn main() -> Result<(), anyhow::Error> {
let args = Args::new();
if args.list() {
for domain in db.blocks().await? {
println!("block {}", domain);
}
for domain in db.allows().await? {
println!("allow {}", domain);
}
return Ok(());
}
if !args.blocks().is_empty() || !args.allowed().is_empty() {
if args.undo() {
db.remove_blocks(args.blocks().to_vec()).await?;