diff --git a/src/command.rs b/src/command.rs index 07e4fb4..268f734 100644 --- a/src/command.rs +++ b/src/command.rs @@ -384,6 +384,12 @@ pub enum ConnectionsCommands { format: String, }, + /// Refresh a connection's schema + Refresh { + /// Connection ID + connection_id: String, + }, + /// Delete a connection from a workspace Delete { /// Connection ID diff --git a/src/connections.rs b/src/connections.rs index 1e3818a..477192d 100644 --- a/src/connections.rs +++ b/src/connections.rs @@ -328,3 +328,52 @@ pub fn list(workspace_id: &str, format: &str) { _ => unreachable!(), } } + +pub fn refresh(workspace_id: &str, connection_id: &str) { + let profile_config = match config::load("default") { + Ok(c) => c, + Err(e) => { + eprintln!("{e}"); + std::process::exit(1); + } + }; + + let api_key = match &profile_config.api_key { + Some(key) if key != "PLACEHOLDER" => key.clone(), + _ => { + eprintln!("error: not authenticated. Run 'hotdata auth login' to log in."); + std::process::exit(1); + } + }; + + let body = serde_json::json!({ + "connection_id": connection_id, + "data": false, + }); + + let url = format!("{}/refresh", profile_config.api_url); + let client = reqwest::blocking::Client::new(); + + let resp = match client + .post(&url) + .header("Authorization", format!("Bearer {api_key}")) + .header("X-Workspace-Id", workspace_id) + .json(&body) + .send() + { + Ok(r) => r, + Err(e) => { + eprintln!("error connecting to API: {e}"); + std::process::exit(1); + } + }; + + if !resp.status().is_success() { + use crossterm::style::Stylize; + eprintln!("{}", crate::util::api_error(resp.text().unwrap_or_default()).red()); + std::process::exit(1); + } + + use crossterm::style::Stylize; + println!("{}", "Schema refresh completed.".green()); +} diff --git a/src/main.rs b/src/main.rs index 7499bda..67e5cd2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -134,6 +134,9 @@ fn main() { } } } + ConnectionsCommands::Refresh { connection_id } => { + connections::refresh(&workspace_id, &connection_id) + } _ => eprintln!("not yet implemented"), } },