Skip to main content

xbp_cli/cli/handlers/
api.rs

1use crate::cli::commands;
2use crate::cli::error::{CliResult, ErrorFactory};
3use crate::cli::ui;
4use crate::commands::{
5    install_api_service, run_api_daemons, run_api_health, run_api_jobs, run_api_projects,
6    run_api_request, run_api_routes,
7};
8use crate::logging::{log_error, log_success};
9
10pub async fn handle_api(cmd: commands::ApiCmd, debug: bool) -> CliResult<()> {
11    match cmd.command {
12        commands::ApiSubCommand::Install { port } => {
13            if let Err(e) = ui::with_loader(
14                &format!("Installing xbp-api.service on port {}", port),
15                install_api_service(port, debug),
16            )
17            .await
18            {
19                let _ = log_error("api", "API systemd install failed", Some(&e)).await;
20                return Err(ErrorFactory::operation(
21                    "api",
22                    "install xbp-api.service",
23                    e,
24                    Some("Check systemd availability and permissions, then retry."),
25                ));
26            }
27            let _ = log_success(
28                "api",
29                &format!("xbp-api.service installed on port {}", port),
30                None,
31            )
32            .await;
33        }
34        commands::ApiSubCommand::Health(health_cmd) => {
35            if let Err(e) = run_api_health(&health_cmd).await {
36                let _ = log_error("api", "API health request failed", Some(&e)).await;
37                return Err(ErrorFactory::operation(
38                    "api",
39                    "run API health request",
40                    e,
41                    Some("Check `API_XBP_URL`, `XBP_API_TOKEN`, or use `--base-url` / `--web`."),
42                ));
43            }
44        }
45        commands::ApiSubCommand::Projects(projects_cmd) => {
46            if let Err(e) = run_api_projects(&projects_cmd).await {
47                let _ = log_error("api", "Projects API command failed", Some(&e)).await;
48                return Err(ErrorFactory::operation(
49                    "api",
50                    "run projects API command",
51                    e,
52                    Some("Check the project payload and configured API base URL."),
53                ));
54            }
55        }
56        commands::ApiSubCommand::Daemons(daemons_cmd) => {
57            if let Err(e) = run_api_daemons(&daemons_cmd).await {
58                let _ = log_error("api", "Daemons API command failed", Some(&e)).await;
59                return Err(ErrorFactory::operation(
60                    "api",
61                    "run daemons API command",
62                    e,
63                    Some("Check the daemon payload and configured API base URL."),
64                ));
65            }
66        }
67        commands::ApiSubCommand::Jobs(jobs_cmd) => {
68            if let Err(e) = run_api_jobs(&jobs_cmd).await {
69                let _ = log_error("api", "Deployment jobs API command failed", Some(&e)).await;
70                return Err(ErrorFactory::operation(
71                    "api",
72                    "run deployment jobs API command",
73                    e,
74                    Some("Check the job payload, IDs, and configured API base URL."),
75                ));
76            }
77        }
78        commands::ApiSubCommand::Routes(routes_cmd) => {
79            if let Err(e) = run_api_routes(&routes_cmd).await {
80                let _ = log_error("api", "Routes API command failed", Some(&e)).await;
81                return Err(ErrorFactory::operation(
82                    "api",
83                    "run routes API command",
84                    e,
85                    Some("Routes usually need a local API base such as `--base-url http://127.0.0.1:8080`."),
86                ));
87            }
88        }
89        commands::ApiSubCommand::Request(request_cmd) => {
90            if let Err(e) = run_api_request(&request_cmd).await {
91                let _ = log_error("api", "API request failed", Some(&e)).await;
92                return Err(ErrorFactory::operation(
93                    "api",
94                    "run API request",
95                    e,
96                    Some("Check `API_XBP_URL`, `XBP_API_TOKEN`, the path, and request body."),
97                ));
98            }
99        }
100    }
101    Ok(())
102}