Linux CLI in the client binary: yumush login|logout|chats|read|send|tail|ci|build|import [--publish server|client|both] shares the GUI's token and network code
Browse files at this commitfc2d7c925867e6fd348ff4e26de2b2462ddcdf6bParent: 12115c5- feat
- Linux CLI in the client binary: yumush login|logout|chats|read|send|tail|ci|build|import [--publish server|client|both] shares the GUI's token and network code
- Side moves to common::update, job label to ci::Job
9 changed files +592 -25
- Cargo.lock +131 -0
- crates/client/Cargo.toml +2 -0
- crates/client/src/ci.rs +9 -0
- crates/client/src/cli.rs +411 -0
- crates/client/src/gui/builder/ci_view.rs +1 -7
- crates/client/src/gui/builder/releases.rs +1 -18
- crates/client/src/lib.rs +2 -0
- crates/client/src/main.rs +5 -0
- crates/common/src/update.rs +30 -0
Cargo.lockmodified+131 -0
| @@ -225,12 +225,56 @@ dependencies = [ | ||
| 225 | 225 | "unicode-width 0.2.3", |
| 226 | 226 | ] |
| 227 | 227 | |
| 228 | +[[package]] | |
| 229 | +name = "anstream" | |
| 230 | +version = "1.0.0" | |
| 231 | +source = "registry+https://github.com/rust-lang/crates.io-index" | |
| 232 | +checksum = "824a212faf96e9acacdbd09febd34438f8f711fb84e09a8916013cd7815ca28d" | |
| 233 | +dependencies = [ | |
| 234 | + "anstyle", | |
| 235 | + "anstyle-parse", | |
| 236 | + "anstyle-query", | |
| 237 | + "anstyle-wincon", | |
| 238 | + "colorchoice", | |
| 239 | + "is_terminal_polyfill", | |
| 240 | + "utf8parse", | |
| 241 | +] | |
| 242 | + | |
| 228 | 243 | [[package]] |
| 229 | 244 | name = "anstyle" |
| 230 | 245 | version = "1.0.14" |
| 231 | 246 | source = "registry+https://github.com/rust-lang/crates.io-index" |
| 232 | 247 | checksum = "940b3a0ca603d1eade50a4846a2afffd5ef57a9feac2c0e2ec2e14f9ead76000" |
| 233 | 248 | |
| 249 | +[[package]] | |
| 250 | +name = "anstyle-parse" | |
| 251 | +version = "1.0.0" | |
| 252 | +source = "registry+https://github.com/rust-lang/crates.io-index" | |
| 253 | +checksum = "52ce7f38b242319f7cabaa6813055467063ecdc9d355bbb4ce0c68908cd8130e" | |
| 254 | +dependencies = [ | |
| 255 | + "utf8parse", | |
| 256 | +] | |
| 257 | + | |
| 258 | +[[package]] | |
| 259 | +name = "anstyle-query" | |
| 260 | +version = "1.1.5" | |
| 261 | +source = "registry+https://github.com/rust-lang/crates.io-index" | |
| 262 | +checksum = "40c48f72fd53cd289104fc64099abca73db4166ad86ea0b4341abe65af83dadc" | |
| 263 | +dependencies = [ | |
| 264 | + "windows-sys 0.61.2", | |
| 265 | +] | |
| 266 | + | |
| 267 | +[[package]] | |
| 268 | +name = "anstyle-wincon" | |
| 269 | +version = "3.0.11" | |
| 270 | +source = "registry+https://github.com/rust-lang/crates.io-index" | |
| 271 | +checksum = "291e6a250ff86cd4a820112fb8898808a366d8f9f58ce16d1f538353ad55747d" | |
| 272 | +dependencies = [ | |
| 273 | + "anstyle", | |
| 274 | + "once_cell_polyfill", | |
| 275 | + "windows-sys 0.61.2", | |
| 276 | +] | |
| 277 | + | |
| 234 | 278 | [[package]] |
| 235 | 279 | name = "antithesis_sdk" |
| 236 | 280 | version = "0.3.0" |
| @@ -1261,12 +1305,53 @@ dependencies = [ | ||
| 1261 | 1305 | "libloading", |
| 1262 | 1306 | ] |
| 1263 | 1307 | |
| 1308 | +[[package]] | |
| 1309 | +name = "clap" | |
| 1310 | +version = "4.6.10" | |
| 1311 | +source = "registry+https://github.com/rust-lang/crates.io-index" | |
| 1312 | +checksum = "50066ca966813ca8957ee15361752f19424a8ff620dbe9c8993d9f99d99b04b3" | |
| 1313 | +dependencies = [ | |
| 1314 | + "clap_builder", | |
| 1315 | + "clap_derive", | |
| 1316 | +] | |
| 1317 | + | |
| 1318 | +[[package]] | |
| 1319 | +name = "clap_builder" | |
| 1320 | +version = "4.6.10" | |
| 1321 | +source = "registry+https://github.com/rust-lang/crates.io-index" | |
| 1322 | +checksum = "a0ed97c96ef4efb3cc84e09fa388df4463e7cc3224ec38d7b1d9e45749333590" | |
| 1323 | +dependencies = [ | |
| 1324 | + "anstream", | |
| 1325 | + "anstyle", | |
| 1326 | + "clap_lex", | |
| 1327 | + "strsim", | |
| 1328 | +] | |
| 1329 | + | |
| 1330 | +[[package]] | |
| 1331 | +name = "clap_derive" | |
| 1332 | +version = "4.6.9" | |
| 1333 | +source = "registry+https://github.com/rust-lang/crates.io-index" | |
| 1334 | +checksum = "b4c174f11f949b9ca8c60580925e282b9cbd5454750e506f08648b2038080cd7" | |
| 1335 | +dependencies = [ | |
| 1336 | + "heck 0.5.0", | |
| 1337 | + "proc-macro2", | |
| 1338 | + "quote", | |
| 1339 | + "syn 3.0.10", | |
| 1340 | +] | |
| 1341 | + | |
| 1342 | +[[package]] | |
| 1343 | +name = "clap_lex" | |
| 1344 | +version = "1.1.2" | |
| 1345 | +source = "registry+https://github.com/rust-lang/crates.io-index" | |
| 1346 | +checksum = "e0921b3876b3a1df8a1c9f912e7f94342458022c2e84288015893d83c4d62a79" | |
| 1347 | + | |
| 1264 | 1348 | [[package]] |
| 1265 | 1349 | name = "client" |
| 1266 | 1350 | version = "0.1.0" |
| 1267 | 1351 | dependencies = [ |
| 1268 | 1352 | "ashpd 0.14.3", |
| 1269 | 1353 | "bytes", |
| 1354 | + "clap", | |
| 1270 | 1355 | "common", |
| 1271 | 1356 | "coreaudio-rs", |
| 1272 | 1357 | "dirs 7.0.0", |
| @@ -1297,6 +1382,7 @@ dependencies = [ | ||
| 1297 | 1382 | "quinn", |
| 1298 | 1383 | "reed-solomon-simd", |
| 1299 | 1384 | "resvg 0.50.0", |
| 1385 | + "rpassword", | |
| 1300 | 1386 | "rtrb", |
| 1301 | 1387 | "rustls-platform-verifier", |
| 1302 | 1388 | "ruzstd", |
| @@ -1411,6 +1497,12 @@ version = "1.1.0" | ||
| 1411 | 1497 | source = "registry+https://github.com/rust-lang/crates.io-index" |
| 1412 | 1498 | checksum = "3d7b894f5411737b7867f4827955924d7c254fc9f4d91a6aad6b097804b1018b" |
| 1413 | 1499 | |
| 1500 | +[[package]] | |
| 1501 | +name = "colorchoice" | |
| 1502 | +version = "1.0.5" | |
| 1503 | +source = "registry+https://github.com/rust-lang/crates.io-index" | |
| 1504 | +checksum = "1d07550c9036bf2ae0c684c4297d503f838287c83c53686d05370d0e139ae570" | |
| 1505 | + | |
| 1414 | 1506 | [[package]] |
| 1415 | 1507 | name = "combine" |
| 1416 | 1508 | version = "4.6.9" |
| @@ -5013,6 +5105,12 @@ dependencies = [ | ||
| 5013 | 5105 | "once_cell", |
| 5014 | 5106 | ] |
| 5015 | 5107 | |
| 5108 | +[[package]] | |
| 5109 | +name = "is_terminal_polyfill" | |
| 5110 | +version = "1.70.2" | |
| 5111 | +source = "registry+https://github.com/rust-lang/crates.io-index" | |
| 5112 | +checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695" | |
| 5113 | + | |
| 5016 | 5114 | [[package]] |
| 5017 | 5115 | name = "itertools" |
| 5018 | 5116 | version = "0.11.0" |
| @@ -6650,6 +6748,12 @@ version = "1.21.4" | ||
| 6650 | 6748 | source = "registry+https://github.com/rust-lang/crates.io-index" |
| 6651 | 6749 | checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50" |
| 6652 | 6750 | |
| 6751 | +[[package]] | |
| 6752 | +name = "once_cell_polyfill" | |
| 6753 | +version = "1.70.2" | |
| 6754 | +source = "registry+https://github.com/rust-lang/crates.io-index" | |
| 6755 | +checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe" | |
| 6756 | + | |
| 6653 | 6757 | [[package]] |
| 6654 | 6758 | name = "oo7" |
| 6655 | 6759 | version = "0.6.0" |
| @@ -7792,6 +7896,27 @@ dependencies = [ | ||
| 7792 | 7896 | "memchr", |
| 7793 | 7897 | ] |
| 7794 | 7898 | |
| 7899 | +[[package]] | |
| 7900 | +name = "rpassword" | |
| 7901 | +version = "7.5.5" | |
| 7902 | +source = "registry+https://github.com/rust-lang/crates.io-index" | |
| 7903 | +checksum = "1fa2c5d9304b618df61ff17f3d3485c6e1854a3d94902a49abbb8d43dfbe0464" | |
| 7904 | +dependencies = [ | |
| 7905 | + "libc", | |
| 7906 | + "rtoolbox", | |
| 7907 | + "windows-sys 0.61.2", | |
| 7908 | +] | |
| 7909 | + | |
| 7910 | +[[package]] | |
| 7911 | +name = "rtoolbox" | |
| 7912 | +version = "0.0.6" | |
| 7913 | +source = "registry+https://github.com/rust-lang/crates.io-index" | |
| 7914 | +checksum = "9a1efe12a1469752d0e6ff5ebec0b6ef4924cc5c4c71046b0ec730040535819d" | |
| 7915 | +dependencies = [ | |
| 7916 | + "libc", | |
| 7917 | + "windows-sys 0.61.2", | |
| 7918 | +] | |
| 7919 | + | |
| 7795 | 7920 | [[package]] |
| 7796 | 7921 | name = "rtrb" |
| 7797 | 7922 | version = "0.4.1" |
| @@ -10006,6 +10131,12 @@ dependencies = [ | ||
| 10006 | 10131 | "utf_types", |
| 10007 | 10132 | ] |
| 10008 | 10133 | |
| 10134 | +[[package]] | |
| 10135 | +name = "utf8parse" | |
| 10136 | +version = "0.2.2" | |
| 10137 | +source = "registry+https://github.com/rust-lang/crates.io-index" | |
| 10138 | +checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" | |
| 10139 | + | |
| 10009 | 10140 | [[package]] |
| 10010 | 10141 | name = "utf_types" |
| 10011 | 10142 | version = "1.0.0" |
crates/client/Cargo.tomlmodified+2 -0
| @@ -51,6 +51,8 @@ nokhwa = { version = "0.10.11", default-features = false, features = ["input-nat | ||
| 51 | 51 | v4l = "0.14.0" |
| 52 | 52 | pipewire = { version = "0.10.1", features = ["v0_3_49"] } |
| 53 | 53 | ksni = "0.3.7" |
| 54 | +clap = { version = "4.6.10", features = ["derive"] } | |
| 55 | +rpassword = "7.5.5" | |
| 54 | 56 | ashpd = { version = "0.14.3", default-features = false, features = ["tokio", "screencast"] } |
| 55 | 57 | |
| 56 | 58 | [target.'cfg(target_os = "windows")'.dependencies] |
crates/client/src/ci.rsmodified+9 -0
| @@ -65,6 +65,15 @@ impl State { | ||
| 65 | 65 | } |
| 66 | 66 | } |
| 67 | 67 | |
| 68 | +impl Job { | |
| 69 | + pub fn label(&self) -> &str { | |
| 70 | + self.name | |
| 71 | + .strip_prefix("build (") | |
| 72 | + .and_then(|name| name.strip_suffix(')')) | |
| 73 | + .unwrap_or(&self.name) | |
| 74 | + } | |
| 75 | +} | |
| 76 | + | |
| 68 | 77 | impl Run { |
| 69 | 78 | pub fn tag(&self) -> Option<&str> { |
| 70 | 79 | self.reference.strip_prefix("refs/tags/") |
crates/client/src/cli.rsadded+411 -0
| @@ -0,0 +1,411 @@ | ||
| 1 | +use std::{collections::HashMap, io::Read, process::exit, time::Duration}; | |
| 2 | + | |
| 3 | +use clap::{Parser, Subcommand}; | |
| 4 | +use common::{ | |
| 5 | + call::{ | |
| 6 | + CreateMessage, CreateTag, GetForge, ImportRelease, ListCommunities, ListMembers, | |
| 7 | + ListMessages, OpenNotes, PublishRelease, | |
| 8 | + }, | |
| 9 | + event::Event, | |
| 10 | + model::{Community, CommunityKind, Id, Message}, | |
| 11 | + update::Side, | |
| 12 | +}; | |
| 13 | +use tokio::sync::mpsc::Receiver; | |
| 14 | + | |
| 15 | +use crate::{ | |
| 16 | + Server, accounts, ci, | |
| 17 | + net::{self, Fail, NetworkEvent, NetworkHandle}, | |
| 18 | +}; | |
| 19 | + | |
| 20 | +const CONNECT: Duration = Duration::from_secs(15); | |
| 21 | + | |
| 22 | +#[derive(Parser)] | |
| 23 | +#[command(name = "yumush", bin_name = "yumush")] | |
| 24 | +struct Cli { | |
| 25 | + #[command(subcommand)] | |
| 26 | + command: Command, | |
| 27 | +} | |
| 28 | + | |
| 29 | +#[derive(Subcommand)] | |
| 30 | +enum Command { | |
| 31 | + Login { | |
| 32 | + username: String, | |
| 33 | + }, | |
| 34 | + Logout, | |
| 35 | + Chats, | |
| 36 | + Read { | |
| 37 | + chat: String, | |
| 38 | + #[arg(short = 'n', default_value_t = 20)] | |
| 39 | + count: usize, | |
| 40 | + }, | |
| 41 | + Send { | |
| 42 | + chat: String, | |
| 43 | + words: Vec<String>, | |
| 44 | + }, | |
| 45 | + Tail { | |
| 46 | + chat: Option<String>, | |
| 47 | + }, | |
| 48 | + Ci { | |
| 49 | + tag: Option<String>, | |
| 50 | + }, | |
| 51 | + Build { | |
| 52 | + tag: String, | |
| 53 | + }, | |
| 54 | + Import { | |
| 55 | + tag: String, | |
| 56 | + #[arg(long)] | |
| 57 | + publish: Option<Side>, | |
| 58 | + #[arg(long)] | |
| 59 | + required: bool, | |
| 60 | + }, | |
| 61 | +} | |
| 62 | + | |
| 63 | +pub fn wanted() -> bool { | |
| 64 | + std::env::args() | |
| 65 | + .nth(1) | |
| 66 | + .is_some_and(|arg| !arg.starts_with('-') || arg == "--help" || arg == "-h") | |
| 67 | +} | |
| 68 | + | |
| 69 | +pub fn run() { | |
| 70 | + let command = Cli::parse().command; | |
| 71 | + let runtime = tokio::runtime::Builder::new_current_thread() | |
| 72 | + .enable_all() | |
| 73 | + .build() | |
| 74 | + .expect("cli runtime"); | |
| 75 | + if let Err(error) = runtime.block_on(execute(command)) { | |
| 76 | + eprintln!("{error}"); | |
| 77 | + exit(1); | |
| 78 | + } | |
| 79 | +} | |
| 80 | + | |
| 81 | +struct Session { | |
| 82 | + network: NetworkHandle, | |
| 83 | + events: Receiver<NetworkEvent>, | |
| 84 | +} | |
| 85 | + | |
| 86 | +async fn execute(command: Command) -> Result<(), String> { | |
| 87 | + match command { | |
| 88 | + Command::Login { username } => login(&username).await, | |
| 89 | + Command::Logout => logout().await, | |
| 90 | + Command::Chats => { | |
| 91 | + let session = open().await?; | |
| 92 | + for community in session.communities().await? { | |
| 93 | + println!("{}", title(&community)); | |
| 94 | + } | |
| 95 | + Ok(()) | |
| 96 | + } | |
| 97 | + Command::Read { chat, count } => { | |
| 98 | + let session = open().await?; | |
| 99 | + let community = session.chat(&chat).await?; | |
| 100 | + let names = session.names(community.id).await?; | |
| 101 | + let messages = session | |
| 102 | + .network | |
| 103 | + .call(ListMessages { | |
| 104 | + community_id: community.id, | |
| 105 | + before: None, | |
| 106 | + }) | |
| 107 | + .await | |
| 108 | + .map_err(text)?; | |
| 109 | + let skip = messages.len().saturating_sub(count); | |
| 110 | + for message in &messages[skip..] { | |
| 111 | + print_message(message, &names, None); | |
| 112 | + } | |
| 113 | + Ok(()) | |
| 114 | + } | |
| 115 | + Command::Send { chat, words } => { | |
| 116 | + let body = if words.is_empty() { | |
| 117 | + let mut body = String::new(); | |
| 118 | + std::io::stdin() | |
| 119 | + .read_to_string(&mut body) | |
| 120 | + .map_err(|error| error.to_string())?; | |
| 121 | + body.trim_end().to_string() | |
| 122 | + } else { | |
| 123 | + words.join(" ") | |
| 124 | + }; | |
| 125 | + let session = open().await?; | |
| 126 | + let community = session.chat(&chat).await?; | |
| 127 | + session | |
| 128 | + .network | |
| 129 | + .call(CreateMessage { | |
| 130 | + community_id: community.id, | |
| 131 | + body, | |
| 132 | + attachments: Vec::new(), | |
| 133 | + reply_to: None, | |
| 134 | + }) | |
| 135 | + .await | |
| 136 | + .map_err(text)?; | |
| 137 | + Ok(()) | |
| 138 | + } | |
| 139 | + Command::Tail { chat } => tail(open().await?, chat).await, | |
| 140 | + Command::Ci { tag } => follow(&open().await?, tag.as_deref()).await, | |
| 141 | + Command::Build { tag } => { | |
| 142 | + let session = open().await?; | |
| 143 | + session | |
| 144 | + .network | |
| 145 | + .call(CreateTag { tag: tag.clone() }) | |
| 146 | + .await | |
| 147 | + .map_err(text)?; | |
| 148 | + println!("tag {tag} created"); | |
| 149 | + follow(&session, Some(&tag)).await | |
| 150 | + } | |
| 151 | + Command::Import { | |
| 152 | + tag, | |
| 153 | + publish, | |
| 154 | + required, | |
| 155 | + } => import(open().await?, tag, publish, required).await, | |
| 156 | + } | |
| 157 | +} | |
| 158 | + | |
| 159 | +fn text(fail: Fail) -> String { | |
| 160 | + fail.to_string() | |
| 161 | +} | |
| 162 | + | |
| 163 | +fn start() -> (NetworkHandle, Receiver<NetworkEvent>, accounts::Accounts) { | |
| 164 | + let server = Server::load(); | |
| 165 | + let accounts = accounts::load(&server.address); | |
| 166 | + let (network, events) = net::start(server); | |
| 167 | + (network, events, accounts) | |
| 168 | +} | |
| 169 | + | |
| 170 | +async fn login(username: &str) -> Result<(), String> { | |
| 171 | + let password = rpassword::prompt_password("password: ").map_err(|error| error.to_string())?; | |
| 172 | + let (network, events, mut accounts) = start(); | |
| 173 | + let session = Session { network, events }; | |
| 174 | + let (user, token) = session | |
| 175 | + .connect(session.network.login(username, &password)) | |
| 176 | + .await?; | |
| 177 | + accounts.sign_in(&user, token); | |
| 178 | + println!("signed in as {}", user.username); | |
| 179 | + Ok(()) | |
| 180 | +} | |
| 181 | + | |
| 182 | +async fn logout() -> Result<(), String> { | |
| 183 | + let session = open().await?; | |
| 184 | + session.network.logout().await.map_err(text)?; | |
| 185 | + let mut accounts = accounts::load(&session.network.server().address); | |
| 186 | + if let Some(user_id) = accounts.active { | |
| 187 | + accounts.forget(user_id); | |
| 188 | + } | |
| 189 | + Ok(()) | |
| 190 | +} | |
| 191 | + | |
| 192 | +async fn open() -> Result<Session, String> { | |
| 193 | + let (network, events, accounts) = start(); | |
| 194 | + let token = accounts | |
| 195 | + .active | |
| 196 | + .and_then(|user_id| accounts.get(user_id)) | |
| 197 | + .map(|account| account.token.clone()) | |
| 198 | + .ok_or("not signed in: yumush login <username>")?; | |
| 199 | + let session = Session { network, events }; | |
| 200 | + session.connect(session.network.resume(&token)).await?; | |
| 201 | + Ok(session) | |
| 202 | +} | |
| 203 | + | |
| 204 | +impl Session { | |
| 205 | + async fn connect<T>( | |
| 206 | + &self, | |
| 207 | + sign_in: impl Future<Output = Result<T, Fail>>, | |
| 208 | + ) -> Result<T, String> { | |
| 209 | + tokio::time::timeout(CONNECT, sign_in) | |
| 210 | + .await | |
| 211 | + .map_err(|_| "server unreachable".to_string())? | |
| 212 | + .map_err(text) | |
| 213 | + } | |
| 214 | + | |
| 215 | + async fn communities(&self) -> Result<Vec<Community>, String> { | |
| 216 | + let mut communities = self.network.call(ListCommunities).await.map_err(text)?; | |
| 217 | + if !communities | |
| 218 | + .iter() | |
| 219 | + .any(|community| matches!(community.kind, CommunityKind::Notes)) | |
| 220 | + { | |
| 221 | + communities.push(self.network.call(OpenNotes).await.map_err(text)?); | |
| 222 | + } | |
| 223 | + Ok(communities) | |
| 224 | + } | |
| 225 | + | |
| 226 | + async fn chat(&self, chat: &str) -> Result<Community, String> { | |
| 227 | + self.communities() | |
| 228 | + .await? | |
| 229 | + .into_iter() | |
| 230 | + .find(|community| title(community).eq_ignore_ascii_case(chat)) | |
| 231 | + .ok_or_else(|| format!("no chat named {chat}; see yumush chats")) | |
| 232 | + } | |
| 233 | + | |
| 234 | + async fn names(&self, community_id: Id) -> Result<HashMap<Id, String>, String> { | |
| 235 | + Ok(self | |
| 236 | + .network | |
| 237 | + .call(ListMembers { community_id }) | |
| 238 | + .await | |
| 239 | + .map_err(text)? | |
| 240 | + .into_iter() | |
| 241 | + .map(|member| (member.user.id, member.user.username)) | |
| 242 | + .collect()) | |
| 243 | + } | |
| 244 | + | |
| 245 | + async fn event(&mut self) -> Result<Event, String> { | |
| 246 | + loop { | |
| 247 | + match self.events.recv().await { | |
| 248 | + Some(NetworkEvent::Pushed(event)) => return Ok(*event), | |
| 249 | + Some(NetworkEvent::SessionLost) => return Err("session lost".to_string()), | |
| 250 | + Some(_) => {} | |
| 251 | + None => return Err("disconnected".to_string()), | |
| 252 | + } | |
| 253 | + } | |
| 254 | + } | |
| 255 | +} | |
| 256 | + | |
| 257 | +fn title(community: &Community) -> String { | |
| 258 | + match &community.kind { | |
| 259 | + CommunityKind::Group => community.name.clone(), | |
| 260 | + CommunityKind::Direct { peer } => peer.username.clone(), | |
| 261 | + CommunityKind::Notes => "notes".to_string(), | |
| 262 | + } | |
| 263 | +} | |
| 264 | + | |
| 265 | +fn print_message(message: &Message, names: &HashMap<Id, String>, chat: Option<&str>) { | |
| 266 | + if message.deleted { | |
| 267 | + return; | |
| 268 | + } | |
| 269 | + let time = jiff::Timestamp::from_millisecond(message.created_at as i64) | |
| 270 | + .map(|time| { | |
| 271 | + time.to_zoned_datetime(jiff::tz::TimeZone::system()) | |
| 272 | + .strftime("%m-%d %H:%M") | |
| 273 | + .to_string() | |
| 274 | + }) | |
| 275 | + .unwrap_or_default(); | |
| 276 | + let name = names.get(&message.user_id).map_or("?", String::as_str); | |
| 277 | + let files: String = message | |
| 278 | + .attachments | |
| 279 | + .iter() | |
| 280 | + .map(|attachment| format!(" [{}]", attachment.name)) | |
| 281 | + .collect(); | |
| 282 | + let chat = chat.map(|chat| format!("#{chat} ")).unwrap_or_default(); | |
| 283 | + let call = message | |
| 284 | + .call | |
| 285 | + .map(|call| format!("[call {}]", call.as_str())) | |
| 286 | + .unwrap_or_default(); | |
| 287 | + println!("{time} {chat}{name}: {call}{}{files}", message.body); | |
| 288 | +} | |
| 289 | + | |
| 290 | +async fn tail(mut session: Session, chat: Option<String>) -> Result<(), String> { | |
| 291 | + let communities = session.communities().await?; | |
| 292 | + let only = match &chat { | |
| 293 | + Some(chat) => Some(session.chat(chat).await?.id), | |
| 294 | + None => None, | |
| 295 | + }; | |
| 296 | + let titles: HashMap<Id, String> = communities | |
| 297 | + .iter() | |
| 298 | + .map(|community| (community.id, title(community))) | |
| 299 | + .collect(); | |
| 300 | + let mut names: HashMap<Id, HashMap<Id, String>> = HashMap::new(); | |
| 301 | + loop { | |
| 302 | + let Event::MessageCreated(message) = session.event().await? else { | |
| 303 | + continue; | |
| 304 | + }; | |
| 305 | + if only.is_some_and(|id| id != message.community_id) { | |
| 306 | + continue; | |
| 307 | + } | |
| 308 | + if let std::collections::hash_map::Entry::Vacant(entry) = names.entry(message.community_id) | |
| 309 | + { | |
| 310 | + entry.insert(session.names(message.community_id).await?); | |
| 311 | + } | |
| 312 | + let chat = only | |
| 313 | + .is_none() | |
| 314 | + .then(|| titles.get(&message.community_id).map(String::as_str)) | |
| 315 | + .flatten(); | |
| 316 | + print_message(&message, &names[&message.community_id], chat); | |
| 317 | + } | |
| 318 | +} | |
| 319 | + | |
| 320 | +async fn follow(session: &Session, tag: Option<&str>) -> Result<(), String> { | |
| 321 | + let repo = session | |
| 322 | + .network | |
| 323 | + .call(GetForge) | |
| 324 | + .await | |
| 325 | + .map_err(text)? | |
| 326 | + .ok_or("the server has no forge set")?; | |
| 327 | + let (_watch, mut events) = ci::watch(repo); | |
| 328 | + let mut states: HashMap<String, ci::State> = HashMap::new(); | |
| 329 | + let mut labels: HashMap<String, String> = HashMap::new(); | |
| 330 | + let mut number = None; | |
| 331 | + while let Some(event) = events.recv().await { | |
| 332 | + match event { | |
| 333 | + ci::Event::Run(run) => { | |
| 334 | + if tag.is_some_and(|tag| run.tag() != Some(tag)) { | |
| 335 | + continue; | |
| 336 | + } | |
| 337 | + if number != Some(run.number) { | |
| 338 | + number = Some(run.number); | |
| 339 | + println!( | |
| 340 | + "run #{} {}", | |
| 341 | + run.number, | |
| 342 | + run.tag().unwrap_or(&run.reference) | |
| 343 | + ); | |
| 344 | + } | |
| 345 | + for job in &run.jobs { | |
| 346 | + labels.insert(job.id.clone(), job.label().to_string()); | |
| 347 | + let state = ci::State::of(&job.state); | |
| 348 | + if states.insert(job.id.clone(), state) != Some(state) { | |
| 349 | + println!("{} {}", job.label(), job.state); | |
| 350 | + } | |
| 351 | + } | |
| 352 | + if ci::State::of(&run.state).finished() { | |
| 353 | + println!("run #{} {}", run.number, run.state); | |
| 354 | + return match ci::State::of(&run.state) { | |
| 355 | + ci::State::Success => Ok(()), | |
| 356 | + _ => Err(format!("run #{} {}", run.number, run.state)), | |
| 357 | + }; | |
| 358 | + } | |
| 359 | + } | |
| 360 | + ci::Event::Line { job, line } => { | |
| 361 | + if let Some(label) = labels.get(&job) { | |
| 362 | + println!("{label} | {line}"); | |
| 363 | + } | |
| 364 | + } | |
| 365 | + } | |
| 366 | + } | |
| 367 | + Err("forge unreachable".to_string()) | |
| 368 | +} | |
| 369 | + | |
| 370 | +async fn import( | |
| 371 | + mut session: Session, | |
| 372 | + tag: String, | |
| 373 | + publish: Option<Side>, | |
| 374 | + required: bool, | |
| 375 | +) -> Result<(), String> { | |
| 376 | + session | |
| 377 | + .network | |
| 378 | + .call(ImportRelease { tag: tag.clone() }) | |
| 379 | + .await | |
| 380 | + .map_err(text)?; | |
| 381 | + println!("importing {tag}"); | |
| 382 | + let staged = loop { | |
| 383 | + if let Event::Imported { tag: done, result } = session.event().await? | |
| 384 | + && done == tag | |
| 385 | + { | |
| 386 | + break result.map_err(|problem| problem.to_string())?; | |
| 387 | + } | |
| 388 | + }; | |
| 389 | + if staged.is_empty() { | |
| 390 | + println!("nothing new"); | |
| 391 | + return Ok(()); | |
| 392 | + } | |
| 393 | + for file in &staged { | |
| 394 | + println!("staged {} {} bytes", file.target, file.size); | |
| 395 | + } | |
| 396 | + let Some(side) = publish else { | |
| 397 | + return Ok(()); | |
| 398 | + }; | |
| 399 | + let files = staged | |
| 400 | + .iter() | |
| 401 | + .filter(|file| side.takes(&file.target)) | |
| 402 | + .map(|file| file.file) | |
| 403 | + .collect(); | |
| 404 | + let release = session | |
| 405 | + .network | |
| 406 | + .call(PublishRelease { files, required }) | |
| 407 | + .await | |
| 408 | + .map_err(text)?; | |
| 409 | + println!("published release #{}", release.seq); | |
| 410 | + Ok(()) | |
| 411 | +} |
crates/client/src/gui/builder/ci_view.rsmodified+1 -7
| @@ -74,12 +74,6 @@ impl Live { | ||
| 74 | 74 | } |
| 75 | 75 | } |
| 76 | 76 | |
| 77 | -fn job_label(name: &str) -> &str { | |
| 78 | - name.strip_prefix("build (") | |
| 79 | - .and_then(|name| name.strip_suffix(')')) | |
| 80 | - .unwrap_or(name) | |
| 81 | -} | |
| 82 | - | |
| 83 | 77 | fn span(from: Option<&str>, to: Option<&str>, seconds: bool) -> Option<String> { |
| 84 | 78 | let from: jiff::Timestamp = from?.parse().ok()?; |
| 85 | 79 | let to = to |
| @@ -141,7 +135,7 @@ fn job_row( | ||
| 141 | 135 | div() |
| 142 | 136 | .text_size(text(text::LABEL)) |
| 143 | 137 | .text_color(palette.text) |
| 144 | - .child(job_label(&job.name).to_string()), | |
| 138 | + .child(job.label().to_string()), | |
| 145 | 139 | ) |
| 146 | 140 | .children(detail.map(|detail| caption_text(detail, palette.muted).truncate())); |
| 147 | 141 | let id = job.id.clone(); |
crates/client/src/gui/builder/releases.rsmodified+1 -18
| @@ -2,7 +2,7 @@ use common::{ | ||
| 2 | 2 | call::{ImportRelease, ListReleases, PublishRelease, SetReleaseRequired}, |
| 3 | 3 | error::ReleaseProblem, |
| 4 | 4 | model::{ReleaseInfo, Staged}, |
| 5 | - update::{SERVER_TARGET, Sha}, | |
| 5 | + update::{Sha, Side}, | |
| 6 | 6 | }; |
| 7 | 7 | use gpui_kit::{ |
| 8 | 8 | AnyElement, Context, Div, Entity, Hsla, IntoElement, ParentElement, PathPromptOptions, Styled, |
| @@ -37,23 +37,6 @@ pub(crate) struct Desk { | ||
| 37 | 37 | pub(super) live: super::ci_view::Live, |
| 38 | 38 | } |
| 39 | 39 | |
| 40 | -#[derive(Clone, Copy)] | |
| 41 | -enum Side { | |
| 42 | - Server, | |
| 43 | - Client, | |
| 44 | - Both, | |
| 45 | -} | |
| 46 | - | |
| 47 | -impl Side { | |
| 48 | - fn takes(self, target: &str) -> bool { | |
| 49 | - match self { | |
| 50 | - Self::Server => target == SERVER_TARGET, | |
| 51 | - Self::Client => target != SERVER_TARGET, | |
| 52 | - Self::Both => true, | |
| 53 | - } | |
| 54 | - } | |
| 55 | -} | |
| 56 | - | |
| 57 | 40 | struct Draft { |
| 58 | 41 | id: u64, |
| 59 | 42 | name: String, |
crates/client/src/lib.rsmodified+2 -0
| @@ -7,6 +7,8 @@ pub mod accounts; | ||
| 7 | 7 | pub mod autostart; |
| 8 | 8 | pub mod avatar; |
| 9 | 9 | pub mod ci; |
| 10 | +#[cfg(target_os = "linux")] | |
| 11 | +pub mod cli; | |
| 10 | 12 | pub mod embed; |
| 11 | 13 | pub mod emoji; |
| 12 | 14 | pub mod gui; |
crates/client/src/main.rsmodified+5 -0
| @@ -27,6 +27,11 @@ fn main() { | ||
| 27 | 27 | ); |
| 28 | 28 | return; |
| 29 | 29 | } |
| 30 | + #[cfg(target_os = "linux")] | |
| 31 | + if client::cli::wanted() { | |
| 32 | + client::cli::run(); | |
| 33 | + return; | |
| 34 | + } | |
| 30 | 35 | tracing_subscriber::registry() |
| 31 | 36 | .with(tracing_subscriber::fmt::layer()) |
| 32 | 37 | .with( |
crates/common/src/update.rsmodified+30 -0
| @@ -143,3 +143,33 @@ mod tests { | ||
| 143 | 143 | ); |
| 144 | 144 | } |
| 145 | 145 | } |
| 146 | + | |
| 147 | +#[derive(Clone, Copy, Debug, PartialEq, Eq)] | |
| 148 | +pub enum Side { | |
| 149 | + Server, | |
| 150 | + Client, | |
| 151 | + Both, | |
| 152 | +} | |
| 153 | + | |
| 154 | +impl Side { | |
| 155 | + pub fn takes(self, target: &str) -> bool { | |
| 156 | + match self { | |
| 157 | + Self::Server => target == SERVER_TARGET, | |
| 158 | + Self::Client => target != SERVER_TARGET, | |
| 159 | + Self::Both => true, | |
| 160 | + } | |
| 161 | + } | |
| 162 | +} | |
| 163 | + | |
| 164 | +impl std::str::FromStr for Side { | |
| 165 | + type Err = &'static str; | |
| 166 | + | |
| 167 | + fn from_str(side: &str) -> Result<Self, Self::Err> { | |
| 168 | + match side { | |
| 169 | + "server" => Ok(Self::Server), | |
| 170 | + "client" => Ok(Self::Client), | |
| 171 | + "both" => Ok(Self::Both), | |
| 172 | + _ => Err("server, client or both"), | |
| 173 | + } | |
| 174 | + } | |
| 175 | +} |