Skip to main content

ytcli/cli/
board.rs

1//! Board commands. Read-only: a board is a view of work, and moving work about
2//! from a command line is what `issue update` is for.
3
4use clap::Subcommand;
5
6use crate::cli::{Session, emit, report};
7use crate::exit::ExitCode;
8use crate::render::{Format, board as render, machine};
9
10#[derive(Debug, Subcommand)]
11pub enum BoardCommand {
12    /// List boards.
13    #[command(long_about = crate::cli::help::md(crate::cli::help::BOARD_LIST))]
14    List,
15    /// Show one board and its columns.
16    #[command(long_about = crate::cli::help::md(crate::cli::help::BOARD_GET))]
17    Get {
18        /// Board id as returned by `board list`.
19        id: String,
20    },
21    /// List the sprints of a board.
22    #[command(long_about = crate::cli::help::md(crate::cli::help::BOARD_SPRINTS))]
23    Sprints {
24        /// Board id as returned by `board list`.
25        id: String,
26    },
27}
28
29pub async fn run(command: &BoardCommand, session: &Session) -> ExitCode {
30    let client = match session.client() {
31        Ok(client) => client,
32        Err(code) => return code,
33    };
34
35    let rendered = match command {
36        BoardCommand::List => match client.boards().await {
37            Ok(boards) => match session.render.format {
38                Format::Text => Ok(render::boards(&boards, &session.render)),
39                Format::JsonRaw => machine(&boards, Format::Json),
40                other => machine(&boards, other),
41            },
42            Err(error) => return failed(&error),
43        },
44        BoardCommand::Get { id } => match client.board(id).await {
45            Ok(board) => match session.render.format {
46                Format::Text => Ok(render::board(&board, &session.render)),
47                Format::JsonRaw => machine(&board, Format::Json),
48                other => machine(&board, other),
49            },
50            Err(error) => return failed(&error),
51        },
52        BoardCommand::Sprints { id } => match client.sprints(id).await {
53            Ok(sprints) => match session.render.format {
54                Format::Text => Ok(render::sprints(id, &sprints, &session.render)),
55                Format::JsonRaw => machine(&sprints, Format::Json),
56                other => machine(&sprints, other),
57            },
58            Err(error) => return failed(&error),
59        },
60    };
61
62    match rendered {
63        Ok(text) => {
64            emit(&text);
65            ExitCode::Success
66        }
67        Err(error) => report(&error, ExitCode::Failure),
68    }
69}
70
71fn failed(error: &crate::api::error::ApiError) -> ExitCode {
72    let code = error.exit_code();
73    report(error, code)
74}