Skip to main content

ytcli/cli/
bulk.rs

1//! Reading a bulk change back.
2//!
3//! A group of its own, holding one read and nothing else. The write that starts
4//! a bulk change is `issue update`, which is where a caller already looks to
5//! change issues; this is the handle on work Tracker is still doing, and it has
6//! to be allowlistable without allowing anything to be written.
7
8use clap::Subcommand;
9
10use crate::cli::{Session, emit, report};
11use crate::exit::ExitCode;
12use crate::render::{Format, bulk, machine};
13
14#[derive(Debug, Subcommand)]
15pub enum BulkCommand {
16    /// Show how far a bulk change got.
17    #[command(long_about = crate::cli::help::md(crate::cli::help::BULK_STATUS))]
18    Status {
19        /// The id `issue update` printed.
20        id: String,
21    },
22}
23
24pub async fn run(command: &BulkCommand, session: &Session) -> ExitCode {
25    let client = match session.client() {
26        Ok(client) => client,
27        Err(code) => return code,
28    };
29
30    let BulkCommand::Status { id } = command;
31    let change = match client.bulk_change(id).await {
32        Ok(change) => change,
33        Err(error) => {
34            let code = error.exit_code();
35            return report(&error, code);
36        }
37    };
38
39    if session.render.format != Format::Text {
40        let format = match session.render.format {
41            Format::JsonRaw => Format::Json,
42            other => other,
43        };
44        return match machine(&change, format) {
45            Ok(text) => {
46                emit(&text);
47                ExitCode::Success
48            }
49            Err(error) => report(&error, ExitCode::Failure),
50        };
51    }
52
53    emit(&bulk::change(&change, &session.render));
54
55    // A change still running has nothing per issue to say yet, and one that
56    // worked has said it in the tally. Only a finished change that did not do
57    // everything is worth the second request.
58    if change.finished() && !change.succeeded() {
59        match client.bulk_change_issues(id).await {
60            Ok(outcomes) => emit(&bulk::failures(&outcomes, &session.render)),
61            Err(error) => {
62                let code = error.exit_code();
63                return report(&error, code);
64            }
65        }
66    }
67
68    // Reading is not judging: this says what happened, and exits zero for having
69    // answered. `issue update` is where a failed change decides an exit code.
70    ExitCode::Success
71}