| 1 | |
|
| 2 | |
|
| 3 | | use super::identity::{owner_home, refresh_event}; |
| 4 | | use super::{ui, Ctx}; |
| 5 | | use anyhow::{bail, Context, Result}; |
| 6 | | use spectra_core::keys::KeyPair; |
| 7 | | use spectra_core::ledger::Ledger; |
| 8 | | use spectra_core::principals::signed_event; |
| 9 | | use spectra_core::projects; |
| 10 | | use std::path::{Path, PathBuf}; |
| 11 | |
|
| 12 | 20× | pub async fn add(ctx: &Ctx, path: &Path, name: Option<String>, remote: Option<String>, owner_home_flag: Option<PathBuf>) -> Result<i32> { |
| 13 | 20× | let path = path.canonicalize().with_context(|| format!("{} does not exist", path.display()))?; |
| 14 | 20× | let name = match name { |
| 15 | 1× | Some(n) => n, |
| 16 | 19× | None => projects::default_name(&path)?, |
| 17 | | }; |
| 18 | 20× | if !super::agent::valid_name(&name) { |
| 19 | 0× | bail!("a project's name is lower-case letters, digits and dashes (--name)"); |
| 20 | 20× | } |
| 21 | 20× | let checkout = projects::inspect(&path)?; |
| 22 | 20× | let home = owner_home(ctx, owner_home_flag); |
| 23 | 20× | let w = ctx.owner_writer(&home).await?; |
| 24 | 20× | let ledger = w.ledger(); |
| 25 | 20× | let exists: Option<String> = sqlx::query_scalar("SELECT name FROM projects WHERE name = ? OR path = ?") |
| 26 | 20× | .bind(&name) |
| 27 | 20× | .bind(path.display().to_string()) |
| 28 | 20× | .fetch_optional(ledger.pool()) |
| 29 | 20× | .await?; |
| 30 | 20× | if let Some(e) = exists { |
| 31 | 1× | bail!("{} is already a project ({e})", path.display()); |
| 32 | 19× | } |
| 33 | 19× | let mut events: Vec<_> = refresh_event(ledger, &home).await?.into_iter().collect(); |
| 34 | 19× | let owner = KeyPair::load_home(&home)?; |
| 35 | 19× | let remote = remote.or(checkout.remote.clone()); |
| 36 | 19× | let payload = serde_json::json!({ |
| 37 | 19× | "name": name, "path": path.display().to_string(), "repo_id": checkout.repo_id, "remote": remote, "modules": checkout.modules, |
| 38 | | }); |
| 39 | 19× | events.push(signed_event(&owner, "human", "project.added", payload).project(name.clone()).subject(name.clone())); |
| 40 | 19× | events.push(spectra_core::conversation::project_channel_event(&name)); |
| 41 | 19× | w.append(events).await?; |
| 42 | 19× | println!("{} {} {}", ui::done("Added"), ui::subject(&name), ui::path(path.display())); |
| 43 | 19× | if let Some(r) = &remote { |
| 44 | 5× | println!("{}", ui::kv("remote", r)); |
| 45 | 14× | } |
| 46 | 19× | if let Some(id) = &checkout.repo_id { |
| 47 | 5× | println!("{}", ui::kv("repo", ui::hash(id))); |
| 48 | 14× | } |
| 49 | 19× | let (deps, users) = relations(ledger, &name).await?; |
| 50 | 19× | if !deps.is_empty() { |
| 51 | 1× | println!("{}", ui::kv("pins", deps.join(", "))); |
| 52 | 18× | } |
| 53 | 19× | if !users.is_empty() { |
| 54 | 1× | println!("{}", ui::kv("pinned by", users.join(", "))); |
| 55 | 18× | } |
| 56 | 19× | if checkout.repo_id.is_none() { |
| 57 | 14× | println!(" {} {}", ui::note(), ui::dim("not an Oxygen checkout: relations come from .oxymodules, so it has none")); |
| 58 | 14× | } |
| 59 | 19× | Ok(0) |
| 60 | 20× | } |
| 61 | |
|
| 62 | 24× | async fn relations(ledger: &Ledger, name: &str) -> Result<(Vec<String>, Vec<String>)> { |
| 63 | 24× | let deps = sqlx::query_scalar("SELECT depends_on FROM project_relations WHERE project = ? ORDER BY 1") |
| 64 | 24× | .bind(name) |
| 65 | 24× | .fetch_all(ledger.pool()) |
| 66 | 24× | .await?; |
| 67 | 24× | let users = sqlx::query_scalar("SELECT project FROM project_relations WHERE depends_on = ? ORDER BY 1") |
| 68 | 24× | .bind(name) |
| 69 | 24× | .fetch_all(ledger.pool()) |
| 70 | 24× | .await?; |
| 71 | 24× | Ok((deps, users)) |
| 72 | 24× | } |
| 73 | |
|
| 74 | 3× | pub async fn list(ctx: &Ctx, json: bool) -> Result<i32> { |
| 75 | 3× | let ledger = Ledger::open_read_only(&ctx.db()?).await?; |
| 76 | 3× | let rows: Vec<(String, String, Option<String>)> = |
| 77 | 3× | sqlx::query_as("SELECT name, path, remote FROM projects ORDER BY name").fetch_all(ledger.pool()).await?; |
| 78 | 3× | let mut out = Vec::new(); |
| 79 | 5× | for (name, path, remote) in rows { |
| 80 | 5× | let (deps, users) = relations(&ledger, &name).await?; |
| 81 | 5× | let agents: Vec<String> = sqlx::query_scalar("SELECT name FROM agents WHERE project = ? AND disabled_ms IS NULL ORDER BY name") |
| 82 | 5× | .bind(&name) |
| 83 | 5× | .fetch_all(ledger.pool()) |
| 84 | 5× | .await?; |
| 85 | 5× | out.push(serde_json::json!({ "name": name, "path": path, "remote": remote, "depends_on": deps, "depended_on_by": users, "agents": agents })); |
| 86 | | } |
| 87 | 3× | if json { |
| 88 | 3× | println!("{}", serde_json::Value::Array(out)); |
| 89 | 3× | return Ok(0); |
| 90 | 0× | } |
| 91 | 0× | if out.is_empty() { |
| 92 | 0× | println!("{} {}", ui::note(), ui::dim("no projects yet: spectra project add PATH")); |
| 93 | 0× | } |
| 94 | 0× | for p in out { |
| 95 | 0× | let list = |
| 96 | 0× | |k: &str| p[k].as_array().map(|a| a.iter().filter_map(|v| v.as_str()).collect::<Vec<_>>().join(", ")).unwrap_or_default(); |
| 97 | 0× | println!( |
| 98 | | "{} {} {}", |
| 99 | 0× | ui::glyph("▸", ">"), |
| 100 | 0× | ui::bookmark(p["name"].as_str().unwrap_or("")), |
| 101 | 0× | ui::path(p["path"].as_str().unwrap_or("")) |
| 102 | | ); |
| 103 | 0× | for (k, label) in [("depends_on", "pins"), ("depended_on_by", "pinned by"), ("agents", "agents")] { |
| 104 | 0× | let v = list(k); |
| 105 | 0× | if !v.is_empty() { |
| 106 | 0× | println!("{}", ui::kv(label, v)); |
| 107 | 0× | } |
| 108 | | } |
| 109 | | } |
| 110 | 0× | Ok(0) |
| 111 | 3× | } |