diff options
author | Vika <vika@fireburn.ru> | 2021-12-05 23:00:01 +0300 |
---|---|---|
committer | Vika <vika@fireburn.ru> | 2021-12-05 23:00:01 +0300 |
commit | 4aa7f01da39ab55b4f6346e7565d8bb29566de39 (patch) | |
tree | 125c147e1a39dfebc4be1a3ac3a6dd8fadb3855e /src/bin | |
parent | c0b552318cb08872c7deae82a10122b0e16b7ae8 (diff) | |
download | kittybox-4aa7f01da39ab55b4f6346e7565d8bb29566de39.tar.zst |
Code cleanup and small bugfixing in templates
Diffstat (limited to 'src/bin')
-rw-r--r-- | src/bin/kittybox_database_converter.rs | 72 |
1 files changed, 47 insertions, 25 deletions
diff --git a/src/bin/kittybox_database_converter.rs b/src/bin/kittybox_database_converter.rs index 900a5c6..4dcd2ab 100644 --- a/src/bin/kittybox_database_converter.rs +++ b/src/bin/kittybox_database_converter.rs @@ -1,63 +1,77 @@ use anyhow::{anyhow, Context}; -use redis::{self, AsyncCommands}; -use kittybox::database::Storage; use kittybox::database::FileStorage; +use kittybox::database::Storage; +use redis::{self, AsyncCommands}; use std::collections::HashMap; /// Convert from a Redis storage to a new storage new_storage. async fn convert_from_redis<S: Storage>(from: String, new_storage: S) -> anyhow::Result<()> { let db = redis::Client::open(from).context("Failed to open the Redis connection")?; - let mut conn = db.get_async_std_connection().await.context("Failed to connect to Redis")?; + let mut conn = db + .get_async_std_connection() + .await + .context("Failed to connect to Redis")?; // Rebinding to convince the borrow checker we're not smuggling stuff outta scope let storage = &new_storage; - + let mut stream = conn.hscan::<_, String>("posts").await?; while let Some(key) = stream.next_item().await { let value = serde_json::from_str::<serde_json::Value>( - &stream.next_item().await - .ok_or(anyhow!("Failed to find a corresponding value for the key"))? + &stream + .next_item() + .await + .ok_or(anyhow!("Failed to find a corresponding value for the key"))?, )?; println!("{}, {:?}", key, value); if value["see_other"].is_string() { - continue + continue; } - let user = &( - url::Url::parse( - value["properties"]["uid"][0] - .as_str().unwrap() - ).unwrap().origin().ascii_serialization().clone() - + "/" - ); + let user = &(url::Url::parse(value["properties"]["uid"][0].as_str().unwrap()) + .unwrap() + .origin() + .ascii_serialization() + .clone() + + "/"); if let Err(err) = storage.clone().put_post(&value, user).await { eprintln!("Error saving post: {}", err); } - } - + let mut stream: redis::AsyncIter<String> = conn.scan_match("settings_*").await?; while let Some(key) = stream.next_item().await { - let mut conn = db.get_async_std_connection().await.context("Failed to connect to Redis")?; + let mut conn = db + .get_async_std_connection() + .await + .context("Failed to connect to Redis")?; let user = key.strip_prefix("settings_").unwrap(); - match conn.hgetall::<&str, HashMap<String, String>>(&key).await.context(format!("Failed getting settings from key {}", key)) { + match conn + .hgetall::<&str, HashMap<String, String>>(&key) + .await + .context(format!("Failed getting settings from key {}", key)) + { Ok(settings) => { for (k, v) in settings.iter() { - if let Err(e) = storage.set_setting(k, &user, v).await.with_context(|| format!("Failed setting {} for {}", k, user)) { + if let Err(e) = storage + .set_setting(k, &user, v) + .await + .with_context(|| format!("Failed setting {} for {}", k, user)) + { eprintln!("{}", e); } } - }, + } Err(e) => { eprintln!("{}", e); } } } - + return Ok(()); } @@ -65,17 +79,25 @@ async fn convert_from_redis<S: Storage>(from: String, new_storage: S) -> anyhow: async fn main() -> anyhow::Result<()> { let mut args = std::env::args(); args.next(); // skip argv[0] - let old_uri = args.next().ok_or_else(|| anyhow!("No import source is provided."))?; - let new_uri = args.next().ok_or_else(|| anyhow!("No import destination is provided."))?; + let old_uri = args + .next() + .ok_or_else(|| anyhow!("No import source is provided."))?; + let new_uri = args + .next() + .ok_or_else(|| anyhow!("No import destination is provided."))?; let storage = if new_uri.starts_with("file:") { let folder = new_uri.strip_prefix("file://").unwrap(); let path = std::path::PathBuf::from(folder); - Box::new(FileStorage::new(path).await.context("Failed to construct the file storage")?) + Box::new( + FileStorage::new(path) + .await + .context("Failed to construct the file storage")?, + ) } else { anyhow::bail!("Cannot construct the storage abstraction for destination storage. Check the storage type?"); }; - + if old_uri.starts_with("redis") { convert_from_redis(old_uri, *storage).await? } |