| 5c6e92fd Andy McCall ↶ |
1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
mod client; |
| b7e1be26 Andy McCall ↶ |
6 |
mod mcp; |
| 5c6e92fd Andy McCall ↶ |
7 |
mod target; |
| 8 |
mod ui; |
| 9 |
|
| 10 |
use anyhow::{bail, Context, Result}; |
| 11 |
use clap::{Parser, Subcommand}; |
| 12 |
use client::Client; |
| fac6853c Andy McCall ↶ |
13 |
use hub_api::{ |
| 14 |
FlowJob, FlowLogLine, FlowRun, Issue, IssueStateChange, NewComment, NewIssue, NewProposal, NewRelease, Proposal, Release, Report, Tag, |
| 15 |
}; |
| 5c6e92fd Andy McCall ↶ |
16 |
use std::time::Duration; |
| 2bbe88a8 Andy McCall ↶ |
17 |
use target::{Choice, Config, Target}; |
| 5c6e92fd Andy McCall ↶ |
18 |
|
| 19 |
#[derive(Parser)] |
| 166f6e9b Andy McCall |
20 |
#[command(name = "oh", version = env!("OXY_VERSION_LONG"), about = "OxygenHub from the command line: issues, proposals, Flow runs, releases and drops")] |
| 5c6e92fd Andy McCall ↶ |
21 |
struct Cli { |
| 22 |
|
| 23 |
#[arg(long, short = 'R', global = true, value_name = "OWNER/NAME")] |
| 24 |
repo: Option<String>, |
| 25 |
|
| 26 |
#[arg(long, global = true, value_name = "URL")] |
| 27 |
hub: Option<String>, |
| 28 |
|
| 29 |
#[arg(long, global = true)] |
| 30 |
remote: Option<String>, |
| 31 |
|
| 32 |
#[arg(long, global = true)] |
| 33 |
json: bool, |
| 34 |
|
| 35 |
#[arg(long, global = true)] |
| 36 |
plain: bool, |
| 37 |
#[command(subcommand)] |
| 38 |
cmd: Cmd, |
| 39 |
} |
| 40 |
|
| 41 |
#[derive(Subcommand)] |
| 42 |
enum Cmd { |
| 43 |
|
| 44 |
Auth { |
| 45 |
#[command(subcommand)] |
| 46 |
cmd: AuthCmd, |
| 47 |
}, |
| 48 |
|
| 49 |
Repo { |
| 50 |
#[command(subcommand)] |
| 51 |
cmd: RepoCmd, |
| 52 |
}, |
| 53 |
|
| 54 |
Issue { |
| 55 |
#[command(subcommand)] |
| 56 |
cmd: IssueCmd, |
| 57 |
}, |
| 58 |
|
| 59 |
Proposal { |
| 60 |
#[command(subcommand)] |
| 61 |
cmd: ProposalCmd, |
| 62 |
}, |
| 40dd3f86 Andy McCall ↶ |
63 |
|
| 64 |
Coverage { |
| 65 |
#[command(subcommand)] |
| 66 |
cmd: CoverageCmd, |
| 67 |
}, |
| 5c6e92fd Andy McCall ↶ |
68 |
|
| 69 |
Run { |
| 70 |
#[command(subcommand)] |
| 71 |
cmd: RunCmd, |
| 72 |
}, |
| fac6853c Andy McCall ↶ |
73 |
|
| 74 |
Report { |
| 75 |
#[command(subcommand)] |
| 76 |
cmd: ReportCmd, |
| 77 |
}, |
| 5c6e92fd Andy McCall ↶ |
78 |
|
| 79 |
Release { |
| 80 |
#[command(subcommand)] |
| 81 |
cmd: ReleaseCmd, |
| 82 |
}, |
| ba0a55bb Andy McCall ↶ |
83 |
|
| 84 |
Tag { |
| 85 |
#[command(subcommand)] |
| 86 |
cmd: TagCmd, |
| 87 |
}, |
| 2291f8cc Andy McCall ↶ |
88 |
|
| 89 |
Label { |
| 90 |
#[command(subcommand)] |
| 91 |
cmd: LabelCmd, |
| 92 |
}, |
| 5ea4872f Andy McCall ↶ |
93 |
|
| 94 |
Milestone { |
| 95 |
#[command(subcommand)] |
| 96 |
cmd: MilestoneCmd, |
| 97 |
}, |
| 17a3a84b Andy McCall ↶ |
98 |
|
| 99 |
|
| 100 |
Project { |
| 101 |
#[command(subcommand)] |
| 102 |
cmd: ProjectCmd, |
| 103 |
|
| 104 |
#[arg(long, global = true)] |
| 105 |
owner: Option<String>, |
| 106 |
}, |
| 62ceb2ee Andy McCall ↶ |
107 |
|
| 108 |
|
| 109 |
Assistant { |
| 110 |
#[command(subcommand)] |
| 111 |
cmd: AssistantCmd, |
| 112 |
|
| 113 |
#[arg(long, global = true)] |
| 114 |
org: Option<String>, |
| 115 |
|
| 116 |
#[arg(long, global = true, conflicts_with = "org")] |
| 117 |
site: bool, |
| 118 |
}, |
| 8aa5ef38 Andy McCall ↶ |
119 |
|
| 120 |
|
| 121 |
Agent { |
| 122 |
#[command(subcommand)] |
| 123 |
cmd: AgentCmd, |
| 124 |
}, |
| 166f6e9b Andy McCall |
125 |
|
| 126 |
|
| 127 |
Drop { |
| 128 |
#[command(subcommand)] |
| 129 |
cmd: DropCmd, |
| 130 |
}, |
| 9bbca80f Andy McCall ↶ |
131 |
|
| 132 |
|
| 133 |
#[command(name = "runner-updates")] |
| 134 |
RunnerUpdates { |
| 135 |
#[command(subcommand)] |
| 136 |
cmd: Option<RunnerUpdatesCmd>, |
| 137 |
}, |
| b7e1be26 Andy McCall ↶ |
138 |
|
| 139 |
Mcp { |
| 140 |
|
| 141 |
#[arg(long)] |
| 142 |
allow_write: bool, |
| 143 |
}, |
| 5c6e92fd Andy McCall ↶ |
144 |
|
| 145 |
Api { |
| 146 |
|
| 147 |
path: String, |
| 148 |
|
| 149 |
#[arg(long, short = 'X', default_value = "GET")] |
| 150 |
method: String, |
| 151 |
|
| 152 |
#[arg(long, short = 'd')] |
| 153 |
data: Option<String>, |
| 154 |
}, |
| 155 |
} |
| 156 |
|
| 157 |
#[derive(Subcommand)] |
| 8aa5ef38 Andy McCall ↶ |
158 |
enum AgentCmd { |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
Grant { |
| 164 |
|
| 165 |
name: String, |
| 166 |
|
| 167 |
#[arg(long, default_value = "changes,seal,refs")] |
| 168 |
scope: String, |
| 169 |
|
| 170 |
#[arg(long = "for", value_name = "DURATION", default_value = "30d")] |
| 171 |
lasts: String, |
| 172 |
}, |
| 173 |
|
| 174 |
List, |
| 175 |
|
| 176 |
Revoke { id: i64 }, |
| 177 |
} |
| 178 |
|
| 179 |
#[derive(Subcommand)] |
| 166f6e9b Andy McCall |
180 |
enum DropCmd { |
| 181 |
|
| 182 |
Create { |
| 183 |
|
| 184 |
#[arg(required = true)] |
| 185 |
files: Vec<std::path::PathBuf>, |
| 186 |
|
| 187 |
#[arg(long)] |
| 188 |
secret: bool, |
| 189 |
|
| 190 |
#[arg(long, short = 'd', value_name = "TEXT")] |
| 191 |
description: Option<String>, |
| 192 |
|
| 193 |
#[arg(long, short = 't')] |
| 194 |
title: Option<String>, |
| 195 |
}, |
| 196 |
|
| 197 |
List { |
| 198 |
|
| 199 |
#[arg(long, short = 'u', value_name = "NAME")] |
| 200 |
user: Option<String>, |
| 201 |
}, |
| 202 |
|
| 203 |
View { |
| 204 |
|
| 205 |
id: String, |
| 206 |
|
| 207 |
#[arg(long, short = 'f', value_name = "NAME")] |
| 208 |
file: Option<String>, |
| 209 |
}, |
| 210 |
|
| 211 |
Delete { |
| 212 |
|
| 213 |
id: String, |
| 9bbca80f Andy McCall ↶ |
214 |
}, |
| 04f9c984 Andy McCall ↶ |
215 |
|
| 216 |
Fork { |
| 217 |
|
| 218 |
id: String, |
| 219 |
}, |
| 220 |
|
| 221 |
Star { |
| 222 |
|
| 223 |
id: String, |
| 224 |
}, |
| 225 |
|
| 226 |
Unstar { |
| 227 |
|
| 228 |
id: String, |
| 229 |
}, |
| 230 |
|
| 231 |
Comments { |
| 232 |
|
| 233 |
id: String, |
| 234 |
}, |
| 235 |
|
| 236 |
Comment { |
| 237 |
|
| 238 |
id: String, |
| 239 |
|
| 240 |
#[arg(long, short = 'm')] |
| 241 |
message: String, |
| 242 |
}, |
| 9bbca80f Andy McCall ↶ |
243 |
} |
| 244 |
|
| 245 |
#[derive(Subcommand)] |
| 246 |
enum RunnerUpdatesCmd { |
| 247 |
|
| 248 |
Show, |
| 249 |
|
| 250 |
Set { |
| 251 |
|
| 252 |
scope: String, |
| 253 |
|
| 254 |
#[arg(long)] |
| 255 |
updates: Option<String>, |
| 256 |
|
| 257 |
#[arg(long)] |
| 258 |
channel: Option<String>, |
| 259 |
|
| 260 |
#[arg(long)] |
| 261 |
window: Option<String>, |
| 262 |
|
| 263 |
#[arg(long)] |
| 264 |
dry_run: Option<String>, |
| 166f6e9b Andy McCall |
265 |
}, |
| 9bbca80f Andy McCall ↶ |
266 |
|
| 267 |
Lift { version: String }, |
| 166f6e9b Andy McCall |
268 |
} |
| 269 |
|
| 270 |
#[derive(Subcommand)] |
| 5c6e92fd Andy McCall ↶ |
271 |
enum AuthCmd { |
| df71c789 Andy McCall ↶ |
272 |
|
| 273 |
|
| 5c6e92fd Andy McCall ↶ |
274 |
Login { |
| df71c789 Andy McCall ↶ |
275 |
|
| 5c6e92fd Andy McCall ↶ |
276 |
#[arg(long = "with-hub", value_name = "URL")] |
| 277 |
url: String, |
| df71c789 Andy McCall ↶ |
278 |
|
| 279 |
|
| 5c6e92fd Andy McCall ↶ |
280 |
#[arg(long)] |
| 281 |
token: Option<String>, |
| 282 |
|
| 283 |
#[arg(long)] |
| 284 |
ca: Option<std::path::PathBuf>, |
| 285 |
}, |
| 286 |
|
| 287 |
Status, |
| 288 |
} |
| 289 |
|
| 290 |
#[derive(Subcommand)] |
| 291 |
enum RepoCmd { |
| 292 |
|
| 293 |
View, |
| 294 |
} |
| 295 |
|
| 296 |
#[derive(Subcommand)] |
| 297 |
enum IssueCmd { |
| 2291f8cc Andy McCall ↶ |
298 |
|
| 299 |
Label(LabelChange), |
| 5c6e92fd Andy McCall ↶ |
300 |
|
| 301 |
List { |
| 302 |
#[arg(long, default_value = "open")] |
| 303 |
state: String, |
| 304 |
}, |
| 305 |
|
| 306 |
View { number: i64 }, |
| 307 |
|
| 308 |
Create { |
| 309 |
#[arg(long, short)] |
| 310 |
title: String, |
| 311 |
#[arg(long, short)] |
| 312 |
body: Option<String>, |
| 313 |
|
| 314 |
#[arg(long, short = 'F')] |
| 315 |
body_file: Option<String>, |
| 316 |
|
| 317 |
#[arg(long, short)] |
| 318 |
label: Vec<String>, |
| 17a3a84b Andy McCall ↶ |
319 |
|
| 320 |
#[arg(long, short)] |
| 321 |
project: Option<i64>, |
| 322 |
|
| 323 |
#[arg(long, requires = "project")] |
| 324 |
column: Option<String>, |
| 1c52d940 Andy McCall ↶ |
325 |
|
| 326 |
#[arg(long, short = 'a', value_name = "FILE")] |
| 327 |
attach: Vec<std::path::PathBuf>, |
| 5c6e92fd Andy McCall ↶ |
328 |
}, |
| 306aec18 Andy McCall ↶ |
329 |
|
| 5c6e92fd Andy McCall ↶ |
330 |
Close { |
| 331 |
number: i64, |
| 332 |
|
| 333 |
#[arg(long, default_value = "completed")] |
| 334 |
reason: String, |
| 306aec18 Andy McCall ↶ |
335 |
#[command(flatten)] |
| 336 |
comment: StateComment, |
| 5c6e92fd Andy McCall ↶ |
337 |
}, |
| 306aec18 Andy McCall ↶ |
338 |
|
| 339 |
Reopen { |
| 340 |
number: i64, |
| 341 |
#[command(flatten)] |
| 342 |
comment: StateComment, |
| 343 |
}, |
| b5229b56 Andy McCall ↶ |
344 |
|
| 345 |
|
| 346 |
Edit { |
| 347 |
number: i64, |
| 348 |
#[arg(long, short)] |
| 349 |
title: Option<String>, |
| 350 |
#[arg(long, short)] |
| 351 |
body: Option<String>, |
| 352 |
|
| 353 |
#[arg(long, short = 'F', conflicts_with = "body")] |
| 354 |
body_file: Option<String>, |
| 355 |
|
| 356 |
#[arg(long, short)] |
| 357 |
label: Vec<String>, |
| 358 |
|
| 359 |
#[arg(long, conflicts_with = "label")] |
| 360 |
no_labels: bool, |
| 361 |
|
| 362 |
#[arg(long, short)] |
| 363 |
assignee: Vec<String>, |
| 364 |
|
| 365 |
#[arg(long, conflicts_with = "assignee")] |
| 366 |
no_assignees: bool, |
| 367 |
|
| 368 |
#[arg(long, short)] |
| 369 |
milestone: Option<String>, |
| 370 |
}, |
| 5c6e92fd Andy McCall ↶ |
371 |
|
| 372 |
Comment { |
| 373 |
number: i64, |
| 374 |
#[arg(long, short)] |
| bd91edeb Andy McCall ↶ |
375 |
body: Option<String>, |
| 376 |
|
| 377 |
|
| 378 |
#[arg(long, short = 'F', conflicts_with = "body")] |
| 379 |
body_file: Option<String>, |
| 1c52d940 Andy McCall ↶ |
380 |
|
| 381 |
#[arg(long, short = 'a', value_name = "FILE")] |
| 382 |
attach: Vec<std::path::PathBuf>, |
| 5c6e92fd Andy McCall ↶ |
383 |
}, |
| 384 |
} |
| 385 |
|
| 386 |
#[derive(Subcommand)] |
| 387 |
enum ProposalCmd { |
| d7e2c275 Andy McCall ↶ |
388 |
|
| 0b20f1ec Andy McCall ↶ |
389 |
List { |
| 390 |
#[arg(long, default_value = "open")] |
| 391 |
state: String, |
| d7e2c275 Andy McCall ↶ |
392 |
|
| 393 |
#[arg(long, default_value_t = 30)] |
| 394 |
limit: usize, |
| 0b20f1ec Andy McCall ↶ |
395 |
}, |
| 5c6e92fd Andy McCall ↶ |
396 |
|
| 397 |
View { number: i64 }, |
| 398 |
|
| 399 |
Create { |
| 400 |
#[arg(long, short)] |
| 401 |
title: String, |
| 402 |
#[arg(long, short)] |
| 403 |
body: Option<String>, |
| 739a763a Andy McCall ↶ |
404 |
|
| 405 |
#[arg(long, short = 'F', conflicts_with = "body")] |
| 406 |
body_file: Option<String>, |
| 5c6e92fd Andy McCall ↶ |
407 |
|
| 408 |
#[arg(long)] |
| 409 |
from: String, |
| 410 |
|
| 411 |
#[arg(long)] |
| 412 |
into: Option<String>, |
| 413 |
#[arg(long)] |
| 414 |
draft: bool, |
| 739a763a Andy McCall ↶ |
415 |
}, |
| 416 |
|
| 417 |
Edit { |
| 418 |
number: i64, |
| 419 |
#[arg(long, short)] |
| 420 |
title: Option<String>, |
| 421 |
#[arg(long, short)] |
| 422 |
body: Option<String>, |
| 423 |
|
| 424 |
#[arg(long, short = 'F', conflicts_with = "body")] |
| 425 |
body_file: Option<String>, |
| 5c6e92fd Andy McCall ↶ |
426 |
}, |
| 2291f8cc Andy McCall ↶ |
427 |
|
| 428 |
Label(LabelChange), |
| 739a763a Andy McCall ↶ |
429 |
|
| 430 |
Comment { |
| 431 |
number: i64, |
| 432 |
#[arg(long, short)] |
| 433 |
body: Option<String>, |
| 434 |
|
| 435 |
#[arg(long, short = 'F', conflicts_with = "body")] |
| 436 |
body_file: Option<String>, |
| 437 |
}, |
| 5c6e92fd Andy McCall ↶ |
438 |
|
| 439 |
Checks { number: i64 }, |
| 1f1eb06a Andy McCall ↶ |
440 |
|
| 2a8b6216 Andy McCall ↶ |
441 |
Land { |
| 442 |
number: i64, |
| 443 |
|
| 444 |
|
| 86a69b53 Andy McCall ↶ |
445 |
#[arg(long, value_name = "REASON", conflicts_with = "when_ready")] |
| 2a8b6216 Andy McCall ↶ |
446 |
over_checks: Option<String>, |
| 86a69b53 Andy McCall ↶ |
447 |
|
| 448 |
#[arg(long)] |
| 449 |
when_ready: bool, |
| 450 |
|
| 451 |
#[arg(long, requires = "when_ready")] |
| 452 |
off: bool, |
| 2a8b6216 Andy McCall ↶ |
453 |
}, |
| 71e65fcf Andy McCall ↶ |
454 |
|
| 455 |
Close { |
| 456 |
number: i64, |
| 457 |
|
| 458 |
#[arg(long, short = 'm')] |
| 459 |
message: Option<String>, |
| 460 |
}, |
| 461 |
|
| 462 |
Reopen { number: i64 }, |
| 09ffa7b6 Andy McCall ↶ |
463 |
|
| 464 |
Ready { |
| 465 |
number: i64, |
| 466 |
|
| 467 |
#[arg(long)] |
| 468 |
undo: bool, |
| 469 |
}, |
| 62ceb2ee Andy McCall ↶ |
470 |
} |
| 471 |
|
| 472 |
#[derive(Subcommand)] |
| 473 |
enum AssistantCmd { |
| 474 |
|
| 475 |
List, |
| 476 |
|
| 477 |
Add { |
| 478 |
|
| 479 |
#[arg(long, default_value = "")] |
| 480 |
name: String, |
| 481 |
|
| 482 |
#[arg(long, default_value = "")] |
| 483 |
preset: String, |
| 484 |
|
| 485 |
#[arg(long, default_value = "")] |
| 486 |
endpoint: String, |
| 487 |
|
| 488 |
#[arg(long, default_value = "")] |
| 489 |
api: String, |
| 490 |
|
| 491 |
#[arg(long)] |
| 492 |
model: String, |
| 493 |
|
| 494 |
#[arg(long, value_name = "VAR")] |
| 495 |
token_env: Option<String>, |
| 496 |
|
| 497 |
#[arg(long)] |
| 498 |
off: bool, |
| 499 |
}, |
| 500 |
|
| 501 |
Remove { name: String }, |
| 502 |
|
| 503 |
Prompt { |
| 504 |
|
| 505 |
#[arg(long, value_name = "FILE")] |
| 506 |
set: Option<std::path::PathBuf>, |
| 507 |
}, |
| 2291f8cc Andy McCall ↶ |
508 |
} |
| 509 |
|
| 510 |
#[derive(Subcommand)] |
| 511 |
enum LabelCmd { |
| 512 |
|
| 513 |
List, |
| 514 |
|
| 515 |
Create { |
| 516 |
name: String, |
| 517 |
|
| 518 |
#[arg(long)] |
| 519 |
colour: Option<String>, |
| 520 |
#[arg(long, short, default_value = "")] |
| 521 |
description: String, |
| 522 |
}, |
| 523 |
|
| 524 |
Delete { name: String }, |
| 17a3a84b Andy McCall ↶ |
525 |
} |
| 526 |
|
| 527 |
#[derive(Subcommand)] |
| 528 |
enum ProjectCmd { |
| 529 |
|
| 530 |
List, |
| 531 |
|
| 439ecd87 Andy McCall ↶ |
532 |
View { |
| 533 |
number: i64, |
| 534 |
|
| 535 |
#[arg(long)] |
| 536 |
archived: bool, |
| 537 |
}, |
| 17a3a84b Andy McCall ↶ |
538 |
|
| 539 |
Create { |
| 540 |
title: String, |
| 541 |
#[arg(long, short, default_value = "")] |
| 542 |
description: String, |
| 543 |
|
| 544 |
#[arg(long)] |
| 545 |
private: bool, |
| 546 |
}, |
| 547 |
|
| 548 |
Close { number: i64 }, |
| 549 |
|
| 550 |
Reopen { number: i64 }, |
| b1e92a62 Andy McCall ↶ |
551 |
|
| 552 |
|
| 553 |
Transfer { |
| 554 |
number: i64, |
| 555 |
|
| 556 |
#[arg(long)] |
| 557 |
to: String, |
| 558 |
}, |
| 439ecd87 Andy McCall ↶ |
559 |
|
| 17a3a84b Andy McCall ↶ |
560 |
Card { |
| 561 |
#[command(subcommand)] |
| 562 |
cmd: CardCmd, |
| 306aec18 Andy McCall ↶ |
563 |
}, |
| 564 |
|
| 565 |
Column { |
| 566 |
#[command(subcommand)] |
| 567 |
cmd: ColumnCmd, |
| 568 |
}, |
| 569 |
} |
| 570 |
|
| 571 |
#[derive(Subcommand)] |
| 572 |
enum ColumnCmd { |
| 573 |
|
| 574 |
Add { |
| 575 |
|
| 576 |
number: i64, |
| 577 |
name: String, |
| 17a3a84b Andy McCall ↶ |
578 |
}, |
| 306aec18 Andy McCall ↶ |
579 |
|
| 580 |
Rename { |
| 581 |
|
| 582 |
number: i64, |
| 583 |
column: String, |
| 584 |
|
| 585 |
to: String, |
| 586 |
}, |
| 587 |
|
| 588 |
Move { |
| 589 |
|
| 590 |
number: i64, |
| 591 |
column: String, |
| 592 |
|
| 593 |
#[arg(long, conflicts_with_all = ["left", "right"], required_unless_present_any = ["left", "right"])] |
| 594 |
to: Option<i64>, |
| 595 |
|
| 596 |
#[arg(long, conflicts_with = "right")] |
| 597 |
left: bool, |
| 598 |
|
| 599 |
#[arg(long)] |
| 600 |
right: bool, |
| 601 |
}, |
| 602 |
|
| 603 |
Remove { |
| 604 |
|
| 605 |
number: i64, |
| 606 |
column: String, |
| 607 |
}, |
| 2291f8cc Andy McCall ↶ |
608 |
} |
| 609 |
|
| 5ea4872f Andy McCall ↶ |
610 |
#[derive(Subcommand)] |
| 17a3a84b Andy McCall ↶ |
611 |
enum CardCmd { |
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
Add { |
| 616 |
number: i64, |
| 617 |
|
| 618 |
reference: Option<String>, |
| 619 |
|
| 620 |
#[arg(long, conflicts_with = "reference")] |
| 621 |
note: Option<String>, |
| 622 |
|
| 623 |
#[arg(long, short)] |
| 624 |
column: Option<String>, |
| 625 |
}, |
| 456f4197 Andy McCall ↶ |
626 |
|
| 627 |
|
| 17a3a84b Andy McCall ↶ |
628 |
Move { |
| 629 |
number: i64, |
| 456f4197 Andy McCall ↶ |
630 |
|
| 631 |
card: Option<i64>, |
| 17a3a84b Andy McCall ↶ |
632 |
#[arg(long, short)] |
| 633 |
column: String, |
| 456f4197 Andy McCall ↶ |
634 |
|
| 635 |
#[arg(long, conflicts_with = "card")] |
| 636 |
from: Option<String>, |
| 637 |
|
| 638 |
#[arg(long, value_delimiter = ',', requires = "from")] |
| 639 |
except: Vec<String>, |
| 640 |
|
| 641 |
#[arg(long, value_delimiter = ',', requires = "from")] |
| 642 |
state: Vec<String>, |
| 643 |
|
| 644 |
#[arg(long, requires = "from")] |
| 439ecd87 Andy McCall ↶ |
645 |
dry_run: bool, |
| 646 |
}, |
| 647 |
|
| 648 |
Archive { |
| 649 |
number: i64, |
| 650 |
|
| 651 |
#[arg(long)] |
| 652 |
from: String, |
| 653 |
|
| 654 |
#[arg(long)] |
| 655 |
older: Option<String>, |
| 656 |
|
| 657 |
#[arg(long, value_delimiter = ',')] |
| 658 |
except: Vec<String>, |
| 659 |
|
| 660 |
#[arg(long, value_delimiter = ',')] |
| 661 |
state: Vec<String>, |
| 662 |
|
| 663 |
#[arg(long)] |
| 456f4197 Andy McCall ↶ |
664 |
dry_run: bool, |
| 17a3a84b Andy McCall ↶ |
665 |
}, |
| 439ecd87 Andy McCall ↶ |
666 |
|
| 667 |
Restore { number: i64, card: i64 }, |
| 17a3a84b Andy McCall ↶ |
668 |
|
| 669 |
Remove { number: i64, card: i64 }, |
| 670 |
} |
| 671 |
|
| 672 |
#[derive(Subcommand)] |
| 5ea4872f Andy McCall ↶ |
673 |
enum MilestoneCmd { |
| 674 |
|
| 675 |
List, |
| 676 |
|
| 677 |
Create { |
| 678 |
title: String, |
| 679 |
#[arg(long, short, default_value = "")] |
| 680 |
description: String, |
| 681 |
|
| 682 |
#[arg(long)] |
| 683 |
due: Option<String>, |
| 684 |
}, |
| 685 |
|
| 686 |
Edit { |
| 687 |
title: String, |
| 688 |
#[arg(long, short)] |
| 689 |
rename: Option<String>, |
| 690 |
#[arg(long, short)] |
| 691 |
description: Option<String>, |
| 692 |
|
| 693 |
#[arg(long)] |
| 694 |
due: Option<String>, |
| 695 |
}, |
| 696 |
|
| 697 |
Close { title: String }, |
| 698 |
|
| 699 |
Reopen { title: String }, |
| 700 |
} |
| 701 |
|
| 2291f8cc Andy McCall ↶ |
702 |
|
| 703 |
#[derive(clap::Args)] |
| 704 |
struct LabelChange { |
| 705 |
number: i64, |
| 706 |
|
| 707 |
#[arg(long, short)] |
| 708 |
add: Vec<String>, |
| 709 |
|
| 710 |
#[arg(long, short)] |
| 711 |
remove: Vec<String>, |
| 712 |
} |
| 713 |
|
| 714 |
impl LabelChange { |
| 715 |
|
| 716 |
fn apply(&self, now: &[hub_api::Label]) -> Result<Vec<String>> { |
| 717 |
if self.add.is_empty() && self.remove.is_empty() { |
| 718 |
bail!("say what to change: --add NAME or --remove NAME"); |
| 719 |
} |
| 720 |
let mut names: Vec<String> = now.iter().map(|l| l.name.clone()).filter(|n| !self.remove.contains(n)).collect(); |
| 721 |
for a in &self.add { |
| 722 |
if !names.contains(a) { |
| 723 |
names.push(a.clone()); |
| 724 |
} |
| 725 |
} |
| 726 |
Ok(names) |
| 727 |
} |
| 728 |
} |
| 729 |
|
| 730 |
fn label_names(labels: &[String]) -> String { |
| 731 |
if labels.is_empty() { |
| 732 |
ui::dim("no labels") |
| 733 |
} else { |
| 734 |
labels.iter().map(|l| format!("[{l}]")).collect::<Vec<_>>().join(" ") |
| 735 |
} |
| 40dd3f86 Andy McCall ↶ |
736 |
} |
| 737 |
|
| 738 |
#[derive(Subcommand)] |
| 739 |
enum CoverageCmd { |
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
Upload { |
| 745 |
file: std::path::PathBuf, |
| 746 |
#[arg(long)] |
| 747 |
bookmark: Option<String>, |
| 748 |
|
| 749 |
#[arg(long, conflicts_with = "bookmark")] |
| 750 |
change: Option<String>, |
| 751 |
}, |
| 5c6e92fd Andy McCall ↶ |
752 |
} |
| 753 |
|
| 754 |
#[derive(Subcommand)] |
| 755 |
enum RunCmd { |
| 756 |
|
| 757 |
List { |
| 758 |
#[arg(long, default_value_t = 20)] |
| 759 |
limit: i64, |
| 760 |
}, |
| 306aec18 Andy McCall ↶ |
761 |
|
| fac6853c Andy McCall ↶ |
762 |
View { |
| 306aec18 Andy McCall ↶ |
763 |
|
| 764 |
number: Option<i64>, |
| 765 |
|
| 766 |
#[arg(long, conflicts_with = "number")] |
| 767 |
bookmark: Option<String>, |
| fac6853c Andy McCall ↶ |
768 |
|
| 769 |
#[arg(long)] |
| 770 |
timings: bool, |
| 771 |
}, |
| 5c6e92fd Andy McCall ↶ |
772 |
|
| 773 |
Start { |
| 774 |
#[arg(long)] |
| 775 |
bookmark: Option<String>, |
| 776 |
|
| 777 |
#[arg(long, conflicts_with = "bookmark")] |
| 778 |
change: Option<String>, |
| 0650d94e Andy McCall ↶ |
779 |
|
| 780 |
#[arg(long = "job", value_name = "NAME")] |
| 781 |
jobs: Vec<String>, |
| 5c6e92fd Andy McCall ↶ |
782 |
|
| 783 |
#[arg(long)] |
| 784 |
watch: bool, |
| 785 |
}, |
| 306aec18 Andy McCall ↶ |
786 |
|
| 787 |
Watch { |
| 788 |
|
| 789 |
number: Option<i64>, |
| 790 |
|
| 791 |
#[arg(long, conflicts_with = "number")] |
| 792 |
bookmark: Option<String>, |
| 793 |
}, |
| 5c6e92fd Andy McCall ↶ |
794 |
|
| 795 |
Log { number: i64, job: String }, |
| fac6853c Andy McCall ↶ |
796 |
|
| 797 |
Report { |
| 798 |
number: i64, |
| 799 |
job: String, |
| 800 |
#[command(flatten)] |
| 801 |
pick: ReportPick, |
| 802 |
}, |
| e8fa5369 Andy McCall ↶ |
803 |
|
| 804 |
|
| 805 |
Rerun { |
| 806 |
number: i64, |
| 807 |
#[arg(long)] |
| 808 |
job: Option<String>, |
| 809 |
}, |
| 745d225d Andy McCall ↶ |
810 |
|
| 811 |
Cancel { number: i64 }, |
| dc59d1b5 Andy McCall ↶ |
812 |
|
| 813 |
Approve { |
| 814 |
number: i64, |
| 815 |
#[arg(long)] |
| 816 |
job: String, |
| 817 |
}, |
| 818 |
|
| 819 |
Reject { |
| 820 |
number: i64, |
| 821 |
#[arg(long)] |
| 822 |
job: String, |
| 823 |
}, |
| fac6853c Andy McCall ↶ |
824 |
} |
| 825 |
|
| 826 |
|
| 827 |
#[derive(clap::Args)] |
| 828 |
struct ReportPick { |
| 829 |
|
| 830 |
#[arg(long)] |
| 831 |
name: Option<String>, |
| 832 |
|
| 833 |
#[arg(long, conflicts_with = "file")] |
| 834 |
list: bool, |
| 835 |
|
| 836 |
#[arg(long, value_name = "PATH")] |
| 837 |
file: Option<String>, |
| 838 |
|
| 839 |
#[arg(long, short, value_name = "FILE", requires = "file")] |
| 840 |
output: Option<std::path::PathBuf>, |
| 5c6e92fd Andy McCall ↶ |
841 |
} |
| 842 |
|
| 843 |
#[derive(Subcommand)] |
| fac6853c Andy McCall ↶ |
844 |
enum ReportCmd { |
| 845 |
|
| 846 |
List { |
| 847 |
|
| 848 |
#[arg(long)] |
| 849 |
bookmark: Option<String>, |
| 850 |
}, |
| 851 |
|
| 852 |
Show { |
| 853 |
report: String, |
| 854 |
#[arg(long, conflicts_with = "bookmark")] |
| 855 |
id: Option<i64>, |
| 856 |
#[arg(long)] |
| 857 |
bookmark: Option<String>, |
| 858 |
|
| 859 |
#[arg(long, conflicts_with = "file")] |
| 860 |
list: bool, |
| 861 |
|
| 862 |
#[arg(long, value_name = "PATH")] |
| 863 |
file: Option<String>, |
| 864 |
|
| 865 |
#[arg(long, short, value_name = "FILE", requires = "file")] |
| 866 |
output: Option<std::path::PathBuf>, |
| 867 |
}, |
| 868 |
} |
| 869 |
|
| 870 |
#[derive(Subcommand)] |
| ba0a55bb Andy McCall ↶ |
871 |
enum TagCmd { |
| 872 |
|
| 873 |
List, |
| 874 |
|
| 875 |
|
| 876 |
Create { |
| 877 |
name: String, |
| 878 |
|
| 879 |
#[arg(long)] |
| 880 |
target: Option<String>, |
| 881 |
#[arg(long, short)] |
| 882 |
message: Option<String>, |
| 883 |
}, |
| 884 |
|
| 885 |
|
| 886 |
|
| 887 |
Delete { |
| 888 |
name: String, |
| 889 |
|
| 890 |
#[arg(long, short)] |
| 891 |
reason: Option<String>, |
| 892 |
}, |
| 893 |
} |
| 894 |
|
| 895 |
#[derive(Subcommand)] |
| 5c6e92fd Andy McCall ↶ |
896 |
enum ReleaseCmd { |
| 897 |
|
| 898 |
List, |
| 899 |
|
| 900 |
Create { |
| 901 |
tag: String, |
| 902 |
#[arg(long, short)] |
| 903 |
title: Option<String>, |
| 904 |
|
| 905 |
#[arg(long, short)] |
| 906 |
notes: Option<String>, |
| 907 |
#[arg(long)] |
| 908 |
prerelease: bool, |
| 909 |
}, |
| ced6d3af Andy McCall ↶ |
910 |
|
| 911 |
Edit { |
| 912 |
tag: String, |
| 913 |
#[arg(long, short)] |
| 914 |
title: Option<String>, |
| 915 |
|
| 916 |
#[arg(long, short)] |
| 917 |
notes: Option<String>, |
| 918 |
|
| 919 |
#[arg(long)] |
| 920 |
prerelease: Option<bool>, |
| 921 |
}, |
| 5c6e92fd Andy McCall ↶ |
922 |
|
| 923 |
Upload { tag: String, files: Vec<std::path::PathBuf> }, |
| 924 |
|
| 925 |
Trust { tag: String }, |
| 926 |
} |
| 927 |
|
| 928 |
fn main() { |
| b5229b56 Andy McCall ↶ |
929 |
|
| 930 |
|
| 931 |
|
| 932 |
#[cfg(unix)] |
| 933 |
unsafe { |
| 934 |
libc::signal(libc::SIGPIPE, libc::SIG_DFL); |
| 935 |
} |
| 5c6e92fd Andy McCall ↶ |
936 |
let cli = Cli::parse(); |
| 937 |
ui::init(cli.plain); |
| 938 |
match run(cli) { |
| 939 |
Ok(code) => std::process::exit(code), |
| 940 |
Err(e) => { |
| 941 |
eprintln!("{} {e:#}", ui::bad("error:")); |
| 942 |
std::process::exit(2); |
| 943 |
} |
| 944 |
} |
| 945 |
} |
| 946 |
|
| 947 |
fn enc(s: &str) -> String { |
| 948 |
let mut out = String::new(); |
| 949 |
for b in s.bytes() { |
| 950 |
if b.is_ascii_alphanumeric() || b"-._~".contains(&b) { |
| 951 |
out.push(b as char); |
| 952 |
} else { |
| 953 |
out.push_str(&format!("%{b:02X}")); |
| 954 |
} |
| 955 |
} |
| 956 |
out |
| 957 |
} |
| 958 |
|
| 959 |
fn run(cli: Cli) -> Result<i32> { |
| 960 |
let cfg = Config::load()?; |
| 961 |
if let Cmd::Auth { cmd: AuthCmd::Login { url, token, ca } } = &cli.cmd { |
| 962 |
return login(cfg, url, token.clone(), ca.clone()); |
| 963 |
} |
| 964 |
let here = std::env::current_dir()?; |
| 965 |
let t = target::resolve(&Choice { repo: cli.repo.as_deref(), hub: cli.hub.as_deref(), remote: cli.remote.as_deref() }, &here, &cfg)?; |
| 966 |
let c = Client::new(&t)?; |
| 967 |
let json = cli.json; |
| 968 |
match cli.cmd { |
| 969 |
Cmd::Auth { cmd: AuthCmd::Status } => { |
| 970 |
let me: serde_json::Value = c.get("/user")?; |
| 971 |
if json { |
| 972 |
println!("{me}"); |
| 973 |
} else { |
| 974 |
println!("{} signed in to {} as {}", ui::ok("✔"), t.hub, ui::strong(me["username"].as_str().unwrap_or("?"))); |
| 975 |
} |
| 976 |
Ok(0) |
| 977 |
} |
| 978 |
Cmd::Auth { .. } => unreachable!(), |
| b7e1be26 Andy McCall ↶ |
979 |
Cmd::Mcp { allow_write } => { |
| 980 |
t.repo().context("oh mcp serves one repository: start it in a checkout, or give --repo OWNER/NAME")?; |
| 981 |
mcp::serve(c, t, allow_write) |
| 982 |
} |
| 5c6e92fd Andy McCall ↶ |
983 |
Cmd::Api { path, method, data } => { |
| 984 |
let path = if path.starts_with('/') { path } else { format!("/{path}") }; |
| 1b770a1f Andy McCall ↶ |
985 |
|
| 986 |
|
| 5c6e92fd Andy McCall ↶ |
987 |
let text = match data { |
| 988 |
Some(d) => c.send_text( |
| 989 |
&method.to_uppercase(), |
| 990 |
&path, |
| 991 |
&serde_json::from_str::<serde_json::Value>(&d).context("--data is not JSON")?, |
| 992 |
)?, |
| 1b770a1f Andy McCall ↶ |
993 |
None if method.eq_ignore_ascii_case("GET") => c.get_text(&path)?, |
| 994 |
None => c.send_text(&method.to_uppercase(), &path, &serde_json::json!({}))?, |
| 5c6e92fd Andy McCall ↶ |
995 |
}; |
| 996 |
println!("{text}"); |
| 997 |
Ok(0) |
| 998 |
} |
| 999 |
Cmd::Repo { cmd: RepoCmd::View } => { |
| 1000 |
let path = t.repo_path()?; |
| 1001 |
if json { |
| 1002 |
println!("{}", c.get_text(&path)?); |
| 1003 |
return Ok(0); |
| 1004 |
} |
| 1005 |
let r: serde_json::Value = c.get(&path)?; |
| 1006 |
println!("{} {}", ui::strong(&format!("{}/{}", t.owner, t.name)), ui::dim(r["visibility"].as_str().unwrap_or(""))); |
| 1007 |
if let Some(d) = r["description"].as_str().filter(|d| !d.is_empty()) { |
| 1008 |
println!("{d}"); |
| 1009 |
} |
| 1010 |
println!("{}", ui::kv("default bookmark", r["default_bookmark"].as_str().unwrap_or(""))); |
| 1011 |
println!("{}", ui::kv("clone", &format!("oxy clone {}/{}/{}", t.hub, t.owner, t.name))); |
| 1012 |
Ok(0) |
| 1013 |
} |
| 1014 |
Cmd::Issue { cmd } => issue(&c, &t, cmd, json), |
| 1015 |
Cmd::Proposal { cmd } => proposal(&c, &t, cmd, json), |
| 1016 |
Cmd::Run { cmd } => flow(&c, &t, cmd, json), |
| fac6853c Andy McCall ↶ |
1017 |
Cmd::Report { cmd } => report(&c, &t, cmd, json), |
| 40dd3f86 Andy McCall ↶ |
1018 |
Cmd::Coverage { cmd: CoverageCmd::Upload { file, bookmark, change } } => { |
| 1019 |
let content = std::fs::read_to_string(&file).with_context(|| format!("read {}", file.display()))?; |
| 1020 |
let mut body = serde_json::json!({ "content": content }); |
| 1021 |
if let Some(b) = bookmark { |
| 1022 |
body["bookmark"] = b.into(); |
| 1023 |
} |
| 1024 |
if let Some(ch) = change { |
| 1025 |
body["change"] = ch.into(); |
| 1026 |
} |
| 1027 |
let base = t.repo_path()?; |
| 1028 |
let r: hub_api::CoverageReport = c.send("POST", &format!("{base}/coverage"), &body)?; |
| 1029 |
if json { |
| 1030 |
println!("{}", serde_json::to_string(&r)?); |
| 1031 |
} else { |
| 1032 |
let pct = r.percent.map(|p| format!("{p:.1}%")).unwrap_or_else(|| "no lines".into()); |
| 1033 |
let on = if r.bookmark.is_empty() { format!("state {}", &r.state[..12.min(r.state.len())]) } else { r.bookmark.clone() }; |
| 1034 |
println!( |
| 1035 |
"{} uploaded coverage {pct} ({} of {} lines, {} files) for {on} {}", |
| 1036 |
ui::ok("✔"), |
| 1037 |
r.lines_hit, |
| 1038 |
r.lines_found, |
| 1039 |
r.files, |
| 1040 |
ui::dim(&r.url) |
| 1041 |
); |
| 1042 |
if r.dropped > 0 { |
| 1043 |
println!(" {}", ui::dim(&format!("{} files it named are not in that state and were left out", r.dropped))); |
| 1044 |
} |
| 1045 |
} |
| 1046 |
Ok(0) |
| 1047 |
} |
| 5c6e92fd Andy McCall ↶ |
1048 |
Cmd::Release { cmd } => release(&c, &t, cmd, json), |
| ba0a55bb Andy McCall ↶ |
1049 |
Cmd::Tag { cmd } => tag(&c, &t, cmd, json), |
| 2291f8cc Andy McCall ↶ |
1050 |
Cmd::Label { cmd } => label(&c, &t, cmd, json), |
| 5ea4872f Andy McCall ↶ |
1051 |
Cmd::Milestone { cmd } => milestone(&c, &t, cmd, json), |
| 17a3a84b Andy McCall ↶ |
1052 |
Cmd::Project { cmd, owner } => project(&c, &t, cmd, owner, json), |
| 62ceb2ee Andy McCall ↶ |
1053 |
Cmd::Assistant { cmd, org, site } => assistant(&c, &t, cmd, org, site, json), |
| 8aa5ef38 Andy McCall ↶ |
1054 |
Cmd::Agent { cmd } => agent(&c, cmd, json), |
| 9bbca80f Andy McCall ↶ |
1055 |
Cmd::RunnerUpdates { cmd } => runner_updates(&c, cmd.unwrap_or(RunnerUpdatesCmd::Show), json), |
| 166f6e9b Andy McCall |
1056 |
Cmd::Drop { cmd } => drop_cmd(&c, cmd, json), |
| 9bbca80f Andy McCall ↶ |
1057 |
} |
| 1058 |
} |
| 1059 |
|
| 1060 |
|
| 1061 |
fn switch(v: Option<&str>) -> Result<Option<Option<bool>>> { |
| 1062 |
Ok(match v { |
| 1063 |
None => None, |
| 1064 |
Some("on") => Some(Some(true)), |
| 1065 |
Some("off") => Some(Some(false)), |
| 1066 |
Some("inherit") => Some(None), |
| 1067 |
Some(other) => bail!("{other:?}: say on, off or inherit"), |
| 1068 |
}) |
| 1069 |
} |
| 1070 |
|
| 1071 |
|
| 1072 |
fn update_scope(scope: &str, all: &hub_api::RunnerUpdates) -> Result<(String, String)> { |
| 1073 |
let (kind, name) = scope.split_once(':').unwrap_or((scope, "")); |
| 1074 |
Ok(match kind { |
| 1075 |
"site" if name.is_empty() => ("site".into(), String::new()), |
| 1076 |
"owner" if !name.is_empty() => ("owner".into(), name.into()), |
| 1077 |
"group" => { |
| 1078 |
let g = all |
| 1079 |
.groups |
| 1080 |
.iter() |
| 1081 |
.find(|g| g.id.to_string() == name || g.name == name || format!("{}/{}", g.owner, g.name) == name) |
| 1082 |
.with_context(|| format!("no test group {name:?} (see `oh runner-updates`)"))?; |
| 1083 |
("group".into(), g.id.to_string()) |
| 1084 |
} |
| 1085 |
"runner" => { |
| 1086 |
let r = all |
| 1087 |
.runners |
| 1088 |
.iter() |
| 1089 |
.find(|r| r.id.to_string() == name || r.name == name) |
| 1090 |
.with_context(|| format!("no runner {name:?} (see `oh runner-updates`)"))?; |
| 1091 |
("runner".into(), r.id.to_string()) |
| 1092 |
} |
| 1093 |
_ => bail!("{scope:?}: say site, owner:NAME, group:NAME or runner:NAME"), |
| 1094 |
}) |
| 1095 |
} |
| 1096 |
|
| 1097 |
fn runner_updates(c: &Client, cmd: RunnerUpdatesCmd, json: bool) -> Result<i32> { |
| 1098 |
let all: hub_api::RunnerUpdates = c.get("/admin/runner-updates")?; |
| 1099 |
match cmd { |
| 1100 |
RunnerUpdatesCmd::Show if json => println!("{}", serde_json::to_string_pretty(&all)?), |
| 1101 |
RunnerUpdatesCmd::Show => { |
| 1102 |
println!( |
| 1103 |
"{} {}", |
| 1104 |
ui::strong("Builds from"), |
| 1105 |
if all.repo.is_empty() { ui::dim("no repository: no runner is offered a build") } else { all.repo.clone() } |
| 1106 |
); |
| 1107 |
for h in &all.holds { |
| 1108 |
println!("{} flow {} held: rolled back on {} ({})", ui::warn("!"), h.version, h.runner, h.why); |
| 1109 |
} |
| 1110 |
for s in &all.settings { |
| 1111 |
let mut said = vec![]; |
| 1112 |
if let Some(u) = s.updates { |
| 1113 |
said.push(format!("updates {}", if u { "on" } else { "off" })); |
| 1114 |
} |
| 1115 |
if let Some(ch) = &s.channel { |
| 1116 |
said.push(format!("channel {ch}")); |
| 1117 |
} |
| 1118 |
if let Some(w) = &s.window { |
| 1119 |
said.push(format!("window {w} UTC")); |
| 1120 |
} |
| 1121 |
if let Some(d) = s.dry_run { |
| 1122 |
said.push(format!("dry run {}", if d { "on" } else { "off" })); |
| 1123 |
} |
| 1124 |
let level = if s.scope == "site" { "site".to_string() } else { format!("{}:{}", s.scope, s.key) }; |
| 1125 |
println!(" {:<18} {}", level, if said.is_empty() { ui::dim("off unless chosen") } else { said.join(", ") }); |
| 1126 |
} |
| 1127 |
for g in &all.groups { |
| 1128 |
let of = if g.owner.is_empty() { "the site's runners".to_string() } else { format!("{}'s runners", g.owner) }; |
| 1ec29691 Andy McCall ↶ |
1129 |
let mut what = g.percent.map(|p| format!("{p}% of {of}")).unwrap_or_else(|| format!("chosen, of {of}")); |
| 1130 |
if let Some(w) = g.wave { |
| 1131 |
what.push_str(&format!(", wave {w}")); |
| 1132 |
} |
| 9bbca80f Andy McCall ↶ |
1133 |
println!( |
| 1134 |
" group {} ({what}): {}", |
| 1135 |
ui::strong(&g.name), |
| 1136 |
if g.members.is_empty() { "none".into() } else { g.members.join(", ") } |
| 1137 |
); |
| 1138 |
} |
| 1139 |
for r in &all.runners { |
| 1140 |
let on = if r.updates { ui::ok(&format!("on, {}", r.channel)) } else { ui::dim("off") }; |
| 1141 |
println!(" {:<16} flow {:<22} {} ({}) {}", r.name, r.version, on, r.from[0], ui::dim(&r.now)); |
| 1142 |
} |
| 1143 |
} |
| 1144 |
RunnerUpdatesCmd::Set { scope, updates, channel, window, dry_run } => { |
| 1145 |
let (scope, key) = update_scope(&scope, &all)?; |
| 1146 |
let mut s = all.settings.iter().find(|x| x.scope == scope && x.key == key).cloned().unwrap_or(hub_api::UpdateSetting { |
| 1147 |
scope: scope.clone(), |
| 1148 |
key: key.clone(), |
| 1149 |
..Default::default() |
| 1150 |
}); |
| 1151 |
if let Some(u) = switch(updates.as_deref())? { |
| 1152 |
s.updates = u; |
| 1153 |
} |
| 1154 |
if let Some(d) = switch(dry_run.as_deref())? { |
| 1155 |
s.dry_run = d; |
| 1156 |
} |
| 1157 |
if let Some(ch) = channel { |
| 1158 |
s.channel = Some(ch).filter(|c| c != "inherit"); |
| 1159 |
} |
| 1160 |
if let Some(w) = window { |
| 1161 |
s.window = Some(w).filter(|w| w != "inherit"); |
| 1162 |
} |
| 1163 |
let out: serde_json::Value = serde_json::from_str(&c.send_text("PUT", "/admin/runner-updates/settings", &s)?)?; |
| 1164 |
println!("{} {}", ui::ok("✔"), out["notice"].as_str().unwrap_or("saved")); |
| 1165 |
} |
| 1166 |
RunnerUpdatesCmd::Lift { version } => { |
| 1167 |
c.send_text("POST", &format!("/admin/runner-updates/holds/{version}/lift"), &serde_json::json!({}))?; |
| 1168 |
println!("{} flow {version} is offered again where the settings say so", ui::ok("✔")); |
| 1169 |
} |
| 166f6e9b Andy McCall |
1170 |
} |
| 9bbca80f Andy McCall ↶ |
1171 |
Ok(0) |
| 166f6e9b Andy McCall |
1172 |
} |
| 1173 |
|
| 1174 |
|
| 1175 |
fn drop_id(s: &str) -> Result<String> { |
| 1176 |
let id = s.trim().trim_end_matches('/').rsplit('/').next().unwrap_or("").to_string(); |
| 1177 |
if id.len() < 16 || !id.bytes().all(|b| b.is_ascii_hexdigit()) { |
| 1178 |
bail!("{s:?} is not a drop's id or address"); |
| 1179 |
} |
| 1180 |
Ok(id.to_ascii_lowercase()) |
| 1181 |
} |
| 1182 |
|
| 1183 |
fn drop_line(d: &hub_api::Drop) -> String { |
| 1184 |
let title = if d.title.is_empty() { d.files.first().map(|f| f.name.clone()).unwrap_or_else(|| d.id.clone()) } else { d.title.clone() }; |
| 1185 |
let files: Vec<&str> = d.files.iter().map(|f| f.name.as_str()).collect(); |
| 1186 |
format!( |
| 1187 |
"{} {} {}{}", |
| 1188 |
ui::dim(&d.id), |
| 1189 |
ui::strong(&title), |
| 1190 |
if d.visibility == "secret" { ui::warn("secret ") } else { String::new() }, |
| 1191 |
ui::dim(&format!("{} · {}", files.join(", "), ui::ago(d.updated_ms))) |
| 1192 |
) |
| 04f9c984 Andy McCall ↶ |
1193 |
} |
| 1194 |
|
| 1195 |
|
| 1196 |
fn drop_star(c: &Client, id: &str, method: &str, json: bool) -> Result<()> { |
| 1197 |
let s: hub_api::DropStars = c.send(method, &format!("/drops/{}/star", drop_id(id)?), &serde_json::json!({}))?; |
| 1198 |
if json { |
| 1199 |
println!("{}", serde_json::to_string(&s)?); |
| 1200 |
} else { |
| 1201 |
println!("{} {} · {} stars", ui::ok("✔"), if s.starred { "starred" } else { "unstarred" }, s.stars); |
| 1202 |
} |
| 1203 |
Ok(()) |
| 166f6e9b Andy McCall |
1204 |
} |
| 1205 |
|
| 1206 |
fn drop_cmd(c: &Client, cmd: DropCmd, json: bool) -> Result<i32> { |
| 1207 |
match cmd { |
| 1208 |
DropCmd::Create { files, secret, description, title } => { |
| 1209 |
let mut out = Vec::with_capacity(files.len()); |
| 1210 |
for f in &files { |
| 1211 |
let name = f.file_name().and_then(|n| n.to_str()).with_context(|| format!("{} has no file name", f.display()))?; |
| 1212 |
let content = |
| 1213 |
std::fs::read_to_string(f).with_context(|| format!("read {} (a drop made here holds text files)", f.display()))?; |
| 1214 |
out.push(hub_api::NewDropFile { name: name.to_string(), content }); |
| 1215 |
} |
| 1216 |
let new = |
| 1217 |
hub_api::NewDrop { title: title.unwrap_or_default(), description: description.unwrap_or_default(), secret, files: out }; |
| 1218 |
let d: hub_api::Drop = c.send("POST", "/drops", &new)?; |
| 1219 |
if json { |
| 1220 |
println!("{}", serde_json::to_string(&d)?); |
| 1221 |
} else { |
| 1222 |
println!("{} made {}drop {} {}", ui::ok("✔"), if secret { "secret " } else { "" }, ui::strong(&d.id), ui::dim(&d.url)); |
| 1223 |
println!(" {}", ui::dim(&format!("oxy clone {}", d.clone_url))); |
| 1224 |
} |
| 1225 |
} |
| 1226 |
DropCmd::List { user } => { |
| 1227 |
let path = match &user { |
| 1228 |
Some(u) => format!("/users/{}/drops", enc(u)), |
| 1229 |
None => "/drops".to_string(), |
| 1230 |
}; |
| 1231 |
if json { |
| 1232 |
println!("{}", c.get_text(&path)?); |
| 1233 |
return Ok(0); |
| 1234 |
} |
| 1235 |
let list: Vec<hub_api::Drop> = c.get(&path)?; |
| 1236 |
if list.is_empty() { |
| 1237 |
println!("{}", ui::dim("No drops.")); |
| 1238 |
} |
| 1239 |
for d in &list { |
| 1240 |
println!("{}", drop_line(d)); |
| 1241 |
} |
| 1242 |
} |
| 1243 |
DropCmd::View { id, file } => { |
| 1244 |
let path = format!("/drops/{}", drop_id(&id)?); |
| 1245 |
if json && file.is_none() { |
| 1246 |
println!("{}", c.get_text(&path)?); |
| 1247 |
return Ok(0); |
| 1248 |
} |
| 1249 |
let d: hub_api::Drop = c.get(&path)?; |
| 1250 |
if let Some(name) = file { |
| 1251 |
let f = d.files.iter().find(|f| f.name == name).with_context(|| format!("the drop has no file {name}"))?; |
| 1252 |
let content = f.content.as_deref().with_context(|| format!("{name} is not shown as text: download {}", f.raw_url))?; |
| 1253 |
print!("{content}"); |
| 1254 |
return Ok(0); |
| 1255 |
} |
| 1256 |
println!("{}", drop_line(&d)); |
| 1257 |
if !d.description.is_empty() { |
| 1258 |
println!("{}", d.description); |
| 1259 |
} |
| 1260 |
println!("{}", ui::kv("url", &d.url)); |
| 1261 |
println!("{}", ui::kv("clone", &format!("oxy clone {}", d.clone_url))); |
| 1262 |
for f in &d.files { |
| 1263 |
println!("\n{} {}", ui::strong(&format!("── {}", f.name)), ui::dim(&format!("({} bytes)", f.size))); |
| 1264 |
match &f.content { |
| 1265 |
Some(text) => { |
| 1266 |
print!("{text}"); |
| 1267 |
if !text.ends_with('\n') && !text.is_empty() { |
| 1268 |
println!(); |
| 1269 |
} |
| 1270 |
} |
| 1271 |
None => println!("{}", ui::dim(&format!("not text, or too large: {}", f.raw_url))), |
| 1272 |
} |
| 1273 |
} |
| 1274 |
} |
| 1275 |
DropCmd::Delete { id } => { |
| 1276 |
let id = drop_id(&id)?; |
| 1277 |
c.send_text("DELETE", &format!("/drops/{id}"), &serde_json::json!({}))?; |
| 1278 |
println!("{} drop {id}", ui::ok("✔ deleted")); |
| 04f9c984 Andy McCall ↶ |
1279 |
} |
| 1280 |
DropCmd::Fork { id } => { |
| 1281 |
let d: hub_api::Drop = c.send("POST", &format!("/drops/{}/fork", drop_id(&id)?), &serde_json::json!({}))?; |
| 1282 |
if json { |
| 1283 |
println!("{}", serde_json::to_string(&d)?); |
| 1284 |
} else { |
| 1285 |
println!("{} forked as {} {}", ui::ok("✔"), ui::strong(&d.id), ui::dim(&d.url)); |
| 1286 |
} |
| 1287 |
} |
| 1288 |
DropCmd::Star { id } => drop_star(c, &id, "PUT", json)?, |
| 1289 |
DropCmd::Unstar { id } => drop_star(c, &id, "DELETE", json)?, |
| 1290 |
DropCmd::Comments { id } => { |
| 1291 |
let path = format!("/drops/{}/comments", drop_id(&id)?); |
| 1292 |
if json { |
| 1293 |
println!("{}", c.get_text(&path)?); |
| 1294 |
return Ok(0); |
| 1295 |
} |
| 1296 |
let list: Vec<hub_api::DropComment> = c.get(&path)?; |
| 1297 |
if list.is_empty() { |
| 1298 |
println!("{}", ui::dim("No comments.")); |
| 1299 |
} |
| 1300 |
for m in &list { |
| 1301 |
println!("{} {}", ui::strong(&m.author), ui::dim(&format!("#{} · {}", m.id, ui::ago(m.created_ms)))); |
| 1302 |
println!("{}\n", m.body); |
| 1303 |
} |
| 166f6e9b Andy McCall |
1304 |
} |
| 04f9c984 Andy McCall ↶ |
1305 |
DropCmd::Comment { id, message } => { |
| 1306 |
let m: hub_api::DropComment = |
| 1307 |
c.send("POST", &format!("/drops/{}/comments", drop_id(&id)?), &hub_api::NewDropComment { body: message })?; |
| 1308 |
if json { |
| 1309 |
println!("{}", serde_json::to_string(&m)?); |
| 1310 |
} else { |
| 1311 |
println!("{} commented (#{})", ui::ok("✔"), m.id); |
| 1312 |
} |
| 1313 |
} |
| 8aa5ef38 Andy McCall ↶ |
1314 |
} |
| 166f6e9b Andy McCall |
1315 |
Ok(0) |
| 8aa5ef38 Andy McCall ↶ |
1316 |
} |
| 1317 |
|
| 1318 |
|
| 1319 |
fn duration_ms(s: &str) -> Result<i64> { |
| 1320 |
let s = s.trim(); |
| 1321 |
let (n, unit) = s.split_at(s.find(|c: char| !c.is_ascii_digit()).unwrap_or(s.len())); |
| 1322 |
let n: i64 = n.parse().with_context(|| format!("{s:?} is not a duration such as 30d, 12h or 90m"))?; |
| 1323 |
let per = match unit { |
| 1324 |
"d" => 86_400_000, |
| 1325 |
"h" => 3_600_000, |
| 1326 |
"m" => 60_000, |
| 1327 |
_ => bail!("{s:?} is not a duration such as 30d, 12h or 90m"), |
| 1328 |
}; |
| 1329 |
if n <= 0 { |
| 1330 |
bail!("a grant must last some time"); |
| 1331 |
} |
| 1332 |
Ok(n * per) |
| 1333 |
} |
| 1334 |
|
| 1335 |
fn agent(c: &Client, cmd: AgentCmd, json: bool) -> Result<i32> { |
| 1336 |
use oxy_core::cert::{Grant, SignedGrant}; |
| 1337 |
use oxy_core::identity::{Identity, Scope}; |
| 1338 |
match cmd { |
| 1339 |
AgentCmd::Grant { name, scope, lasts } => { |
| 1340 |
let home = oxy_core::repo::default_home(); |
| 1341 |
let (me_id, sk) = oxy_core::repo::home_key(&home).map_err(|e| anyhow::anyhow!("{e}"))?.with_context(|| { |
| 1342 |
format!( |
| 1343 |
"{} names no key of yours (author.key) with its secret: the grant is signed with your own key, in your own shell", |
| 1344 |
home.display() |
| 1345 |
) |
| 1346 |
})?; |
| 1347 |
let me: serde_json::Value = c.get("/user")?; |
| 1348 |
let username = me["username"].as_str().context("the hub did not say who you are")?; |
| 1349 |
let hub = Identity::decode(&c.get_root_bytes("/.well-known/oxyhub-key")?).map_err(|e| anyhow::anyhow!("the hub's key: {e}"))?; |
| 1350 |
let scope = Scope::parse(&scope).map_err(|e| anyhow::anyhow!("{e}"))?; |
| 1351 |
let now = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH)?.as_millis() as i64; |
| 1352 |
let grant = Grant { |
| 1353 |
grantor: me_id.hash(), |
| 1354 |
subject: format!("agent:{name}@{username}"), |
| 1355 |
issuer: hub.hash(), |
| 1356 |
scope, |
| 1357 |
not_before: (now - 60_000) as u64, |
| 1358 |
expires: (now + duration_ms(&lasts)?) as u64, |
| 1359 |
}; |
| 1360 |
let signed = SignedGrant::sign(grant, &me_id, &sk); |
| 1361 |
let r: serde_json::Value = |
| 1362 |
c.send("POST", "/agents/grants", &serde_json::json!({ "grant": oxy_core::dsse::base64(&signed.encode()) }))?; |
| 1363 |
if json { |
| 1364 |
println!("{r}"); |
| 1365 |
return Ok(0); |
| 1366 |
} |
| 1367 |
println!( |
| 1368 |
"{} {} to act for you as {} ({}), until {}", |
| 1369 |
ui::ok("✔ granted"), |
| 1370 |
ui::strong(&name), |
| 1371 |
r["subject"].as_str().unwrap_or(""), |
| 1372 |
r["scope"].as_str().unwrap_or(""), |
| 1373 |
ui::date(r["expires_ms"].as_i64().unwrap_or(0)) |
| 1374 |
); |
| 1375 |
println!(" token (shown once): {}", r["token"].as_str().unwrap_or("")); |
| 1376 |
println!( |
| 1377 |
" {}", |
| 1378 |
ui::dim("in the agent's shell: oxy auth grant, paste the token; then oxy config --user author.signer keyless") |
| 1379 |
); |
| 1380 |
println!(" {}", ui::dim(&format!("revoke it with: oh agent revoke {}", r["id"]))); |
| 1381 |
Ok(0) |
| 1382 |
} |
| 1383 |
AgentCmd::List => { |
| 1384 |
let grants: Vec<serde_json::Value> = c.get("/agents/grants")?; |
| 1385 |
if json { |
| 1386 |
println!("{}", serde_json::to_string(&grants)?); |
| 1387 |
return Ok(0); |
| 1388 |
} |
| 1389 |
if grants.is_empty() { |
| 1390 |
println!("{}", ui::dim("no agent grants")); |
| 1391 |
} |
| 1392 |
let now = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH)?.as_millis() as i64; |
| 1393 |
for g in grants { |
| 1394 |
let state = if g["revoked_ms"].is_i64() { |
| 1395 |
ui::bad("revoked") |
| 1396 |
} else if g["expires_ms"].as_i64().unwrap_or(0) <= now { |
| 1397 |
ui::dim("ended") |
| 1398 |
} else { |
| 1399 |
ui::ok("live") |
| 1400 |
}; |
| 1401 |
let used = g["last_used_ms"].as_i64().map(|t| format!("used {}", ui::ago(t))).unwrap_or_else(|| "never used".into()); |
| 1402 |
println!( |
| 1403 |
"#{:<4} {:<8} {} · {} · until {} · {}", |
| 1404 |
g["id"], |
| 1405 |
state, |
| 1406 |
ui::strong(g["subject"].as_str().unwrap_or("")), |
| 1407 |
g["scope"].as_str().unwrap_or(""), |
| 1408 |
ui::date(g["expires_ms"].as_i64().unwrap_or(0)), |
| 1409 |
ui::dim(&used) |
| 1410 |
); |
| 1411 |
} |
| 1412 |
Ok(0) |
| 1413 |
} |
| 1414 |
AgentCmd::Revoke { id } => { |
| 1415 |
c.send_text("DELETE", &format!("/agents/grants/{id}"), &serde_json::json!({}))?; |
| 1416 |
println!("{} grant #{id}: no more certificates under it", ui::ok("✔ revoked")); |
| 1417 |
Ok(0) |
| 1418 |
} |
| 5c6e92fd Andy McCall ↶ |
1419 |
} |
| 1420 |
} |
| 1421 |
|
| 1422 |
fn login(mut cfg: Config, url: &str, token: Option<String>, ca: Option<std::path::PathBuf>) -> Result<i32> { |
| 1423 |
let hub = url.trim_end_matches('/').to_string(); |
| df71c789 Andy McCall ↶ |
1424 |
let token = match token.as_deref() { |
| 1425 |
Some("-") => { |
| 5c6e92fd Andy McCall ↶ |
1426 |
eprintln!("Paste a token from {hub}/settings/tokens, then press Enter:"); |
| 1427 |
let mut line = String::new(); |
| 1428 |
std::io::stdin().read_line(&mut line)?; |
| 1429 |
line.trim().to_string() |
| 1430 |
} |
| df71c789 Andy McCall ↶ |
1431 |
Some(t) => t.to_string(), |
| 1432 |
None => device_sign_in(&hub, ca.clone())?, |
| 5c6e92fd Andy McCall ↶ |
1433 |
}; |
| 1434 |
if token.is_empty() { |
| 1435 |
bail!("no token given"); |
| 1436 |
} |
| 1437 |
let t = Target { hub: hub.clone(), token: Some(token.clone()), ca: ca.clone(), owner: String::new(), name: String::new() }; |
| 1438 |
let me: serde_json::Value = Client::new(&t)?.get("/user").context("checking the token")?; |
| 2bbe88a8 Andy McCall ↶ |
1439 |
let account = me["username"].as_str().unwrap_or("?"); |
| 1440 |
|
| 1441 |
let key = oxy_core::credentials::hub_of(&hub).with_context(|| format!("{hub} is not a hub URL"))?; |
| 1442 |
let ca = ca.map(|c| std::fs::canonicalize(&c).unwrap_or(c)); |
| 1443 |
oxy_core::credentials::Credentials::user().save(&key, &token, Some(account), ca.as_deref()).map_err(|e| anyhow::anyhow!("{e}"))?; |
| 5c6e92fd Andy McCall ↶ |
1444 |
cfg.default_hub = Some(hub.clone()); |
| 1445 |
cfg.save()?; |
| 2bbe88a8 Andy McCall ↶ |
1446 |
println!("{} signed in to {hub} as {}", ui::ok("✔"), ui::strong(account)); |
| 1447 |
println!("{}", ui::dim(" oxy uses this sign-in too; oxy auth status lists it, oxy auth logout ends it")); |
| 5c6e92fd Andy McCall ↶ |
1448 |
Ok(0) |
| 1449 |
} |
| 1450 |
|
| df71c789 Andy McCall ↶ |
1451 |
|
| 1452 |
|
| 1453 |
fn device_sign_in(hub: &str, ca: Option<std::path::PathBuf>) -> Result<String> { |
| 1454 |
let t = Target { hub: hub.to_string(), token: None, ca, owner: String::new(), name: String::new() }; |
| 1455 |
let c = Client::new(&t)?; |
| 1456 |
let host = std::fs::read_to_string("/etc/hostname") |
| 1457 |
.ok() |
| 1458 |
.or_else(|| std::env::var("COMPUTERNAME").ok()) |
| 1459 |
.map(|h| h.trim().to_string()) |
| 1460 |
.filter(|h| !h.is_empty()); |
| 1461 |
let client = match host { |
| 1462 |
Some(h) => format!("oh on {h}"), |
| 1463 |
None => "oh".to_string(), |
| 1464 |
}; |
| 1465 |
let scopes = ["repo:read", "repo:write", "proposals:write", "issues:write", "user:read"]; |
| 1466 |
let (code, body) = c.post_raw("/device/code", &serde_json::json!({ "client": client, "scopes": scopes }))?; |
| 1467 |
let v: serde_json::Value = serde_json::from_str(&body).context("the hub's answer to a device code request")?; |
| 1468 |
if code != 200 { |
| 1469 |
bail!("{hub} cannot sign in a device: {}", v["error_description"].as_str().or(v["detail"].as_str()).unwrap_or(&body)); |
| 1470 |
} |
| 1471 |
let device = v["device_code"].as_str().context("no device code")?.to_string(); |
| 1472 |
let user = v["user_code"].as_str().unwrap_or("?"); |
| 1473 |
let url = v["verification_uri_complete"].as_str().or(v["verification_uri"].as_str()).unwrap_or(hub); |
| 1474 |
let mut interval = v["interval"].as_u64().unwrap_or(5).max(1); |
| 1475 |
let deadline = std::time::Instant::now() + std::time::Duration::from_secs(v["expires_in"].as_u64().unwrap_or(900)); |
| 1476 |
eprintln!("Open {} and approve the code {}", ui::strong(url), ui::strong(user)); |
| 1477 |
eprintln!("{}", ui::dim("Waiting for approval (Ctrl-C to give up)…")); |
| 1478 |
loop { |
| 1479 |
std::thread::sleep(std::time::Duration::from_secs(interval)); |
| 1480 |
if std::time::Instant::now() > deadline { |
| 1481 |
bail!("the code expired before it was approved; run oh auth login again"); |
| 1482 |
} |
| 1483 |
let (code, body) = c.post_raw("/device/token", &serde_json::json!({ "device_code": device }))?; |
| 1484 |
let v: serde_json::Value = serde_json::from_str(&body).unwrap_or_default(); |
| 1485 |
if code == 200 { |
| 1486 |
return v["access_token"].as_str().map(String::from).context("no token in the hub's answer"); |
| 1487 |
} |
| 1488 |
match v["error"].as_str() { |
| 1489 |
Some("authorization_pending") => {} |
| 1490 |
Some("slow_down") => interval += 5, |
| 1491 |
Some("access_denied") => bail!("the sign-in was denied on the web"), |
| 1492 |
Some("expired_token") => bail!("the code expired before it was approved; run oh auth login again"), |
| 1493 |
_ => bail!("signing in: HTTP {code}: {body}"), |
| 1494 |
} |
| 1495 |
} |
| 1496 |
} |
| 1497 |
|
| 5c6e92fd Andy McCall ↶ |
1498 |
fn body_of(body: Option<String>, file: Option<String>) -> Result<String> { |
| 1499 |
match (body, file) { |
| 1500 |
(Some(b), _) => Ok(b), |
| 1501 |
(None, Some(f)) if f == "-" => { |
| 1502 |
let mut s = String::new(); |
| 1503 |
std::io::Read::read_to_string(&mut std::io::stdin(), &mut s)?; |
| 1504 |
Ok(s) |
| 1505 |
} |
| 1506 |
(None, Some(f)) => std::fs::read_to_string(&f).with_context(|| format!("read {f}")), |
| 1507 |
(None, None) => Ok(String::new()), |
| 1508 |
} |
| 1509 |
} |
| 1510 |
|
| 1511 |
fn issue_line(i: &Issue) -> String { |
| 1512 |
let glyph = if i.state == "open" { ui::ok("●") } else { ui::dim("○") }; |
| 1513 |
let labels: Vec<String> = i.labels.iter().map(|l| format!("[{}]", l.name)).collect(); |
| 1514 |
format!( |
| 1515 |
"{glyph} {} {}{}{}", |
| 1516 |
ui::dim(&format!("#{}", i.number)), |
| 1517 |
i.title, |
| 1518 |
if labels.is_empty() { String::new() } else { format!(" {}", ui::dim(&labels.join(" "))) }, |
| 1519 |
ui::dim(&format!(" · {} · {}", i.author, ui::ago(i.updated_ms))) |
| 1520 |
) |
| 1521 |
} |
| 1522 |
|
| 1523 |
fn issue(c: &Client, t: &Target, cmd: IssueCmd, json: bool) -> Result<i32> { |
| 1524 |
let base = t.repo_path()?; |
| 1525 |
match cmd { |
| 1526 |
IssueCmd::List { state } => { |
| 1527 |
let path = format!("{base}/issues?state={}", enc(&state)); |
| 1528 |
if json { |
| 1529 |
println!("{}", c.get_text(&path)?); |
| 1530 |
return Ok(0); |
| 1531 |
} |
| 1532 |
let list: Vec<Issue> = c.get(&path)?; |
| 1533 |
if list.is_empty() { |
| 1534 |
println!("{}", ui::dim(&format!("No {state} issues."))); |
| 1535 |
} |
| 1536 |
for i in &list { |
| 1537 |
println!("{}", issue_line(i)); |
| 1538 |
} |
| 1539 |
} |
| 1540 |
IssueCmd::View { number } => { |
| 1541 |
let path = format!("{base}/issues/{number}"); |
| 1542 |
if json { |
| 1543 |
println!("{}", c.get_text(&path)?); |
| 1544 |
return Ok(0); |
| 1545 |
} |
| 1546 |
let i: Issue = c.get(&path)?; |
| 1547 |
println!("{}", issue_line(&i)); |
| 1548 |
if !i.body.trim().is_empty() { |
| 1549 |
println!("\n{}\n", i.body.trim_end()); |
| 1550 |
} |
| 1551 |
let comments: Vec<hub_api::Comment> = c.get(&format!("{path}/comments"))?; |
| 1552 |
for cm in comments { |
| 1553 |
println!("{} {}\n{}\n", ui::strong(&cm.author), ui::dim(&ui::ago(cm.created_ms)), cm.body.trim_end()); |
| 1554 |
} |
| 1555 |
println!("{}", ui::dim(&i.url)); |
| 1556 |
} |
| 1c52d940 Andy McCall ↶ |
1557 |
IssueCmd::Create { title, body, body_file, label, project, column, attach } => { |
| 1558 |
let body = with_attachments(c, &base, body_of(body, body_file)?, &attach)?; |
| 1559 |
let new = NewIssue { title, body, labels: label, assignees: vec![], milestone: None }; |
| 5c6e92fd Andy McCall ↶ |
1560 |
let i: Issue = c.send("POST", &format!("{base}/issues"), &new)?; |
| 1561 |
if json { |
| 1562 |
println!("{}", serde_json::to_string(&i)?); |
| 1563 |
} else { |
| 1564 |
println!("{} opened #{} {}", ui::ok("✔"), i.number, ui::dim(&i.url)); |
| 17a3a84b Andy McCall ↶ |
1565 |
} |
| 1566 |
if let Some(n) = project { |
| 1567 |
let card = hub_api::NewCard { column, reference: format!("{}#{}", t.name, i.number), note: String::new() }; |
| 1568 |
let added: hub_api::ProjectCard = c.send("POST", &format!("/projects/{}/{n}/cards", t.owner), &card)?; |
| 1569 |
if !json { |
| 1570 |
println!("{} on project {n} as card {}", ui::ok("✔"), added.id); |
| 1571 |
} |
| 5c6e92fd Andy McCall ↶ |
1572 |
} |
| 1573 |
} |
| 306aec18 Andy McCall ↶ |
1574 |
IssueCmd::Close { number, reason, comment } => { |
| 1575 |
comment.post(c, &base, &format!("{base}/issues/{number}/comments"))?; |
| 5c6e92fd Andy McCall ↶ |
1576 |
let i: Issue = c.send("POST", &format!("{base}/issues/{number}"), &IssueStateChange { state: "closed".into(), reason })?; |
| 1577 |
println!("{} closed #{} {}", ui::ok("✔"), i.number, i.title); |
| 1578 |
} |
| 306aec18 Andy McCall ↶ |
1579 |
IssueCmd::Reopen { number, comment } => { |
| 1580 |
comment.post(c, &base, &format!("{base}/issues/{number}/comments"))?; |
| 5c6e92fd Andy McCall ↶ |
1581 |
let i: Issue = |
| 1582 |
c.send("POST", &format!("{base}/issues/{number}"), &IssueStateChange { state: "open".into(), reason: String::new() })?; |
| 1583 |
println!("{} reopened #{} {}", ui::ok("✔"), i.number, i.title); |
| b5229b56 Andy McCall ↶ |
1584 |
} |
| 1585 |
IssueCmd::Edit { number, title, body, body_file, label, no_labels, assignee, no_assignees, milestone } => { |
| 1586 |
let body = match (&body, &body_file) { |
| 1587 |
(None, None) => None, |
| 1588 |
_ => Some(body_of(body, body_file)?), |
| 1589 |
}; |
| 1590 |
let labels = if no_labels { |
| 1591 |
Some(Vec::new()) |
| 1592 |
} else if label.is_empty() { |
| 1593 |
None |
| 1594 |
} else { |
| 1595 |
Some(label) |
| 1596 |
}; |
| 1597 |
let assignees = if no_assignees { |
| 1598 |
Some(Vec::new()) |
| 1599 |
} else if assignee.is_empty() { |
| 1600 |
None |
| 1601 |
} else { |
| 1602 |
Some(assignee) |
| 1603 |
}; |
| 1604 |
if title.is_none() && body.is_none() && labels.is_none() && assignees.is_none() && milestone.is_none() { |
| 1605 |
bail!("say what to change: --title, --body, --body-file, --label, --assignee or --milestone"); |
| 1606 |
} |
| 1607 |
let edit = hub_api::IssueEdit { title, body, labels, assignees, milestone }; |
| 1608 |
let i: Issue = c.send("PATCH", &format!("{base}/issues/{number}"), &edit)?; |
| 1609 |
if json { |
| 1610 |
println!("{}", serde_json::to_string(&i)?); |
| 1611 |
} else { |
| 1612 |
println!("{} edited #{} {}", ui::ok("✔"), i.number, ui::dim(&i.url)); |
| 1613 |
} |
| 2291f8cc Andy McCall ↶ |
1614 |
} |
| 1615 |
IssueCmd::Label(change) => { |
| 1616 |
let path = format!("{base}/issues/{}", change.number); |
| 1617 |
let now: Issue = c.get(&path)?; |
| 1618 |
let labels = change.apply(&now.labels)?; |
| 1619 |
let edit = hub_api::IssueEdit { title: None, body: None, labels: Some(labels.clone()), assignees: None, milestone: None }; |
| 1620 |
let i: Issue = c.send("PATCH", &path, &edit)?; |
| 1621 |
if json { |
| 1622 |
println!("{}", serde_json::to_string(&i)?); |
| 1623 |
} else { |
| 1624 |
println!("{} #{} {}", ui::ok("✔"), i.number, label_names(&labels)); |
| 1625 |
} |
| 5c6e92fd Andy McCall ↶ |
1626 |
} |
| 1c52d940 Andy McCall ↶ |
1627 |
IssueCmd::Comment { number, body, body_file, attach } => { |
| 1628 |
let body = with_attachments(c, &base, body_of(body, body_file)?, &attach)?; |
| bd91edeb Andy McCall ↶ |
1629 |
if body.trim().is_empty() { |
| 1c52d940 Andy McCall ↶ |
1630 |
bail!("give the comment with --body, --body-file or --attach"); |
| bd91edeb Andy McCall ↶ |
1631 |
} |
| 5c6e92fd Andy McCall ↶ |
1632 |
c.send_text("POST", &format!("{base}/issues/{number}/comments"), &NewComment { body, path: None, line: None })?; |
| 1633 |
println!("{} commented on #{number}", ui::ok("✔")); |
| 1634 |
} |
| 1635 |
} |
| 1636 |
Ok(0) |
| 306aec18 Andy McCall ↶ |
1637 |
} |
| 1638 |
|
| 1639 |
|
| 1640 |
|
| 1641 |
#[derive(clap::Args)] |
| 1642 |
struct StateComment { |
| 1643 |
|
| 1644 |
#[arg(long, short)] |
| 1645 |
body: Option<String>, |
| 1646 |
|
| 1647 |
#[arg(long, short = 'F', conflicts_with = "body")] |
| 1648 |
body_file: Option<String>, |
| 1649 |
|
| 1650 |
#[arg(long, short = 'a', value_name = "FILE")] |
| 1651 |
attach: Vec<std::path::PathBuf>, |
| 1652 |
} |
| 1653 |
|
| 1654 |
impl StateComment { |
| 1655 |
fn post(self, c: &Client, base: &str, path: &str) -> Result<()> { |
| 1656 |
if self.body.is_none() && self.body_file.is_none() && self.attach.is_empty() { |
| 1657 |
return Ok(()); |
| 1658 |
} |
| 1659 |
let body = with_attachments(c, base, body_of(self.body, self.body_file)?, &self.attach)?; |
| 1660 |
if !body.trim().is_empty() { |
| 1661 |
c.send_text("POST", path, &NewComment { body, path: None, line: None })?; |
| 1662 |
} |
| 1663 |
Ok(()) |
| 1664 |
} |
| 1665 |
} |
| 1666 |
|
| 1667 |
|
| 1668 |
|
| 1669 |
fn run_or_newest(c: &Client, base: &str, number: Option<i64>, bookmark: Option<&str>) -> Result<i64> { |
| 1670 |
if let Some(n) = number { |
| 1671 |
return Ok(n); |
| 1672 |
} |
| 1673 |
let runs: Vec<FlowRun> = c.get(&format!("{base}/flow/runs?limit=100"))?; |
| 1674 |
runs.iter().filter(|r| bookmark.is_none_or(|b| r.bookmark == b)).map(|r| r.number).max().with_context(|| match bookmark { |
| 1675 |
Some(b) => format!("no runs of {b} among the latest 100"), |
| 1676 |
None => "no runs yet".to_string(), |
| 1677 |
}) |
| 1c52d940 Andy McCall ↶ |
1678 |
} |
| 1679 |
|
| 1680 |
|
| 1681 |
|
| 1682 |
|
| 1683 |
fn with_attachments(c: &Client, base: &str, body: String, files: &[std::path::PathBuf]) -> Result<String> { |
| 1684 |
if files.is_empty() { |
| 1685 |
return Ok(body); |
| 1686 |
} |
| 1687 |
let mut links = Vec::new(); |
| 1688 |
for f in files { |
| 1689 |
let bytes = std::fs::read(f).with_context(|| format!("reading {}", f.display()))?; |
| 1690 |
let name = f.file_name().map(|n| n.to_string_lossy().into_owned()).unwrap_or_else(|| "file".into()); |
| 1691 |
let answer = c.upload(&format!("{base}/attachments?name={}", enc(&name)), &bytes)?; |
| 1692 |
let a: hub_api::Attachment = |
| 1693 |
serde_json::from_str(&answer).with_context(|| format!("attaching {}: unexpected answer", f.display()))?; |
| 1694 |
links.push(a.markdown); |
| 1695 |
} |
| 1696 |
let links = links.join("\n\n"); |
| 1697 |
Ok(if body.trim().is_empty() { links } else { format!("{}\n\n{links}", body.trim_end()) }) |
| 5c6e92fd Andy McCall ↶ |
1698 |
} |
| 1699 |
|
| 1700 |
fn source_name(s: &hub_api::ProposalSource) -> String { |
| 1701 |
match s { |
| 1702 |
hub_api::ProposalSource::Bookmark { bookmark } => bookmark.clone(), |
| 1703 |
hub_api::ProposalSource::Fork { owner, name, bookmark } => format!("{owner}/{name}:{bookmark}"), |
| 1704 |
hub_api::ProposalSource::Changes { hashes } => format!("{} changes", hashes.len()), |
| 1705 |
} |
| 1706 |
} |
| 1707 |
|
| 1708 |
fn proposal_line(p: &Proposal) -> String { |
| 1709 |
let state = serde_json::to_value(p.state).ok().and_then(|v| v.as_str().map(String::from)).unwrap_or_default(); |
| 1710 |
let glyph = match state.as_str() { |
| 1711 |
"open" => ui::ok("●"), |
| 1712 |
"draft" => ui::dim("◌"), |
| 1713 |
"landed" => ui::strong("✔"), |
| 1714 |
_ => ui::dim("○"), |
| 1715 |
}; |
| 1716 |
format!( |
| 1717 |
"{glyph} {} {} {}{}", |
| 1718 |
ui::dim(&format!("#{}", p.number)), |
| 1719 |
p.title, |
| 1720 |
ui::dim(&format!("{} → {}", source_name(&p.source), p.target)), |
| 1721 |
ui::dim(&format!(" · {} · {state}", p.author)) |
| 1722 |
) |
| 1723 |
} |
| 1724 |
|
| 1725 |
fn proposal(c: &Client, t: &Target, cmd: ProposalCmd, json: bool) -> Result<i32> { |
| 1726 |
let base = t.repo_path()?; |
| 1727 |
match cmd { |
| d7e2c275 Andy McCall ↶ |
1728 |
ProposalCmd::List { state, limit } => { |
| 1729 |
let path = format!("{base}/proposals?state={state}&limit={limit}"); |
| 5c6e92fd Andy McCall ↶ |
1730 |
if json { |
| 1731 |
println!("{}", c.get_text(&path)?); |
| 1732 |
return Ok(0); |
| 1733 |
} |
| 1734 |
let list: Vec<Proposal> = c.get(&path)?; |
| 1735 |
if list.is_empty() { |
| 0b20f1ec Andy McCall ↶ |
1736 |
println!("{}", ui::dim(&format!("No {} proposals.", if state == "all" { "" } else { &state }).replace(" ", " "))); |
| 5c6e92fd Andy McCall ↶ |
1737 |
} |
| 1738 |
for p in &list { |
| 1739 |
println!("{}", proposal_line(p)); |
| 1740 |
} |
| 1741 |
} |
| 1742 |
ProposalCmd::View { number } => { |
| 1743 |
let path = format!("{base}/proposals/{number}"); |
| 1744 |
if json { |
| 1745 |
println!("{}", c.get_text(&path)?); |
| 1746 |
return Ok(0); |
| 1747 |
} |
| 1748 |
let p: Proposal = c.get(&path)?; |
| 1749 |
println!("{}", proposal_line(&p)); |
| 1750 |
if !p.body.trim().is_empty() { |
| 1751 |
println!("\n{}\n", p.body.trim_end()); |
| 1752 |
} |
| 1753 |
println!("{}", ui::kv("changes", &p.changes.len().to_string())); |
| 1754 |
for ch in &p.changes { |
| 1755 |
println!(" {} {}", ui::dim(&ch.hash[..12.min(ch.hash.len())]), ch.message.lines().next().unwrap_or("")); |
| 1756 |
} |
| 1757 |
println!("{}", ui::kv("approvals", &p.approvals.iter().map(|a| a.account.clone()).collect::<Vec<_>>().join(", "))); |
| 1758 |
if p.landable { |
| 1759 |
println!("{} can land", ui::ok("✔")); |
| 1760 |
} else { |
| 1761 |
for b in &p.blockers { |
| 1762 |
println!("{} {b}", ui::bad("✘")); |
| 1763 |
} |
| 1764 |
} |
| 1765 |
if p.conflicted { |
| 1766 |
println!("{} landing it would leave conflicts", ui::bad("!")); |
| 1767 |
} |
| 1768 |
println!("{}", ui::dim(&p.url)); |
| 1769 |
} |
| 739a763a Andy McCall ↶ |
1770 |
ProposalCmd::Create { title, body, body_file, from, into, draft } => { |
| 5c6e92fd Andy McCall ↶ |
1771 |
let target = match into { |
| 1772 |
Some(i) => i, |
| 1773 |
None => c.get::<serde_json::Value>(&base)?["default_bookmark"].as_str().unwrap_or("main").to_string(), |
| 1774 |
}; |
| 739a763a Andy McCall ↶ |
1775 |
let new = NewProposal { title, body: body_of(body, body_file)?, target, source: from, draft }; |
| 5c6e92fd Andy McCall ↶ |
1776 |
let p: Proposal = c.send("POST", &format!("{base}/proposals"), &new)?; |
| 1777 |
if json { |
| 1778 |
println!("{}", serde_json::to_string(&p)?); |
| 1779 |
} else { |
| 1780 |
println!("{} proposed #{} {}", ui::ok("✔"), p.number, ui::dim(&p.url)); |
| 1f1eb06a Andy McCall ↶ |
1781 |
} |
| 739a763a Andy McCall ↶ |
1782 |
} |
| 1783 |
ProposalCmd::Edit { number, title, body, body_file } => { |
| 1784 |
let body = match (&body, &body_file) { |
| 1785 |
(None, None) => None, |
| 1786 |
_ => Some(body_of(body, body_file)?), |
| 1787 |
}; |
| 1788 |
if title.is_none() && body.is_none() { |
| 1789 |
bail!("say what to change: --title, --body or --body-file"); |
| 1790 |
} |
| 09ffa7b6 Andy McCall ↶ |
1791 |
let edit = hub_api::ProposalEdit { title, body, ..Default::default() }; |
| 739a763a Andy McCall ↶ |
1792 |
let p: Proposal = c.send("PATCH", &format!("{base}/proposals/{number}"), &edit)?; |
| 1793 |
if json { |
| 1794 |
println!("{}", serde_json::to_string(&p)?); |
| 1795 |
} else { |
| 1796 |
println!("{} edited #{} {}", ui::ok("✔"), p.number, ui::dim(&p.url)); |
| 2291f8cc Andy McCall ↶ |
1797 |
} |
| 1798 |
} |
| 1799 |
ProposalCmd::Label(change) => { |
| 1800 |
let path = format!("{base}/proposals/{}", change.number); |
| 1801 |
let now: Proposal = c.get(&path)?; |
| 1802 |
let labels = change.apply(&now.labels)?; |
| 1803 |
let edit = hub_api::ProposalEdit { labels: Some(labels.clone()), ..Default::default() }; |
| 1804 |
let p: Proposal = c.send("PATCH", &path, &edit)?; |
| 1805 |
if json { |
| 1806 |
println!("{}", serde_json::to_string(&p)?); |
| 1807 |
} else { |
| 1808 |
println!("{} #{} {}", ui::ok("✔"), p.number, label_names(&labels)); |
| 739a763a Andy McCall ↶ |
1809 |
} |
| 1810 |
} |
| 1811 |
ProposalCmd::Comment { number, body, body_file } => { |
| 1812 |
let body = body_of(body, body_file)?; |
| 1813 |
if body.trim().is_empty() { |
| 1814 |
bail!("give the comment with --body or --body-file"); |
| 1815 |
} |
| 1816 |
c.send_text("POST", &format!("{base}/proposals/{number}/comments"), &NewComment { body, path: None, line: None })?; |
| 1817 |
println!("{} commented on #{number}", ui::ok("✔")); |
| 86a69b53 Andy McCall ↶ |
1818 |
} |
| 1819 |
ProposalCmd::Land { number, when_ready: true, off, .. } => { |
| 1820 |
let path = format!("{base}/proposals/{number}/auto-land"); |
| 1821 |
let p: Proposal = |
| 1822 |
if off { c.send("DELETE", &path, &serde_json::json!({}))? } else { c.send("POST", &path, &serde_json::json!({}))? }; |
| 1823 |
if json { |
| 1824 |
println!("{}", serde_json::to_string(&p)?); |
| 1825 |
} else if p.landed.is_some() { |
| 1826 |
println!("{} landed #{} on {}: its checks had passed", ui::ok("✔"), p.number, p.target); |
| 1827 |
} else if p.auto_land.is_some() { |
| 1828 |
println!("{} #{} will land when its checks pass", ui::ok("✔"), p.number); |
| 1829 |
} else { |
| 1830 |
println!("{} #{} will not land by itself", ui::ok("✔"), p.number); |
| 1831 |
} |
| 1f1eb06a Andy McCall ↶ |
1832 |
} |
| 86a69b53 Andy McCall ↶ |
1833 |
ProposalCmd::Land { number, over_checks, .. } => { |
| 2a8b6216 Andy McCall ↶ |
1834 |
let body = match &over_checks { |
| 1835 |
Some(r) => serde_json::json!({ "over_checks": r }), |
| 1836 |
None => serde_json::json!({}), |
| 1837 |
}; |
| 1838 |
let p: Proposal = c.send("POST", &format!("{base}/proposals/{number}/land"), &body)?; |
| 1f1eb06a Andy McCall ↶ |
1839 |
if json { |
| 1840 |
println!("{}", serde_json::to_string(&p)?); |
| 1841 |
} else { |
| 1842 |
println!("{} landed #{} on {}", ui::ok("✔"), p.number, p.target); |
| 71e65fcf Andy McCall ↶ |
1843 |
} |
| 1844 |
} |
| 1845 |
ProposalCmd::Close { number, message } => { |
| 1846 |
if let Some(body) = message { |
| 1847 |
c.send_text("POST", &format!("{base}/proposals/{number}/comments"), &NewComment { body, path: None, line: None })?; |
| 1848 |
} |
| 1849 |
let p: Proposal = c.send("PATCH", &format!("{base}/proposals/{number}"), &serde_json::json!({ "state": "closed" }))?; |
| 1850 |
if json { |
| 1851 |
println!("{}", serde_json::to_string(&p)?); |
| 1852 |
} else { |
| 1853 |
println!("{} closed #{} {}", ui::ok("✔"), p.number, p.title); |
| 1854 |
} |
| 1855 |
} |
| 1856 |
ProposalCmd::Reopen { number } => { |
| 1857 |
let p: Proposal = c.send("PATCH", &format!("{base}/proposals/{number}"), &serde_json::json!({ "state": "open" }))?; |
| 1858 |
if json { |
| 1859 |
println!("{}", serde_json::to_string(&p)?); |
| 1860 |
} else { |
| 1861 |
println!("{} reopened #{} {}", ui::ok("✔"), p.number, p.title); |
| 09ffa7b6 Andy McCall ↶ |
1862 |
} |
| 1863 |
} |
| 1864 |
ProposalCmd::Ready { number, undo } => { |
| 1865 |
let edit = hub_api::ProposalEdit { draft: Some(undo), ..Default::default() }; |
| 1866 |
let p: Proposal = c.send("PATCH", &format!("{base}/proposals/{number}"), &edit)?; |
| 1867 |
if json { |
| 1868 |
println!("{}", serde_json::to_string(&p)?); |
| 1869 |
} else if undo { |
| 1870 |
println!("{} #{} {} is a draft again", ui::ok("✔"), p.number, p.title); |
| 1871 |
} else { |
| 1872 |
println!("{} #{} {} is ready for review", ui::ok("✔"), p.number, p.title); |
| 5c6e92fd Andy McCall ↶ |
1873 |
} |
| 1874 |
} |
| 1875 |
ProposalCmd::Checks { number } => { |
| 1876 |
let path = format!("{base}/proposals/{number}/checks"); |
| 1877 |
if json { |
| 1878 |
println!("{}", c.get_text(&path)?); |
| 1879 |
return Ok(0); |
| 1880 |
} |
| 1881 |
let checks: Vec<hub_api::Check> = c.get(&path)?; |
| 1882 |
if checks.is_empty() { |
| 1883 |
println!("{}", ui::dim("No checks yet.")); |
| 1884 |
} |
| 1885 |
for ch in checks { |
| 1886 |
println!("{} {} {}", ui::status(ch.status.as_str()), ch.name, ui::dim(&ch.summary)); |
| 1887 |
} |
| 1888 |
} |
| 1889 |
} |
| 1890 |
Ok(0) |
| 1891 |
} |
| 1892 |
|
| 1893 |
fn run_line(r: &FlowRun) -> String { |
| 1894 |
let what = |
| 1895 |
if r.bookmark.is_empty() { format!("change {}", r.heads.first().map(|h| &h[..12]).unwrap_or("")) } else { r.bookmark.clone() }; |
| 1896 |
format!( |
| 1897 |
"{} {} {} {}", |
| 1898 |
ui::status(&r.status), |
| 1899 |
ui::dim(&format!("#{}", r.number)), |
| 1900 |
what, |
| 1901 |
ui::dim(&format!("· {} by {} · {}", r.trigger, r.actor, ui::ago(r.created_ms))) |
| 1902 |
) |
| 1903 |
} |
| 1904 |
|
| fac6853c Andy McCall ↶ |
1905 |
fn print_run(r: &FlowRun, timings: bool) { |
| 5c6e92fd Andy McCall ↶ |
1906 |
println!("{}", run_line(r)); |
| 1907 |
for j in &r.jobs { |
| 1908 |
let dur = match (j.started_ms, j.finished_ms) { |
| 1909 |
(Some(a), Some(b)) => format!(" {:.1} s", (b - a) as f64 / 1000.0), |
| 1910 |
_ => String::new(), |
| 1911 |
}; |
| 1912 |
let on = if j.runner.is_empty() { String::new() } else { format!(" on {}", j.runner) }; |
| 1913 |
println!(" {} {}{}", ui::status(&j.status), j.name, ui::dim(&format!("{dur}{on}"))); |
| 501d8b26 Andy McCall ↶ |
1914 |
|
| 1915 |
|
| 67040b15 Andy McCall ↶ |
1916 |
if j.status == "queued" && !j.waiting.is_empty() { |
| 1917 |
println!(" {}", ui::warn(&format!("waiting: {}", j.waiting))); |
| 501d8b26 Andy McCall ↶ |
1918 |
} else if !j.placed.is_empty() { |
| 1919 |
println!(" {}", ui::dim(&format!("placed: {}", j.placed))); |
| 67040b15 Andy McCall ↶ |
1920 |
} |
| fac6853c Andy McCall ↶ |
1921 |
if timings { |
| 1922 |
let t = timing_line(j); |
| 1923 |
if !t.is_empty() { |
| 1924 |
println!(" {}", ui::dim(&t)); |
| 1925 |
} |
| 1926 |
} |
| 5c6e92fd Andy McCall ↶ |
1927 |
} |
| 1928 |
if !r.error.is_empty() { |
| 1929 |
println!("{}", ui::bad(&r.error)); |
| 1930 |
} |
| 1931 |
println!("{}", ui::dim(&r.url)); |
| 1932 |
} |
| 1933 |
|
| fac6853c Andy McCall ↶ |
1934 |
|
| 1935 |
fn took(ms: i64) -> String { |
| 1936 |
match ms { |
| 1937 |
..=999 => format!("{ms} ms"), |
| 1938 |
1000..=59_999 => format!("{:.1} s", ms as f64 / 1000.0), |
| 1939 |
_ => format!("{} min {} s", ms / 60_000, (ms % 60_000) / 1000), |
| 1940 |
} |
| 1941 |
} |
| 1942 |
|
| 1943 |
|
| 1944 |
fn mib(m: i64) -> String { |
| 1945 |
if m >= 1024 { |
| 1946 |
format!("{:.1} GiB", m as f64 / 1024.0) |
| 1947 |
} else { |
| 1948 |
format!("{m} MiB") |
| 1949 |
} |
| 1950 |
} |
| 1951 |
|
| 1952 |
|
| 1953 |
|
| 1954 |
fn timing_line(j: &FlowJob) -> String { |
| 1955 |
let mut parts = vec![]; |
| 1956 |
if let (Some(q), Some(s)) = (j.queued_ms, j.started_ms) { |
| 1957 |
parts.push(format!("waited {}", took((s - q).max(0)))); |
| 1958 |
} |
| 1959 |
parts.extend(j.phases.iter().map(|p| format!("{} {}", p.name, took(p.ms as i64)))); |
| 1960 |
let peaks: Vec<String> = [("disk", j.peak_disk_mib), ("memory", j.peak_memory_mib)] |
| 1961 |
.into_iter() |
| 1962 |
.filter_map(|(what, m)| m.map(|m| format!("{what} {}", mib(m)))) |
| 1963 |
.collect(); |
| 1964 |
if !peaks.is_empty() { |
| 1965 |
parts.push(format!("peak {}", peaks.join(", "))); |
| 1966 |
} |
| 1967 |
parts.join(" · ") |
| 1968 |
} |
| 1969 |
|
| 1970 |
|
| 1971 |
fn enc_path(path: &str) -> String { |
| 1972 |
path.split('/').map(enc).collect::<Vec<_>>().join("/") |
| 1973 |
} |
| 1974 |
|
| 1975 |
|
| 1976 |
|
| 1977 |
|
| 1978 |
fn show_report( |
| 1979 |
c: &Client, |
| 1980 |
at: &str, |
| 1981 |
files: impl Fn(&str) -> String, |
| 1982 |
list: bool, |
| 1983 |
file: Option<String>, |
| 1984 |
output: Option<std::path::PathBuf>, |
| 1985 |
json: bool, |
| 1986 |
) -> Result<()> { |
| 1987 |
use std::io::Write; |
| 1988 |
if let Some(path) = file { |
| 1989 |
let bytes = c.get_bytes(&files(&path))?; |
| 1990 |
match output { |
| 1991 |
Some(out) => { |
| 1992 |
std::fs::write(&out, &bytes).with_context(|| format!("writing {}", out.display()))?; |
| 1993 |
eprintln!("{} {path}: {} bytes to {}", ui::ok("✔"), bytes.len(), out.display()); |
| 1994 |
} |
| 1995 |
None => std::io::stdout().write_all(&bytes)?, |
| 1996 |
} |
| 1997 |
return Ok(()); |
| 1998 |
} |
| 1999 |
if json { |
| 2000 |
println!("{}", c.get_text(at)?); |
| 2001 |
return Ok(()); |
| 2002 |
} |
| 2003 |
if !list { |
| 2004 |
std::io::stdout().write_all(&c.get_bytes(&files("report.toml"))?)?; |
| 2005 |
return Ok(()); |
| 2006 |
} |
| 2007 |
let r: Report = c.get(at)?; |
| 2008 |
print_report(&r); |
| 2009 |
if r.files_expired_ms.is_some() { |
| 2010 |
println!("{}", ui::warn("its files expired under retention; its metrics stay")); |
| 2011 |
} |
| 2012 |
for f in &r.manifest { |
| 2013 |
println!(" {:>10} {}", ui::dim(&f.size.to_string()), f.path); |
| 2014 |
} |
| 2015 |
println!("{}", ui::dim(&r.url)); |
| 2016 |
Ok(()) |
| 2017 |
} |
| 2018 |
|
| 2019 |
|
| 2020 |
fn print_report(r: &Report) { |
| 2021 |
let from = match r.run { |
| 2022 |
Some(n) => format!("run #{n} {} on {}", r.job, r.bookmark), |
| 2023 |
None => format!("upload on {}", if r.bookmark.is_empty() { "a change" } else { &r.bookmark }), |
| 2024 |
}; |
| 2025 |
println!( |
| 2026 |
"{} {} {}", |
| 2027 |
ui::strong(&r.name), |
| 2028 |
ui::dim(&format!("{} · {:?}", r.kind, r.title)), |
| 2029 |
ui::dim(&format!("· {from} · {} files", r.files)) |
| 2030 |
); |
| 2031 |
if !r.summary.is_empty() { |
| 2032 |
println!(" {}", r.summary); |
| 2033 |
} |
| 2034 |
for m in &r.metrics { |
| 2035 |
let unit = if m.unit.is_empty() { String::new() } else { format!(" {}", m.unit) }; |
| 2036 |
let mut notes = vec![]; |
| 2037 |
if !m.better.is_empty() { |
| 2038 |
notes.push(format!("{} is better", m.better)); |
| 2039 |
} |
| 2040 |
if let Some(a) = m.fail_above { |
| 2041 |
notes.push(format!("fails above {a}")); |
| 2042 |
} |
| 2043 |
if let Some(b) = m.fail_below { |
| 2044 |
notes.push(format!("fails below {b}")); |
| 2045 |
} |
| 2046 |
let notes = if notes.is_empty() { String::new() } else { format!(" ({})", notes.join(", ")) }; |
| 2047 |
println!(" {} {}{unit}{}", m.key, m.value, ui::dim(¬es)); |
| 2048 |
} |
| 2049 |
if let Some(ctx) = r.context.as_object().filter(|m| !m.is_empty()) { |
| 2050 |
let line: Vec<String> = |
| 2051 |
ctx.iter().map(|(k, v)| format!("{k}={}", v.as_str().map(String::from).unwrap_or_else(|| v.to_string()))).collect(); |
| 2052 |
println!(" {}", ui::dim(&line.join(" "))); |
| 2053 |
} |
| 2054 |
} |
| 2055 |
|
| 2056 |
|
| 2057 |
fn report(c: &Client, t: &Target, cmd: ReportCmd, json: bool) -> Result<i32> { |
| 2058 |
let base = t.repo_path()?; |
| 2059 |
match cmd { |
| 2060 |
ReportCmd::List { bookmark } => { |
| 2061 |
let path = format!("{base}/reports{}", bookmark.map(|b| format!("?bookmark={}", enc(&b))).unwrap_or_default()); |
| 2062 |
if json { |
| 2063 |
println!("{}", c.get_text(&path)?); |
| 2064 |
return Ok(0); |
| 2065 |
} |
| 2066 |
let list: Vec<Report> = c.get(&path)?; |
| 2067 |
if list.is_empty() { |
| 2068 |
println!("{}", ui::dim("No reports yet: a Flow job publishes one with `output { report \"name\" { path … } }`.")); |
| 2069 |
} |
| 2070 |
for r in &list { |
| 2071 |
print_report(r); |
| 2072 |
} |
| 2073 |
} |
| 2074 |
ReportCmd::Show { report, id, bookmark, list, file, output } => { |
| 2075 |
let at = format!("{base}/reports/{}", enc(&report)); |
| 2076 |
let query = match (id, bookmark) { |
| 2077 |
(Some(id), _) => format!("?id={id}"), |
| 2078 |
(None, Some(b)) => format!("?bookmark={}", enc(&b)), |
| 2079 |
(None, None) => String::new(), |
| 2080 |
}; |
| 2081 |
let files = |path: &str| format!("{at}/files/{}{query}", enc_path(path)); |
| 2082 |
show_report(c, &format!("{at}{query}"), files, list, file, output, json)?; |
| 2083 |
} |
| 2084 |
} |
| 2085 |
Ok(0) |
| 2086 |
} |
| 2087 |
|
| 5c6e92fd Andy McCall ↶ |
2088 |
fn live(status: &str) -> bool { |
| 2089 |
matches!(status, "queued" | "running") |
| dc59d1b5 Andy McCall ↶ |
2090 |
} |
| 2091 |
|
| 2092 |
|
| 2093 |
fn decide_job(c: &Client, base: &str, number: i64, job: &str, verb: &str, json: bool) -> Result<()> { |
| 2094 |
let done: serde_json::Value = c.send("POST", &format!("{base}/flow/runs/{number}/jobs/{job}/{verb}"), &serde_json::json!({}))?; |
| 2095 |
if json { |
| 2096 |
println!("{done}"); |
| 2097 |
} else { |
| 2098 |
println!("{} run #{number}: {}", ui::ok("✔"), done["said"].as_str().unwrap_or("")); |
| 2099 |
} |
| 2100 |
Ok(()) |
| 5c6e92fd Andy McCall ↶ |
2101 |
} |
| 2102 |
|
| 2103 |
fn watch(c: &Client, base: &str, number: i64, json: bool) -> Result<i32> { |
| 2104 |
let path = format!("{base}/flow/runs/{number}"); |
| 2105 |
let mut seen: std::collections::HashMap<String, String> = std::collections::HashMap::new(); |
| 2106 |
loop { |
| 2107 |
let r: FlowRun = c.get(&path)?; |
| 2108 |
if !json { |
| 2109 |
for j in &r.jobs { |
| 67040b15 Andy McCall ↶ |
2110 |
let shown = format!("{}\u{0}{}", j.status, j.waiting); |
| 2111 |
if seen.get(&j.name) != Some(&shown) { |
| 5c6e92fd Andy McCall ↶ |
2112 |
println!("{} {}", ui::status(&j.status), j.name); |
| 67040b15 Andy McCall ↶ |
2113 |
if j.status == "queued" && !j.waiting.is_empty() { |
| 2114 |
println!(" {}", ui::warn(&format!("waiting: {}", j.waiting))); |
| 2115 |
} |
| 2116 |
seen.insert(j.name.clone(), shown); |
| 5c6e92fd Andy McCall ↶ |
2117 |
} |
| 2118 |
} |
| 2119 |
} |
| 2120 |
if !live(&r.status) { |
| 2121 |
if json { |
| 2122 |
println!("{}", serde_json::to_string(&r)?); |
| 2123 |
} else { |
| 2124 |
println!("{} run #{} {}", ui::status(&r.status), r.number, r.status); |
| 2125 |
} |
| 2126 |
return Ok(if r.status == "passed" { 0 } else { 1 }); |
| 2127 |
} |
| 2128 |
std::thread::sleep(Duration::from_secs(3)); |
| 2129 |
} |
| 2130 |
} |
| 2131 |
|
| 2132 |
fn flow(c: &Client, t: &Target, cmd: RunCmd, json: bool) -> Result<i32> { |
| 2133 |
let base = t.repo_path()?; |
| 2134 |
match cmd { |
| 2135 |
RunCmd::List { limit } => { |
| 2136 |
let path = format!("{base}/flow/runs?limit={limit}"); |
| 2137 |
if json { |
| 2138 |
println!("{}", c.get_text(&path)?); |
| 2139 |
return Ok(0); |
| 2140 |
} |
| 2141 |
let runs: Vec<FlowRun> = c.get(&path)?; |
| 2142 |
if runs.is_empty() { |
| 2143 |
println!("{}", ui::dim("No runs yet.")); |
| 2144 |
} |
| 2145 |
for r in &runs { |
| 2146 |
println!("{}", run_line(r)); |
| 2147 |
} |
| 2148 |
} |
| 306aec18 Andy McCall ↶ |
2149 |
RunCmd::View { number, bookmark, timings } => { |
| 2150 |
let number = run_or_newest(c, &base, number, bookmark.as_deref())?; |
| 5c6e92fd Andy McCall ↶ |
2151 |
let path = format!("{base}/flow/runs/{number}"); |
| 2152 |
if json { |
| 2153 |
println!("{}", c.get_text(&path)?); |
| 2154 |
return Ok(0); |
| 2155 |
} |
| fac6853c Andy McCall ↶ |
2156 |
print_run(&c.get(&path)?, timings); |
| 5c6e92fd Andy McCall ↶ |
2157 |
} |
| 0650d94e Andy McCall ↶ |
2158 |
RunCmd::Start { bookmark, change, jobs, watch: follow } => { |
| 2159 |
let mut body = match (bookmark, change) { |
| 5c6e92fd Andy McCall ↶ |
2160 |
(_, Some(ch)) => serde_json::json!({ "change": ch }), |
| 2161 |
(Some(b), None) => serde_json::json!({ "bookmark": b }), |
| 2162 |
(None, None) => serde_json::json!({}), |
| 2163 |
}; |
| 0650d94e Andy McCall ↶ |
2164 |
if !jobs.is_empty() { |
| 2165 |
body["jobs"] = serde_json::json!(jobs); |
| 2166 |
} |
| 5c6e92fd Andy McCall ↶ |
2167 |
let started: serde_json::Value = c.send("POST", &format!("{base}/flow/runs"), &body)?; |
| 2168 |
let n = started["number"].as_i64().context("the hub named no run")?; |
| 2169 |
if !json || !follow { |
| 2170 |
if json { |
| 2171 |
println!("{started}"); |
| 2172 |
} else { |
| 2173 |
println!("{} started run #{n} {}", ui::ok("✔"), ui::dim(started["url"].as_str().unwrap_or(""))); |
| 2174 |
} |
| 2175 |
} |
| 2176 |
if follow { |
| 2177 |
return watch(c, &base, n, json); |
| 2178 |
} |
| 2179 |
} |
| 306aec18 Andy McCall ↶ |
2180 |
RunCmd::Watch { number, bookmark } => { |
| 2181 |
let number = run_or_newest(c, &base, number, bookmark.as_deref())?; |
| 2182 |
return watch(c, &base, number, json); |
| 2183 |
} |
| 5c6e92fd Andy McCall ↶ |
2184 |
RunCmd::Log { number, job } => { |
| 2185 |
let mut after = 0; |
| 2186 |
loop { |
| 2187 |
let path = format!("{base}/flow/runs/{number}/jobs/{}/log?after={after}&limit=5000", enc(&job)); |
| 2188 |
let lines: Vec<FlowLogLine> = c.get(&path)?; |
| 2189 |
if lines.is_empty() { |
| 2190 |
break; |
| 2191 |
} |
| 2192 |
for l in &lines { |
| 2193 |
if json { |
| 2194 |
println!("{}", serde_json::to_string(l)?); |
| 2195 |
} else if l.stream == "flow" { |
| 2196 |
println!("{}", ui::dim(&l.text)); |
| 2197 |
} else { |
| 2198 |
println!("{}", l.text); |
| 2199 |
} |
| 2200 |
} |
| 2201 |
after = lines.last().map(|l| l.seq).unwrap_or(after); |
| e8fa5369 Andy McCall ↶ |
2202 |
} |
| fac6853c Andy McCall ↶ |
2203 |
} |
| 2204 |
RunCmd::Report { number, job, pick } => { |
| 2205 |
let at = format!("{base}/flow/runs/{number}/jobs/{}/report", enc(&job)); |
| 2206 |
let query = pick.name.as_deref().map(|n| format!("?name={}", enc(n))).unwrap_or_default(); |
| 2207 |
let files = |path: &str| format!("{at}/{}{query}", enc_path(path)); |
| 2208 |
show_report(c, &format!("{at}{query}"), files, pick.list, pick.file, pick.output, json)?; |
| e8fa5369 Andy McCall ↶ |
2209 |
} |
| 2210 |
RunCmd::Rerun { number, job: Some(job) } => { |
| 2211 |
let queued: serde_json::Value = |
| 2212 |
c.send("POST", &format!("{base}/flow/runs/{number}/jobs/{job}/rerun"), &serde_json::json!({}))?; |
| 2213 |
if json { |
| 2214 |
println!("{queued}"); |
| 2215 |
} else { |
| 2216 |
let n = queued["queued"].as_i64().unwrap_or(1); |
| 2217 |
let with = if n > 1 { format!(" with the {} that depend on it", n - 1) } else { String::new() }; |
| 2218 |
println!("{} queued {job} of run #{number} again{with}", ui::ok("✔")); |
| 5c6e92fd Andy McCall ↶ |
2219 |
} |
| 2220 |
} |
| e8fa5369 Andy McCall ↶ |
2221 |
RunCmd::Rerun { number, job: None } => { |
| 5c6e92fd Andy McCall ↶ |
2222 |
let started: serde_json::Value = c.send("POST", &format!("{base}/flow/runs/{number}/rerun"), &serde_json::json!({}))?; |
| 2223 |
if json { |
| 2224 |
println!("{started}"); |
| 2225 |
} else { |
| 2226 |
println!("{} started run #{} {}", ui::ok("✔"), started["number"], ui::dim(started["url"].as_str().unwrap_or(""))); |
| 2291f8cc Andy McCall ↶ |
2227 |
} |
| 2228 |
} |
| dc59d1b5 Andy McCall ↶ |
2229 |
RunCmd::Approve { number, job } => decide_job(c, &base, number, &job, "approve", json)?, |
| 2230 |
RunCmd::Reject { number, job } => decide_job(c, &base, number, &job, "reject", json)?, |
| 745d225d Andy McCall ↶ |
2231 |
RunCmd::Cancel { number } => { |
| 2232 |
let done: serde_json::Value = c.send("POST", &format!("{base}/flow/runs/{number}/cancel"), &serde_json::json!({}))?; |
| 2233 |
if json { |
| 2234 |
println!("{done}"); |
| 2235 |
} else { |
| 2236 |
println!("{} cancelled run #{number}", ui::ok("✔")); |
| 2237 |
} |
| 2238 |
} |
| 2291f8cc Andy McCall ↶ |
2239 |
} |
| 2240 |
Ok(0) |
| 2241 |
} |
| 2242 |
|
| 2243 |
fn label(c: &Client, t: &Target, cmd: LabelCmd, json: bool) -> Result<i32> { |
| 2244 |
let base = t.repo_path()?; |
| 2245 |
match cmd { |
| 2246 |
LabelCmd::List => { |
| 2247 |
let path = format!("{base}/labels"); |
| 2248 |
if json { |
| 2249 |
println!("{}", c.get_text(&path)?); |
| 2250 |
return Ok(0); |
| 2251 |
} |
| 2252 |
let labels: Vec<hub_api::Label> = c.get(&path)?; |
| 2253 |
if labels.is_empty() { |
| 2254 |
println!("{}", ui::dim("No labels.")); |
| 5c6e92fd Andy McCall ↶ |
2255 |
} |
| 2291f8cc Andy McCall ↶ |
2256 |
for l in labels { |
| 2257 |
println!("{} {} {}", ui::dim(&l.colour), l.name, ui::dim(&l.description)); |
| 2258 |
} |
| 2259 |
} |
| 2260 |
LabelCmd::Create { name, colour, description } => { |
| 2261 |
let l: hub_api::Label = c.send("POST", &format!("{base}/labels"), &hub_api::NewLabel { name, colour, description })?; |
| 2262 |
if json { |
| 2263 |
println!("{}", serde_json::to_string(&l)?); |
| 2264 |
} else { |
| 2265 |
println!("{} label {} {}", ui::ok("✔"), l.name, ui::dim(&l.colour)); |
| 2266 |
} |
| 2267 |
} |
| 2268 |
LabelCmd::Delete { name } => { |
| 2269 |
c.send_text("DELETE", &format!("{base}/labels/{}", enc(&name)), &serde_json::json!({}))?; |
| 2270 |
println!("{} deleted label {name}", ui::ok("✔")); |
| 62ceb2ee Andy McCall ↶ |
2271 |
} |
| 2272 |
} |
| 2273 |
Ok(0) |
| 2274 |
} |
| 2275 |
|
| 2276 |
fn assistant(c: &Client, t: &Target, cmd: AssistantCmd, org: Option<String>, site: bool, json: bool) -> Result<i32> { |
| 2277 |
let base = match (&org, site) { |
| 2278 |
(Some(o), _) => format!("/orgs/{}/assistants", enc(o)), |
| 2279 |
(None, true) => "/admin/assistants".to_string(), |
| 2280 |
(None, false) => format!("{}/assistants", t.repo_path()?), |
| 2281 |
}; |
| 2282 |
match cmd { |
| 2283 |
AssistantCmd::List => { |
| 2284 |
if json { |
| 2285 |
println!("{}", c.get_text(&base)?); |
| 2286 |
return Ok(0); |
| 2287 |
} |
| 2288 |
let list: Vec<hub_api::Assistant> = c.get(&base)?; |
| 2289 |
if list.is_empty() { |
| 2290 |
println!("{}", ui::dim("No assistants.")); |
| 2291 |
} |
| 2292 |
for a in list { |
| 2293 |
let state = if a.enabled && !a.off_above { |
| 2294 |
ui::ok("on") |
| 2295 |
} else if a.off_above { |
| 2296 |
ui::dim("off above") |
| 2297 |
} else { |
| 2298 |
ui::dim("off") |
| 2299 |
}; |
| 2300 |
let from = if a.from.is_empty() { String::new() } else { ui::dim(&format!(" · from {}", a.from)) }; |
| 2301 |
println!("{} {} {} {}{}", state, a.name, ui::dim(&a.model), ui::dim(&a.endpoint), from); |
| 2302 |
} |
| 2303 |
} |
| 2304 |
AssistantCmd::Add { name, preset, endpoint, api, model, token_env, off } => { |
| 2305 |
let token = match token_env { |
| 2306 |
Some(var) => std::env::var(&var).map_err(|_| anyhow::anyhow!("${var} is not set"))?, |
| 2307 |
None => String::new(), |
| 2308 |
}; |
| 2309 |
let body = |
| 2310 |
hub_api::NewAssistant { name, preset, endpoint, api, model, token, headers: String::new(), enabled: !off, budget: None }; |
| 2311 |
let r: serde_json::Value = c.send("POST", &base, &body)?; |
| 2312 |
println!("{} {}", ui::ok("✔"), r["ok"].as_str().unwrap_or("saved")); |
| 2313 |
} |
| 2314 |
AssistantCmd::Remove { name } => { |
| 2315 |
c.send_text("DELETE", &format!("{base}/{}", enc(&name)), &serde_json::json!({}))?; |
| 2316 |
println!("{} removed {name}", ui::ok("✔")); |
| 2317 |
} |
| 2318 |
AssistantCmd::Prompt { set } => { |
| 2319 |
let p: hub_api::AssistantPrompt = match set { |
| 2320 |
Some(path) => { |
| 2321 |
let text = if path.as_os_str() == "-" { |
| 2322 |
let mut s = String::new(); |
| 2323 |
std::io::Read::read_to_string(&mut std::io::stdin(), &mut s)?; |
| 2324 |
s |
| 2325 |
} else { |
| 2326 |
std::fs::read_to_string(&path)? |
| 2327 |
}; |
| 2328 |
c.send("PUT", &format!("{base}/prompt"), &hub_api::AssistantPrompt { prompt: text, inherited: vec![] })? |
| 2329 |
} |
| 2330 |
None => c.get(&format!("{base}/prompt"))?, |
| 2331 |
}; |
| 2332 |
if json { |
| 2333 |
println!("{}", serde_json::to_string(&p)?); |
| 2334 |
return Ok(0); |
| 2335 |
} |
| 2336 |
for (from, text) in &p.inherited { |
| 2337 |
println!("{}\n{}\n", ui::dim(&format!("── from {from}")), text); |
| 2338 |
} |
| 2339 |
if p.prompt.is_empty() { |
| 2340 |
println!("{}", ui::dim("No prompt of its own.")); |
| 2341 |
} else { |
| 2342 |
println!("{}\n{}", ui::dim("── this level"), p.prompt); |
| 2343 |
} |
| 17a3a84b Andy McCall ↶ |
2344 |
} |
| 2345 |
} |
| 2346 |
Ok(0) |
| 2347 |
} |
| 2348 |
|
| 2349 |
fn project_line(p: &hub_api::Project) -> String { |
| 2350 |
let glyph = if p.closed { ui::dim("▢") } else { ui::ok("▣") }; |
| 2351 |
let private = if p.public { "" } else { " · private" }; |
| 2352 |
format!("{glyph} {} {} {}", ui::dim(&p.number.to_string()), p.title, ui::dim(&format!("updated {}{private}", ui::ago(p.updated_ms)))) |
| 2353 |
} |
| 2354 |
|
| 2355 |
fn project(c: &Client, t: &Target, cmd: ProjectCmd, owner: Option<String>, json: bool) -> Result<i32> { |
| 2356 |
let owner = match owner { |
| 2357 |
Some(o) => o, |
| 2358 |
None => t.repo()?.0.to_string(), |
| 2359 |
}; |
| 2360 |
let base = format!("/projects/{}", enc(&owner)); |
| 2361 |
let state = |number: i64, closed: bool, verb: &str| -> Result<()> { |
| 2362 |
let p: hub_api::Project = |
| 2363 |
c.send("PATCH", &format!("{base}/{number}"), &hub_api::ProjectEdit { closed: Some(closed), ..Default::default() })?; |
| 2364 |
if json { |
| 2365 |
println!("{}", serde_json::to_string(&p)?); |
| 2366 |
} else { |
| 2367 |
println!("{} {verb} project {} {}", ui::ok("✔"), p.number, p.title); |
| 2368 |
} |
| 2369 |
Ok(()) |
| 2370 |
}; |
| 2371 |
match cmd { |
| 2372 |
ProjectCmd::List => { |
| 2373 |
if json { |
| 2374 |
println!("{}", c.get_text(&base)?); |
| 2375 |
return Ok(0); |
| 2376 |
} |
| 2377 |
let list: Vec<hub_api::Project> = c.get(&base)?; |
| 2378 |
if list.is_empty() { |
| 2379 |
println!("{}", ui::dim(&format!("No projects for {owner}: oh project create TITLE"))); |
| 2380 |
} |
| 2381 |
for p in &list { |
| 2382 |
println!("{}", project_line(p)); |
| 2383 |
} |
| 2384 |
} |
| 439ecd87 Andy McCall ↶ |
2385 |
ProjectCmd::View { number, archived } => { |
| 17a3a84b Andy McCall ↶ |
2386 |
let path = format!("{base}/{number}"); |
| 439ecd87 Andy McCall ↶ |
2387 |
let cards = format!("{path}/cards?archived=true"); |
| 17a3a84b Andy McCall ↶ |
2388 |
if json { |
| 439ecd87 Andy McCall ↶ |
2389 |
println!("{}", c.get_text(if archived { &cards } else { &path })?); |
| 17a3a84b Andy McCall ↶ |
2390 |
return Ok(0); |
| 2391 |
} |
| 439ecd87 Andy McCall ↶ |
2392 |
let mut b: hub_api::Board = c.get(&path)?; |
| 2393 |
if archived { |
| 2394 |
b.columns = c.get(&cards)?; |
| 2395 |
} |
| 17a3a84b Andy McCall ↶ |
2396 |
println!("{}", project_line(&b.project)); |
| 2397 |
if !b.project.description.is_empty() { |
| 2398 |
println!("{}", b.project.description); |
| 2399 |
} |
| 2400 |
for col in &b.columns { |
| 2401 |
println!(); |
| 2402 |
println!("{} {}", ui::strong(&col.name), ui::dim(&format!("{} · column {}", col.cards.len(), col.id))); |
| 2403 |
if col.cards.is_empty() { |
| 2404 |
println!(" {}", ui::dim("(empty)")); |
| 2405 |
} |
| 2406 |
for card in &col.cards { |
| 2407 |
let what = if card.reference.is_empty() { ui::dim("note") } else { card.reference.clone() }; |
| 2408 |
let st = if card.state.is_empty() { String::new() } else { format!(" {}", ui::dim(&card.state)) }; |
| 6a306cd0 Andy McCall ↶ |
2409 |
let mut tags: Vec<String> = card.labels.iter().map(|l| format!("[{l}]")).collect(); |
| 2410 |
tags.extend(card.assignees.iter().map(|a| format!("@{a}"))); |
| 2411 |
let tags = if tags.is_empty() { String::new() } else { format!(" {}", ui::dim(&tags.join(" "))) }; |
| 2412 |
println!(" {} {what}{st} {}{tags}", ui::dim(&format!("{:>4}", card.id)), card.title); |
| 17a3a84b Andy McCall ↶ |
2413 |
} |
| 2414 |
} |
| 2415 |
println!("{}", ui::dim(&format!("\n{}", b.project.url))); |
| 2416 |
} |
| 2417 |
ProjectCmd::Create { title, description, private } => { |
| 2418 |
let p: hub_api::Project = c.send("POST", &base, &hub_api::NewProject { title, description, private })?; |
| 2419 |
if json { |
| 2420 |
println!("{}", serde_json::to_string(&p)?); |
| 2421 |
} else { |
| 2422 |
println!("{} project {} {} {}", ui::ok("✔"), p.number, p.title, ui::dim(&p.url)); |
| 306aec18 Andy McCall ↶ |
2423 |
} |
| 2424 |
} |
| 2425 |
ProjectCmd::Column { cmd } => { |
| 2426 |
let col_path = |number: i64, column: &str| format!("{base}/{number}/columns/{}", enc(column)); |
| 2427 |
let (number, b, what): (i64, hub_api::Board, String) = match cmd { |
| 2428 |
ColumnCmd::Add { number, name } => { |
| 2429 |
let b = c.send("POST", &format!("{base}/{number}/columns"), &hub_api::NewColumn { name: name.clone() })?; |
| 2430 |
(number, b, format!("added column {name}")) |
| 2431 |
} |
| 2432 |
ColumnCmd::Rename { number, column, to } => { |
| 2433 |
let edit = hub_api::ColumnEdit { name: Some(to.clone()), position: None }; |
| 2434 |
(number, c.send("PATCH", &col_path(number, &column), &edit)?, format!("renamed column {column} to {to}")) |
| 2435 |
} |
| 2436 |
ColumnCmd::Move { number, column, to, left, right } => { |
| 2437 |
let position = match to { |
| 2438 |
Some(n) => n - 1, |
| 2439 |
None => { |
| 2440 |
let now: hub_api::Board = c.get(&format!("{base}/{number}"))?; |
| 2441 |
let at = now |
| 2442 |
.columns |
| 2443 |
.iter() |
| 2444 |
.position(|col| col.id.to_string() == column || col.name.eq_ignore_ascii_case(&column)) |
| 2445 |
.with_context(|| format!("no column {column} on board {number}"))? |
| 2446 |
as i64; |
| 2447 |
if left { |
| 2448 |
(at - 1).max(0) |
| 2449 |
} else { |
| 2450 |
debug_assert!(right); |
| 2451 |
at + 1 |
| 2452 |
} |
| 2453 |
} |
| 2454 |
}; |
| 2455 |
let edit = hub_api::ColumnEdit { name: None, position: Some(position) }; |
| 2456 |
(number, c.send("PATCH", &col_path(number, &column), &edit)?, format!("moved column {column}")) |
| 2457 |
} |
| 2458 |
ColumnCmd::Remove { number, column } => { |
| 2459 |
(number, c.send("DELETE", &col_path(number, &column), &serde_json::json!({}))?, format!("removed column {column}")) |
| 2460 |
} |
| 2461 |
}; |
| 2462 |
if json { |
| 2463 |
println!("{}", serde_json::to_string(&b)?); |
| 2464 |
} else { |
| 2465 |
let order: Vec<&str> = b.columns.iter().map(|col| col.name.as_str()).collect(); |
| 2466 |
println!("{} {what} on board {number}: {}", ui::ok("✔"), ui::dim(&order.join(" · "))); |
| 17a3a84b Andy McCall ↶ |
2467 |
} |
| 2468 |
} |
| 2469 |
ProjectCmd::Close { number } => state(number, true, "closed")?, |
| 2470 |
ProjectCmd::Reopen { number } => state(number, false, "reopened")?, |
| b1e92a62 Andy McCall ↶ |
2471 |
ProjectCmd::Transfer { number, to } => { |
| 2472 |
let p: hub_api::Project = |
| 2473 |
c.send("PATCH", &format!("{base}/{number}"), &hub_api::ProjectEdit { owner: Some(to.clone()), ..Default::default() })?; |
| 2474 |
if json { |
| 2475 |
println!("{}", serde_json::to_string(&p)?); |
| 2476 |
} else { |
| 2477 |
println!("{} transferred project {} to {to}: now {} {}", ui::ok("✔"), number, p.number, p.url); |
| 2478 |
} |
| 2479 |
} |
| 17a3a84b Andy McCall ↶ |
2480 |
ProjectCmd::Card { cmd: CardCmd::Add { number, reference, note, column } } => { |
| 2481 |
|
| 2482 |
|
| 2483 |
let reference = match reference { |
| 2484 |
None => String::new(), |
| 2485 |
Some(r) if r.contains('#') && !r.starts_with('#') => match r.split_once('/') { |
| 2486 |
Some((o, rest)) if o == owner => rest.to_string(), |
| 2487 |
Some(_) => bail!("{r}: a project holds only {owner}'s issues and proposals"), |
| 2488 |
None => r, |
| 2489 |
}, |
| 2490 |
Some(r) => { |
| 2491 |
let n = r.trim_start_matches('#'); |
| 2492 |
if n.parse::<i64>().is_err() { |
| 2493 |
bail!("{r}: give an issue or proposal number, or NAME#N"); |
| 2494 |
} |
| 2495 |
format!("{}#{n}", t.repo()?.1) |
| 2496 |
} |
| 2497 |
}; |
| 2498 |
let note = note.unwrap_or_default(); |
| 2499 |
if reference.is_empty() && note.is_empty() { |
| 2500 |
bail!("say what to add: an issue or proposal (12, #12, owner/name#12) or --note TEXT"); |
| 2501 |
} |
| 2502 |
let card: hub_api::ProjectCard = |
| 2503 |
c.send("POST", &format!("{base}/{number}/cards"), &hub_api::NewCard { column, reference, note })?; |
| 2504 |
if json { |
| 2505 |
println!("{}", serde_json::to_string(&card)?); |
| 2506 |
} else { |
| 2507 |
println!("{} card {} {}", ui::ok("✔"), card.id, if card.reference.is_empty() { &card.title } else { &card.reference }); |
| 2508 |
} |
| 2509 |
} |
| 456f4197 Andy McCall ↶ |
2510 |
ProjectCmd::Card { cmd: CardCmd::Move { number, card, column, from, except, state, dry_run } } => match (card, from) { |
| 2511 |
(Some(card), None) => { |
| 2512 |
c.send_text("PATCH", &format!("{base}/{number}/cards/{card}"), &hub_api::CardMove { column: column.clone() })?; |
| 2513 |
println!("{} card {card} moved to {column}", ui::ok("✔")); |
| 2514 |
} |
| 2515 |
(None, Some(from)) => { |
| 2516 |
let r: hub_api::CardsMoved = c.send( |
| 2517 |
"POST", |
| 2518 |
&format!("{base}/{number}/cards/move"), |
| 2519 |
&hub_api::CardsMove { from: from.clone(), column: column.clone(), except, state, dry_run }, |
| 2520 |
)?; |
| 2521 |
if json { |
| 2522 |
println!("{}", serde_json::to_string(&r)?); |
| 2523 |
return Ok(0); |
| 2524 |
} |
| 2525 |
let name = |c: &hub_api::ProjectCard| if c.reference.is_empty() { c.title.clone() } else { c.reference.clone() }; |
| 2526 |
for m in &r.moved { |
| 2527 |
println!(" {} {} {}", if dry_run { "would move" } else { "moved" }, name(m), ui::dim(&m.title)); |
| 2528 |
} |
| 2529 |
let verb = if dry_run { "would move" } else { "moved" }; |
| 2530 |
println!("{} {verb} {} card(s) from {from} to {column}; {} left in {from}", ui::ok("✔"), r.moved.len(), r.kept.len()); |
| 2531 |
} |
| 2532 |
_ => bail!("name a card, or --from COLUMN to move every card of a column"), |
| 2533 |
}, |
| 439ecd87 Andy McCall ↶ |
2534 |
ProjectCmd::Card { cmd: CardCmd::Archive { number, from, older, except, state, dry_run } } => { |
| 2535 |
let older_days = match older.as_deref().map(|o| o.trim().trim_end_matches('d')) { |
| 2536 |
None => None, |
| 2537 |
Some(d) => Some(d.parse::<i64>().map_err(|_| anyhow::anyhow!("--older takes days: 14d, or 14"))?), |
| 2538 |
}; |
| 2539 |
let r: hub_api::CardsArchived = c.send( |
| 2540 |
"POST", |
| 2541 |
&format!("{base}/{number}/cards/archive"), |
| 2542 |
&hub_api::CardsArchive { from: from.clone(), older_days, except, state, dry_run }, |
| 2543 |
)?; |
| 2544 |
if json { |
| 2545 |
println!("{}", serde_json::to_string(&r)?); |
| 2546 |
return Ok(0); |
| 2547 |
} |
| 2548 |
let verb = if dry_run { "would archive" } else { "archived" }; |
| 2549 |
for a in &r.archived { |
| 2550 |
let name = if a.reference.is_empty() { a.title.clone() } else { a.reference.clone() }; |
| 2551 |
println!(" {verb} {name} {}", ui::dim(&a.title)); |
| 2552 |
} |
| 2553 |
println!("{} {verb} {} card(s) from {from}; {} left on the board", ui::ok("✔"), r.archived.len(), r.kept.len()); |
| 2554 |
} |
| 2555 |
ProjectCmd::Card { cmd: CardCmd::Restore { number, card } } => { |
| 2556 |
c.send_text("POST", &format!("{base}/{number}/cards/{card}/restore"), &serde_json::json!({}))?; |
| 2557 |
println!("{} card {card} restored", ui::ok("✔")); |
| 2558 |
} |
| 17a3a84b Andy McCall ↶ |
2559 |
ProjectCmd::Card { cmd: CardCmd::Remove { number, card } } => { |
| 2560 |
c.send_text("DELETE", &format!("{base}/{number}/cards/{card}"), &serde_json::json!({}))?; |
| 2561 |
println!("{} card {card} removed", ui::ok("✔")); |
| 5ea4872f Andy McCall ↶ |
2562 |
} |
| 2563 |
} |
| 2564 |
Ok(0) |
| 2565 |
} |
| 2566 |
|
| 2567 |
fn milestone_line(m: &hub_api::Milestone) -> String { |
| 2568 |
let glyph = if m.state == "open" { ui::ok("◆") } else { ui::dim("◇") }; |
| 2569 |
let due = m.due_ms.map(|d| format!(" · due {}", ui::ago(d))).unwrap_or_default(); |
| 2570 |
format!("{glyph} {} {}{}", m.title, ui::dim(&format!("{} open, {} closed", m.open, m.closed)), ui::dim(&due)) |
| 2571 |
} |
| 2572 |
|
| 2573 |
fn milestone(c: &Client, t: &Target, cmd: MilestoneCmd, json: bool) -> Result<i32> { |
| 2574 |
let base = t.repo_path()?; |
| 2575 |
let edit = |title: &str, e: hub_api::MilestoneEdit, verb: &str| -> Result<()> { |
| 2576 |
let m: hub_api::Milestone = c.send("PATCH", &format!("{base}/milestones/{}", enc(title)), &e)?; |
| 2577 |
if json { |
| 2578 |
println!("{}", serde_json::to_string(&m)?); |
| 2579 |
} else { |
| 2580 |
println!("{} {verb} milestone {}", ui::ok("✔"), m.title); |
| 2581 |
} |
| 2582 |
Ok(()) |
| 2583 |
}; |
| 2584 |
match cmd { |
| 2585 |
MilestoneCmd::List => { |
| 2586 |
let path = format!("{base}/milestones"); |
| 2587 |
if json { |
| 2588 |
println!("{}", c.get_text(&path)?); |
| 2589 |
return Ok(0); |
| 2590 |
} |
| 2591 |
let list: Vec<hub_api::Milestone> = c.get(&path)?; |
| 2592 |
if list.is_empty() { |
| 2593 |
println!("{}", ui::dim("No milestones.")); |
| 2594 |
} |
| 2595 |
for m in &list { |
| 2596 |
println!("{}", milestone_line(m)); |
| 2597 |
} |
| 2598 |
} |
| 2599 |
MilestoneCmd::Create { title, description, due } => { |
| 2600 |
let m: hub_api::Milestone = |
| 2601 |
c.send("POST", &format!("{base}/milestones"), &hub_api::NewMilestone { title, description, due })?; |
| 2602 |
if json { |
| 2603 |
println!("{}", serde_json::to_string(&m)?); |
| 2604 |
} else { |
| 2605 |
println!("{} milestone {}", ui::ok("✔"), m.title); |
| 2606 |
} |
| 2607 |
} |
| 2608 |
MilestoneCmd::Edit { title, rename, description, due } => { |
| 2609 |
if rename.is_none() && description.is_none() && due.is_none() { |
| 2610 |
bail!("say what to change: --rename, --description or --due"); |
| 2611 |
} |
| 2612 |
edit(&title, hub_api::MilestoneEdit { title: rename, description, due, state: None }, "edited")?; |
| 2613 |
} |
| 2614 |
MilestoneCmd::Close { title } => { |
| 2615 |
edit(&title, hub_api::MilestoneEdit { state: Some("closed".into()), ..Default::default() }, "closed")?; |
| 2616 |
} |
| 2617 |
MilestoneCmd::Reopen { title } => { |
| 2618 |
edit(&title, hub_api::MilestoneEdit { state: Some("open".into()), ..Default::default() }, "reopened")?; |
| ba0a55bb Andy McCall ↶ |
2619 |
} |
| 2620 |
} |
| 2621 |
Ok(0) |
| 2622 |
} |
| 2623 |
|
| 2624 |
fn tag(c: &Client, t: &Target, cmd: TagCmd, json: bool) -> Result<i32> { |
| 2625 |
let base = t.repo_path()?; |
| 2626 |
match cmd { |
| 2627 |
TagCmd::List => { |
| 2628 |
let path = format!("{base}/tags"); |
| 2629 |
if json { |
| 2630 |
println!("{}", c.get_text(&path)?); |
| 2631 |
return Ok(0); |
| 2632 |
} |
| 2633 |
let tags: Vec<Tag> = c.get(&path)?; |
| 2634 |
if tags.is_empty() { |
| 2635 |
println!("{}", ui::dim("no tags")); |
| 2636 |
} |
| 2637 |
for t in tags { |
| 2638 |
let heads: Vec<&str> = t.heads.iter().map(|h| &h[..h.len().min(12)]).collect(); |
| 2639 |
let signer = t.signer.map(|s| format!("signed by {s}")).unwrap_or_else(|| "unsigned".into()); |
| 2640 |
println!("{} {} {}", t.name, ui::dim(&heads.join(" ")), ui::dim(&signer)); |
| 2641 |
} |
| 2642 |
} |
| 2643 |
TagCmd::Create { name, target, message } => { |
| 2644 |
let new = hub_api::NewTag { name, target, message }; |
| 2645 |
let made: Tag = c.send("POST", &format!("{base}/tags"), &new)?; |
| 2646 |
if json { |
| 2647 |
println!("{}", serde_json::to_string(&made)?); |
| 2648 |
} else { |
| 2649 |
println!("{} tagged {} {}", ui::ok("✔"), made.name, ui::dim(&format!("{} head(s)", made.heads.len()))); |
| 2650 |
} |
| 2651 |
} |
| 2652 |
TagCmd::Delete { name, reason } => { |
| 2653 |
let reason = reason.unwrap_or_default(); |
| 2654 |
c.send_text("DELETE", &format!("{base}/tags/{}?reason={}", enc(&name), enc(&reason)), &serde_json::json!({}))?; |
| 2655 |
if json { |
| 2656 |
println!("{}", serde_json::json!({ "withdrawn": name })); |
| 2657 |
} else { |
| 2658 |
println!("{} withdrew {} {}", ui::ok("✔"), name, ui::dim("(clones drop it on their next fetch)")); |
| 2659 |
} |
| 5c6e92fd Andy McCall ↶ |
2660 |
} |
| 2661 |
} |
| 2662 |
Ok(0) |
| 2663 |
} |
| 2664 |
|
| 2665 |
fn release(c: &Client, t: &Target, cmd: ReleaseCmd, json: bool) -> Result<i32> { |
| 2666 |
let base = t.repo_path()?; |
| 2667 |
match cmd { |
| 2668 |
ReleaseCmd::List => { |
| 2669 |
let path = format!("{base}/releases"); |
| 2670 |
if json { |
| 2671 |
println!("{}", c.get_text(&path)?); |
| 2672 |
return Ok(0); |
| 2673 |
} |
| 2674 |
let list: Vec<Release> = c.get(&path)?; |
| 2675 |
if list.is_empty() { |
| 2676 |
println!("{}", ui::dim("No releases.")); |
| 2677 |
} |
| 2678 |
for r in &list { |
| 2679 |
println!( |
| 2680 |
"{} {} {}{}", |
| 2681 |
if r.latest { ui::ok("●") } else { ui::dim("○") }, |
| 2682 |
ui::strong(&r.tag), |
| 2683 |
r.title, |
| 2684 |
ui::dim(&format!( |
| 2685 |
" · {} asset{} · {}", |
| 2686 |
r.assets.len(), |
| 2687 |
if r.assets.len() == 1 { "" } else { "s" }, |
| 2688 |
ui::ago(r.created_ms) |
| 2689 |
)) |
| 2690 |
); |
| 2691 |
} |
| 2692 |
} |
| 2693 |
ReleaseCmd::Create { tag, title, notes, prerelease } => { |
| 2694 |
let new = NewRelease { title: title.unwrap_or_else(|| tag.clone()), tag, notes, prerelease }; |
| 2695 |
let r: Release = c.send("POST", &format!("{base}/releases"), &new)?; |
| 2696 |
if json { |
| 2697 |
println!("{}", serde_json::to_string(&r)?); |
| 2698 |
} else { |
| 2699 |
println!("{} released {} {}", ui::ok("✔"), r.tag, ui::dim(&r.url)); |
| ced6d3af Andy McCall ↶ |
2700 |
} |
| 2701 |
} |
| 2702 |
ReleaseCmd::Edit { tag, title, notes, prerelease } => { |
| 2703 |
let notes = match notes.as_deref() { |
| 2704 |
Some("-") => Some(body_of(None, Some("-".into()))?), |
| 2705 |
_ => notes, |
| 2706 |
}; |
| 2707 |
if title.is_none() && notes.is_none() && prerelease.is_none() { |
| 2708 |
bail!("say what to change: --title, --notes or --prerelease"); |
| 2709 |
} |
| 2710 |
let edit = hub_api::ReleaseEdit { title, notes, prerelease }; |
| 2711 |
let r: Release = c.send("PATCH", &format!("{base}/releases/{tag}"), &edit)?; |
| 2712 |
if json { |
| 2713 |
println!("{}", serde_json::to_string(&r)?); |
| 2714 |
} else { |
| 2715 |
println!("{} edited {} {}", ui::ok("✔"), r.tag, ui::dim(&r.url)); |
| 5c6e92fd Andy McCall ↶ |
2716 |
} |
| 2717 |
} |
| 2718 |
ReleaseCmd::Upload { tag, files } => { |
| 2719 |
if files.is_empty() { |
| 2720 |
bail!("name the files to attach"); |
| 2721 |
} |
| 2722 |
for f in files { |
| 2723 |
let name = f.file_name().and_then(|n| n.to_str()).context("a file name")?.to_string(); |
| 2724 |
let bytes = std::fs::read(&f).with_context(|| format!("read {}", f.display()))?; |
| 2725 |
c.upload(&format!("{base}/releases/{}/assets?name={}", enc(&tag), enc(&name)), &bytes)?; |
| 2726 |
println!("{} attached {name} to {tag}", ui::ok("✔")); |
| 2727 |
} |
| 2728 |
} |
| 2729 |
ReleaseCmd::Trust { tag } => { |
| 2730 |
let path = format!("{base}/releases/trust/{}", enc(&tag)); |
| 2731 |
if json { |
| 2732 |
println!("{}", c.get_text(&path)?); |
| 2733 |
return Ok(0); |
| 2734 |
} |
| 2735 |
let r: serde_json::Value = c.get(&path)?; |
| 2736 |
println!("{} {}", ui::strong(&format!("Trust report for {tag}")), ui::dim(&format!("{} changes", r["changes"]))); |
| 2737 |
let mut worst = 0; |
| 2738 |
for ch in r["audit"].as_array().into_iter().flatten() { |
| 2739 |
let v = ch["verdict"].as_str().unwrap_or(""); |
| 2740 |
worst = worst.max(match v { |
| 2741 |
"fail" => 2, |
| 2742 |
"warn" => 1, |
| 2743 |
_ => 0, |
| 2744 |
}); |
| 2745 |
let g = match v { |
| 2746 |
"pass" => ui::ok("✔"), |
| 2747 |
"warn" => ui::warn("!"), |
| 2748 |
_ => ui::bad("✘"), |
| 2749 |
}; |
| 2750 |
println!("{g} {} {}", ch["title"].as_str().unwrap_or(""), ui::dim(ch["detail"].as_str().unwrap_or(""))); |
| 2751 |
} |
| 2752 |
return Ok(if worst == 2 { 1 } else { 0 }); |
| 2753 |
} |
| 2754 |
} |
| 2755 |
Ok(0) |
| 166f6e9b Andy McCall |
2756 |
} |
| 2757 |
|
| 2758 |
#[cfg(test)] |
| 2759 |
mod tests { |
| fac6853c Andy McCall ↶ |
2760 |
|
| 2761 |
#[test] |
| 2762 |
fn job_timings_and_report_commands_read_plainly() { |
| 2763 |
use clap::Parser; |
| 2764 |
let job = hub_api::FlowJob { |
| 2765 |
name: "perf".into(), |
| 2766 |
status: "passed".into(), |
| 2767 |
depends_on: vec![], |
| 2768 |
runner: "earth-1".into(), |
| 2769 |
exit_code: Some(0), |
| 2770 |
error: String::new(), |
| 2771 |
started_ms: Some(13_000), |
| 2772 |
finished_ms: Some(200_000), |
| 2773 |
log_lines: 10, |
| 2774 |
waiting: String::new(), |
| 2775 |
placed: String::new(), |
| 2776 |
approval: String::new(), |
| 2777 |
queued_ms: Some(1_000), |
| 2778 |
ready_ms: Some(12_500), |
| 2779 |
phases: vec![hub_api::FlowPhase { name: "image".into(), ms: 850 }, hub_api::FlowPhase { name: "run".into(), ms: 125_400 }], |
| 2780 |
peak_disk_mib: Some(2150), |
| 2781 |
peak_memory_mib: Some(900), |
| 2782 |
}; |
| 2783 |
assert_eq!(super::timing_line(&job), "waited 12.0 s · image 850 ms · run 2 min 5 s · peak disk 2.1 GiB, memory 900 MiB"); |
| 2784 |
let bare = hub_api::FlowJob { queued_ms: None, phases: vec![], peak_disk_mib: None, peak_memory_mib: None, ..job }; |
| 2785 |
assert_eq!(super::timing_line(&bare), ""); |
| 2786 |
assert_eq!(super::enc_path("data/frame times.csv"), "data/frame%20times.csv"); |
| 2787 |
|
| 2788 |
let cli = |
| 2789 |
super::Cli::try_parse_from(["oh", "run", "report", "26", "performance", "--file", "data/frames.csv", "-o", "f.csv"]).unwrap(); |
| 2790 |
let super::Cmd::Run { cmd: super::RunCmd::Report { number, job, pick } } = cli.cmd else { panic!("run report") }; |
| 2791 |
assert_eq!((number, job.as_str(), pick.file.as_deref(), pick.list), (26, "performance", Some("data/frames.csv"), false)); |
| 2792 |
assert!(super::Cli::try_parse_from(["oh", "run", "report", "26", "perf", "--list", "--file", "x"]).is_err(), "one or the other"); |
| 2793 |
assert!(super::Cli::try_parse_from(["oh", "run", "report", "26", "perf", "-o", "x"]).is_err(), "-o is for --file"); |
| 2794 |
let cli = super::Cli::try_parse_from(["oh", "report", "show", "performance", "--bookmark", "main", "--list"]).unwrap(); |
| 2795 |
assert!(matches!(cli.cmd, super::Cmd::Report { cmd: super::ReportCmd::Show { list: true, .. } })); |
| 2796 |
assert!(super::Cli::try_parse_from(["oh", "run", "view", "3", "--timings"]).is_ok()); |
| 2797 |
} |
| 9bbca80f Andy McCall ↶ |
2798 |
|
| 2799 |
#[test] |
| 2800 |
fn runner_update_scopes_resolve_by_name_or_id() { |
| 2801 |
let all = hub_api::RunnerUpdates { |
| 2802 |
repo: String::new(), |
| 2803 |
settings: vec![], |
| 1ec29691 Andy McCall ↶ |
2804 |
groups: vec![hub_api::RunnerGroup { |
| 2805 |
id: 4, |
| 2806 |
owner: "acme".into(), |
| 2807 |
name: "canary".into(), |
| 2808 |
percent: None, |
| 2809 |
members: vec![], |
| 2810 |
wave: None, |
| 2811 |
}], |
| 9bbca80f Andy McCall ↶ |
2812 |
runners: vec![], |
| 2813 |
holds: vec![], |
| 2814 |
}; |
| 2815 |
assert_eq!(update_scope("site", &all).unwrap(), ("site".to_string(), String::new())); |
| 2816 |
assert_eq!(update_scope("owner:acme", &all).unwrap(), ("owner".to_string(), "acme".to_string())); |
| 2817 |
for g in ["group:canary", "group:acme/canary", "group:4"] { |
| 2818 |
assert_eq!(update_scope(g, &all).unwrap(), ("group".to_string(), "4".to_string()), "{g}"); |
| 2819 |
} |
| 2820 |
assert!(update_scope("runner:plex", &all).unwrap_err().to_string().contains("no runner")); |
| 2821 |
assert!(update_scope("everyone", &all).is_err()); |
| 2822 |
assert_eq!(switch(Some("inherit")).unwrap(), Some(None)); |
| 2823 |
assert!(switch(Some("yes")).is_err()); |
| 2824 |
} |
| 166f6e9b Andy McCall |
2825 |
use super::*; |
| 2826 |
|
| 2827 |
|
| 2828 |
|
| 2829 |
#[test] |
| 2830 |
fn drop_commands_parse() { |
| 2831 |
let cli = Cli::try_parse_from(["oh", "drop", "create", "a.txt", "b.md", "--secret", "-d", "Two files", "-t", "Notes"]).unwrap(); |
| 2832 |
match cli.cmd { |
| 2833 |
Cmd::Drop { cmd: DropCmd::Create { files, secret, description, title } } => { |
| 2834 |
assert_eq!(files, vec![std::path::PathBuf::from("a.txt"), std::path::PathBuf::from("b.md")]); |
| 2835 |
assert!(secret); |
| 2836 |
assert_eq!((description.as_deref(), title.as_deref()), (Some("Two files"), Some("Notes"))); |
| 2837 |
} |
| 2838 |
_ => panic!("not drop create"), |
| 2839 |
} |
| 2840 |
assert!(Cli::try_parse_from(["oh", "drop", "create"]).is_err(), "a drop needs a file"); |
| 9bbca80f Andy McCall ↶ |
2841 |
match Cli::try_parse_from(["oh", "runner-updates", "set", "group:canary", "--updates", "on", "--window", "02-05"]).unwrap() { |
| 2842 |
Cli { cmd: Cmd::RunnerUpdates { cmd: Some(RunnerUpdatesCmd::Set { scope, updates, window, channel, dry_run }) }, .. } => { |
| 2843 |
assert_eq!((scope.as_str(), updates.as_deref(), window.as_deref()), ("group:canary", Some("on"), Some("02-05"))); |
| 2844 |
assert!(channel.is_none() && dry_run.is_none()); |
| 2845 |
} |
| 2846 |
_ => panic!("not runner-updates set"), |
| 2847 |
} |
| 2848 |
assert!(matches!(Cli::try_parse_from(["oh", "runner-updates"]).unwrap().cmd, Cmd::RunnerUpdates { cmd: None })); |
| 166f6e9b Andy McCall |
2849 |
match Cli::try_parse_from(["oh", "drop", "list", "--user", "bob", "--json"]).unwrap() { |
| 2850 |
Cli { cmd: Cmd::Drop { cmd: DropCmd::List { user } }, json, .. } => assert_eq!((user.as_deref(), json), (Some("bob"), true)), |
| 2851 |
_ => panic!("not drop list"), |
| 2852 |
} |
| 2853 |
match Cli::try_parse_from(["oh", "drop", "view", "0123456789abcdef01234567", "-f", "a.txt"]).unwrap().cmd { |
| 2854 |
Cmd::Drop { cmd: DropCmd::View { id, file } } => { |
| 2855 |
assert_eq!((id.as_str(), file.as_deref()), ("0123456789abcdef01234567", Some("a.txt"))) |
| 2856 |
} |
| 2857 |
_ => panic!("not drop view"), |
| 2858 |
} |
| 2859 |
assert!(matches!(Cli::try_parse_from(["oh", "drop", "delete", "x"]).unwrap().cmd, Cmd::Drop { cmd: DropCmd::Delete { .. } })); |
| 04f9c984 Andy McCall ↶ |
2860 |
for (verb, ok) in [("fork", true), ("star", true), ("unstar", true), ("comments", true)] { |
| 2861 |
assert_eq!(Cli::try_parse_from(["oh", "drop", verb, "x"]).is_ok(), ok, "{verb}"); |
| 2862 |
} |
| 2863 |
assert!(Cli::try_parse_from(["oh", "drop", "comment", "x"]).is_err(), "a comment says something"); |
| 2864 |
match Cli::try_parse_from(["oh", "drop", "comment", "x", "-m", "Nice"]).unwrap().cmd { |
| 2865 |
Cmd::Drop { cmd: DropCmd::Comment { message, .. } } => assert_eq!(message, "Nice"), |
| 2866 |
_ => panic!("not drop comment"), |
| 2867 |
} |
| 166f6e9b Andy McCall |
2868 |
assert_eq!(drop_id("0123456789ABCDEF01234567").unwrap(), "0123456789abcdef01234567"); |
| 2869 |
assert_eq!(drop_id("https://hub.example/andy/drops/0123456789abcdef01234567/").unwrap(), "0123456789abcdef01234567"); |
| 2870 |
assert!(drop_id("andy/notes").is_err()); |
| 2871 |
} |
| 5c6e92fd Andy McCall ↶ |
2872 |
} |