| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | | pub mod admin; |
| 6 | | pub mod agent; |
| 7 | | pub mod format; |
| 8 | | pub mod identity; |
| 9 | | pub mod json; |
| 10 | | pub mod marks; |
| 11 | | pub mod project; |
| 12 | | pub mod ui; |
| 13 | |
|
| 14 | | use anyhow::{bail, Result}; |
| 15 | | use clap::{Parser, Subcommand}; |
| 16 | | use std::path::PathBuf; |
| 17 | |
|
| 18 | | #[derive(Parser)] |
| 19 | | #[command(name = "spectra", version = env!("SPECTRA_VERSION_LONG"), about = "A control plane for AI coding agents")] |
| 20 | | pub struct Cli { |
| 21 | | |
| 22 | | #[arg(long, global = true)] |
| 23 | | pub plain: bool, |
| 24 | | |
| 25 | | #[arg(long, global = true, env = "SPECTRA_DATA")] |
| 26 | | pub data: Option<PathBuf>, |
| 27 | | #[command(subcommand)] |
| 28 | | pub command: Command, |
| 29 | | } |
| 30 | |
|
| 31 | | #[derive(Subcommand)] |
| 32 | | pub enum Command { |
| 33 | | |
| 34 | | Version { |
| 35 | | |
| 36 | | #[arg(long)] |
| 37 | | json: bool, |
| 38 | | }, |
| 39 | | |
| 40 | | |
| 41 | | Init { |
| 42 | | |
| 43 | | #[arg(long)] |
| 44 | | owner_home: Option<PathBuf>, |
| 45 | | |
| 46 | | #[arg(long)] |
| 47 | | name: Option<String>, |
| 48 | | }, |
| 49 | | |
| 50 | | Identity { |
| 51 | | #[command(subcommand)] |
| 52 | | command: IdentityCmd, |
| 53 | | }, |
| 54 | | |
| 55 | | Project { |
| 56 | | #[command(subcommand)] |
| 57 | | command: ProjectCmd, |
| 58 | | }, |
| 59 | | |
| 60 | | Agent { |
| 61 | | #[command(subcommand)] |
| 62 | | command: AgentCmd, |
| 63 | | }, |
| 64 | | |
| 65 | | Admin { |
| 66 | | #[command(subcommand)] |
| 67 | | command: Admin, |
| 68 | | }, |
| 69 | | } |
| 70 | |
|
| 71 | | #[derive(Subcommand)] |
| 72 | | pub enum IdentityCmd { |
| 73 | | |
| 74 | | Show { |
| 75 | | #[arg(long)] |
| 76 | | json: bool, |
| 77 | | }, |
| 78 | | |
| 79 | | Refresh { |
| 80 | | #[arg(long)] |
| 81 | | owner_home: Option<PathBuf>, |
| 82 | | }, |
| 83 | | } |
| 84 | |
|
| 85 | | #[derive(Subcommand)] |
| 86 | | pub enum ProjectCmd { |
| 87 | | |
| 88 | | Add { |
| 89 | | path: PathBuf, |
| 90 | | |
| 91 | | #[arg(long)] |
| 92 | | name: Option<String>, |
| 93 | | |
| 94 | | #[arg(long)] |
| 95 | | remote: Option<String>, |
| 96 | | #[arg(long)] |
| 97 | | owner_home: Option<PathBuf>, |
| 98 | | }, |
| 99 | | |
| 100 | | List { |
| 101 | | #[arg(long)] |
| 102 | | json: bool, |
| 103 | | }, |
| 104 | | } |
| 105 | |
|
| 106 | | #[derive(Subcommand)] |
| 107 | | pub enum AgentCmd { |
| 108 | | |
| 109 | | Add { |
| 110 | | |
| 111 | | name: String, |
| 112 | | |
| 113 | | #[arg(long)] |
| 114 | | kind: String, |
| 115 | | |
| 116 | | #[arg(long)] |
| 117 | | project: String, |
| 118 | | |
| 119 | | #[arg(long)] |
| 120 | | home: PathBuf, |
| 121 | | |
| 122 | | #[arg(long)] |
| 123 | | uid: Option<u32>, |
| 124 | | |
| 125 | | #[arg(long, conflicts_with = "uid")] |
| 126 | | learn_uid: bool, |
| 127 | | |
| 128 | | #[arg(long, default_value_t = 600)] |
| 129 | | wait: u64, |
| 130 | | |
| 131 | | #[arg(long)] |
| 132 | | owner_home: Option<PathBuf>, |
| 133 | | }, |
| 134 | | |
| 135 | | List { |
| 136 | | #[arg(long)] |
| 137 | | json: bool, |
| 138 | | }, |
| 139 | | |
| 140 | | Disable { |
| 141 | | name: String, |
| 142 | | #[arg(long)] |
| 143 | | owner_home: Option<PathBuf>, |
| 144 | | }, |
| 145 | | } |
| 146 | |
|
| 147 | | #[derive(Subcommand)] |
| 148 | | pub enum Admin { |
| 149 | | |
| 150 | | Verify { |
| 151 | | |
| 152 | | #[arg(long)] |
| 153 | | json: bool, |
| 154 | | }, |
| 155 | | |
| 156 | | Check, |
| 157 | | |
| 158 | | Rebuild, |
| 159 | | } |
| 160 | |
|
| 161 | |
|
| 162 | | pub struct Ctx { |
| 163 | | pub data: PathBuf, |
| 164 | | } |
| 165 | |
|
| 166 | | impl Ctx { |
| 167 | | |
| 168 | 32× | pub fn db(&self) -> Result<PathBuf> { |
| 169 | 32× | let db = self.data.join("spectra.db"); |
| 170 | 32× | if !db.exists() { |
| 171 | 1× | bail!("no Spectra in {} (run `spectra init`, or pass --data)", self.data.display()); |
| 172 | 31× | } |
| 173 | 31× | Ok(db) |
| 174 | 32× | } |
| 175 | |
|
| 176 | | |
| 177 | | |
| 178 | 15× | pub async fn writer(&self) -> Result<spectra_core::ledger::Ledger> { |
| 179 | 15× | let db = self.db()?; |
| 180 | 15× | let instance = spectra_core::keys::KeyPair::instance(&self.data, "spectra")?; |
| 181 | 15× | spectra_core::ledger::Ledger::open(&db, Some(instance)).await |
| 182 | 15× | } |
| 183 | |
|
| 184 | | |
| 185 | 15× | pub fn config_owner_home(&self) -> Option<PathBuf> { |
| 186 | 15× | let text = std::fs::read_to_string(self.data.join("spectra.toml")).ok()?; |
| 187 | 9× | let t: toml::Table = text.parse().ok()?; |
| 188 | 9× | t.get("owner")?.get("home")?.as_str().map(PathBuf::from) |
| 189 | 15× | } |
| 190 | | } |
| 191 | |
|
| 192 | 0× | fn default_data() -> PathBuf { |
| 193 | 0× | if let Ok(x) = std::env::var("XDG_DATA_HOME") { |
| 194 | 0× | return PathBuf::from(x).join("spectra"); |
| 195 | 0× | } |
| 196 | 0× | let home = std::env::var("HOME").unwrap_or_else(|_| ".".into()); |
| 197 | 0× | PathBuf::from(home).join(".local").join("share").join("spectra") |
| 198 | 0× | } |
| 199 | |
|
| 200 | 38× | fn runtime() -> Result<tokio::runtime::Runtime> { |
| 201 | 38× | Ok(tokio::runtime::Builder::new_multi_thread().enable_all().build()?) |
| 202 | 38× | } |
| 203 | |
|
| 204 | 38× | pub fn run() -> Result<i32> { |
| 205 | 38× | let cli = Cli::parse(); |
| 206 | 38× | ui::init(cli.plain); |
| 207 | 38× | let ctx = Ctx { data: cli.data.unwrap_or_else(default_data) }; |
| 208 | 38× | match cli.command { |
| 209 | 0× | Command::Version { json } => version(json), |
| 210 | 7× | Command::Init { owner_home, name } => runtime()?.block_on(identity::init(&ctx, owner_home, name)), |
| 211 | 0× | Command::Identity { command } => runtime()?.block_on(async { |
| 212 | 0× | match command { |
| 213 | 0× | IdentityCmd::Show { json } => identity::show(&ctx, json).await, |
| 214 | 0× | IdentityCmd::Refresh { owner_home } => identity::refresh(&ctx, owner_home).await, |
| 215 | | } |
| 216 | 0× | }), |
| 217 | 9× | Command::Project { command } => runtime()?.block_on(async { |
| 218 | 9× | match command { |
| 219 | 7× | ProjectCmd::Add { path, name, remote, owner_home } => project::add(&ctx, &path, name, remote, owner_home).await, |
| 220 | 2× | ProjectCmd::List { json } => project::list(&ctx, json).await, |
| 221 | | } |
| 222 | 9× | }), |
| 223 | 8× | Command::Agent { command } => runtime()?.block_on(async { |
| 224 | 8× | match command { |
| 225 | 3× | AgentCmd::Add { name, kind, project, home, uid, learn_uid, wait, owner_home } => { |
| 226 | 3× | agent::add(&ctx, agent::Add { name, kind, project, home, uid, learn_uid, wait_secs: wait, owner_home }).await |
| 227 | | } |
| 228 | 3× | AgentCmd::List { json } => agent::list(&ctx, json).await, |
| 229 | 2× | AgentCmd::Disable { name, owner_home } => agent::disable(&ctx, &name, owner_home).await, |
| 230 | | } |
| 231 | 8× | }), |
| 232 | 14× | Command::Admin { command } => runtime()?.block_on(async { |
| 233 | 14× | match command { |
| 234 | 9× | Admin::Verify { json } => admin::verify(&ctx, json).await, |
| 235 | 4× | Admin::Check => admin::check(&ctx).await, |
| 236 | 1× | Admin::Rebuild => admin::rebuild(&ctx).await, |
| 237 | | } |
| 238 | 14× | }), |
| 239 | | } |
| 240 | 38× | } |
| 241 | |
|
| 242 | 0× | fn version(as_json: bool) -> Result<i32> { |
| 243 | 0× | if as_json { |
| 244 | 0× | println!( |
| 245 | 0× | "{}", |
| 246 | 0× | serde_json::json!({ |
| 247 | 0× | "version": env!("CARGO_PKG_VERSION"), |
| 248 | 0× | "build": env!("SPECTRA_BUILD_ID"), |
| 249 | 0× | "mcp_protocol_versions": spectra_mcp::PROTOCOL_VERSIONS, |
| 250 | 0× | "ledger_format": spectra_core::LEDGER_FORMAT, |
| 251 | 0× | }) |
| 252 | 0× | ); |
| 253 | 0× | } else { |
| 254 | 0× | println!("spectra {}", env!("SPECTRA_VERSION_LONG")); |
| 255 | 0× | } |
| 256 | 0× | Ok(0) |
| 257 | 0× | } |