|
1 | 1 | use napi_derive::napi; |
2 | 2 | use std::env; |
3 | | -use std::fs; |
| 3 | +use std::fs::{self, File}; |
| 4 | +use std::io; |
4 | 5 | use std::path::Path; |
5 | 6 | use std::process::Command; |
6 | 7 | use serde::{Serialize, Deserialize}; |
@@ -69,6 +70,22 @@ async fn handle_postgres_connection( |
69 | 70 | } |
70 | 71 | } |
71 | 72 |
|
| 73 | +fn read_file(path: &str) -> napi::Result<File> { |
| 74 | + fs::File::open(path) |
| 75 | + .map_err(|error| napi::Error::new( |
| 76 | + napi::Status::GenericFailure, |
| 77 | + format!("Failed to open {}: {}", path, error) |
| 78 | + )) |
| 79 | +} |
| 80 | + |
| 81 | +fn create_file(path: &str) -> napi::Result<File> { |
| 82 | + fs::File::create(path) |
| 83 | + .map_err(|error| napi::Error::new( |
| 84 | + napi::Status::GenericFailure, |
| 85 | + format!("Failed to create {}: {}", path, error) |
| 86 | + )) |
| 87 | +} |
| 88 | + |
72 | 89 | #[napi] |
73 | 90 | pub async fn run_npm_script(script: String) -> napi::Result<ProcessOutput> { |
74 | 91 | let home_dir = env::var("HOME").or_else(|_| env::var("USERPROFILE")).unwrap_or_default(); |
@@ -194,3 +211,54 @@ pub fn file_exists(file_path: String) -> bool { |
194 | 211 | fs::metadata(filepath).map(|metadata| metadata.is_file()).unwrap_or(false) |
195 | 212 | } |
196 | 213 |
|
| 214 | +#[napi] |
| 215 | +pub async fn rename_database(url: String, database: String, new_database_name: String) -> napi::Result<PgResponse> { |
| 216 | + let (client, connection) = match create_postgres_connection(&url).await { |
| 217 | + Ok((client, connection)) => (client, connection), |
| 218 | + Err(error) => { |
| 219 | + return create_postgres_error_response(&error); |
| 220 | + } |
| 221 | + }; |
| 222 | + |
| 223 | + tokio::spawn(handle_postgres_connection(connection)); |
| 224 | + |
| 225 | + let query = format!("ALTER DATABASE \"{}\" RENAME TO \"{}\";", database, new_database_name); |
| 226 | + match client.batch_execute(&query).await { |
| 227 | + Ok(_) => Ok(PgResponse { |
| 228 | + code: "00000".to_string(), |
| 229 | + message: format!("Database {} renamed to {}", database, new_database_name) |
| 230 | + }), |
| 231 | + Err(error) => create_postgres_error_response(&error) |
| 232 | + } |
| 233 | +} |
| 234 | + |
| 235 | +#[napi] |
| 236 | +pub fn find_nonexistent_files(paths: Vec<String>) -> Vec<String> { |
| 237 | + paths |
| 238 | + .into_iter() |
| 239 | + .filter(|path| !fs::metadata(path).is_ok()) |
| 240 | + .collect() |
| 241 | +} |
| 242 | + |
| 243 | +#[napi] |
| 244 | +pub fn copy_file( |
| 245 | + source: String, |
| 246 | + destination: String, |
| 247 | + create_dest_if_not_exists: Option<bool> |
| 248 | +) -> napi::Result<()> { |
| 249 | + let mut source_file = read_file(&source)?; |
| 250 | + |
| 251 | + let mut destination_file = if create_dest_if_not_exists.unwrap_or(false) { |
| 252 | + create_file(&destination)? |
| 253 | + } else { |
| 254 | + read_file(&destination)? |
| 255 | + }; |
| 256 | + |
| 257 | + io::copy(&mut source_file, &mut destination_file) |
| 258 | + .map_err(|error| napi::Error::new( |
| 259 | + napi::Status::GenericFailure, |
| 260 | + format!("Failed to write to {}: {}", &destination, error) |
| 261 | + ))?; |
| 262 | + |
| 263 | + Ok(()) |
| 264 | +} |
0 commit comments