kura_cli/commands/
provider.rs1use clap::Subcommand;
2use serde_json::json;
3use uuid::Uuid;
4
5use crate::util::{api_request, read_json_from_file};
6
7#[derive(Subcommand)]
8pub enum ProviderCommands {
9 List,
11 Upsert {
13 #[arg(long)]
15 request_file: String,
16 },
17 Revoke {
19 #[arg(long)]
21 connection_id: Uuid,
22 #[arg(long)]
24 reason: String,
25 },
26}
27
28pub async fn run(api_url: &str, token: Option<&str>, command: ProviderCommands) -> i32 {
29 match command {
30 ProviderCommands::List => list(api_url, token).await,
31 ProviderCommands::Upsert { request_file } => upsert(api_url, token, &request_file).await,
32 ProviderCommands::Revoke {
33 connection_id,
34 reason,
35 } => revoke(api_url, token, connection_id, &reason).await,
36 }
37}
38
39async fn list(api_url: &str, token: Option<&str>) -> i32 {
40 api_request(
41 api_url,
42 reqwest::Method::GET,
43 "/v1/providers/connections",
44 token,
45 None,
46 &[],
47 &[],
48 false,
49 false,
50 )
51 .await
52}
53
54async fn upsert(api_url: &str, token: Option<&str>, request_file: &str) -> i32 {
55 let body = match read_json_from_file(request_file) {
56 Ok(v) => v,
57 Err(e) => crate::util::exit_error(
58 &e,
59 Some("Provide a valid JSON provider-connection payload."),
60 ),
61 };
62
63 api_request(
64 api_url,
65 reqwest::Method::POST,
66 "/v1/providers/connections",
67 token,
68 Some(body),
69 &[],
70 &[],
71 false,
72 false,
73 )
74 .await
75}
76
77async fn revoke(api_url: &str, token: Option<&str>, connection_id: Uuid, reason: &str) -> i32 {
78 let path = format!("/v1/providers/connections/{connection_id}/revoke");
79 let body = json!({ "reason": reason });
80
81 api_request(
82 api_url,
83 reqwest::Method::POST,
84 &path,
85 token,
86 Some(body),
87 &[],
88 &[],
89 false,
90 false,
91 )
92 .await
93}