Skip to main content

radicle_cli/commands/
ls.rs

1mod args;
2
3pub use args::Args;
4
5use radicle::storage::{ReadStorage, RepositoryInfo};
6
7use crate::terminal as term;
8
9use term::Element;
10
11pub fn run(args: Args, ctx: impl term::Context) -> anyhow::Result<()> {
12    let profile = ctx.profile()?;
13    let storage = &profile.storage;
14    let repos = storage.repositories()?;
15    let policy = profile.policies()?;
16    let mut table = term::Table::new(term::TableOptions::bordered());
17    let mut rows = Vec::new();
18
19    if repos.is_empty() {
20        return Ok(());
21    }
22
23    for RepositoryInfo {
24        rid,
25        head,
26        doc,
27        refs,
28        ..
29    } in repos
30    {
31        if doc.is_public() && args.private {
32            continue;
33        }
34        if !doc.is_public() && args.public {
35            continue;
36        }
37        if refs.is_none() && !args.all && !args.seeded {
38            continue;
39        }
40        let seeded = policy.is_seeding(&rid)?;
41
42        if !seeded && !args.all {
43            continue;
44        }
45        if !seeded && args.seeded {
46            continue;
47        }
48        let proj = match doc.project() {
49            Ok(p) => p,
50            Err(e) => {
51                log::error!(target: "cli", "Error loading project payload for {rid}: {e}");
52                continue;
53            }
54        };
55        let head = term::format::oid(head).into();
56
57        rows.push([
58            term::format::bold(proj.name().to_owned()),
59            term::format::tertiary(rid.urn()),
60            if seeded {
61                term::format::visibility(doc.visibility()).into()
62            } else {
63                term::format::dim("local").into()
64            },
65            term::format::secondary(head),
66            term::format::italic(proj.description().to_owned()),
67        ]);
68    }
69    rows.sort();
70
71    if rows.is_empty() {
72        term::print(term::format::italic("Nothing to show."));
73    } else {
74        table.header([
75            "Name".into(),
76            "RID".into(),
77            "Visibility".into(),
78            "Head".into(),
79            "Description".into(),
80        ]);
81        table.divider();
82        table.extend(rows);
83        table.print();
84    }
85
86    Ok(())
87}