You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
1.5 KiB

  1. use crate::args::ARGS;
  2. use anyhow::Context;
  3. use redis::AsyncCommands;
  4. use tracing::info;
  5. use url::Url;
  6. pub mod config_json;
  7. pub async fn get_redis_url() -> anyhow::Result<Url> {
  8. let base_redis_url = Url::parse(&ARGS.redis).context("parsing redis url")?;
  9. info!(
  10. "Connecting to redis {} to find project config...",
  11. base_redis_url
  12. );
  13. let client = redis::Client::open(base_redis_url.clone()).context("connecting to redis")?;
  14. let mut base_con = client
  15. .get_async_connection()
  16. .await
  17. .context("get_async_connection")?;
  18. info!("Connected!");
  19. info!("Attempting to retrieve project configuration...");
  20. let config: Option<String> = base_con
  21. .hget("trackers", &ARGS.project)
  22. .await
  23. .context("hget trackers")?;
  24. if config.is_none() {
  25. panic!("Unable to get project config!");
  26. }
  27. let config: config_json::Root =
  28. serde_json::from_str(&config.unwrap()).context("parsing project config")?;
  29. info!("Read config:\n{:#?}", config);
  30. if let Some(r) = config.redis {
  31. let mut u = Url::parse("redis://127.0.0.1/")?;
  32. u.set_host(Some(&r.host)).unwrap();
  33. u.set_port(Some(r.port)).unwrap();
  34. u.set_username("default").unwrap();
  35. u.set_password(Some(&r.pass)).unwrap();
  36. info!("Found project redis server at {}!", u);
  37. Ok(u)
  38. } else {
  39. info!("No project-specific redis config found; staying on this redis!");
  40. Ok(base_redis_url)
  41. }
  42. }