Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 13 additions & 12 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ uuid = { version = "1", features = ["v4"] }
regex = "1"
anyhow = "1"
rand = "0.8"
reqwest = { version = "0.12", default-features = false, features = ["rustls-tls"] }
37 changes: 33 additions & 4 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,40 @@ fn expand_env_vars(raw: &str) -> String {
.into_owned()
}

pub fn load_config(path: &Path) -> anyhow::Result<Config> {
let raw = std::fs::read_to_string(path)
.map_err(|e| anyhow::anyhow!("failed to read {}: {e}", path.display()))?;
fn is_url(s: &str) -> bool {
s.starts_with("http://") || s.starts_with("https://")
}

async fn fetch_remote_config(url: &str) -> anyhow::Result<String> {
let client = reqwest::Client::builder()
.timeout(std::time::Duration::from_secs(10))
.build()?;
let response = client
.get(url)
.send()
.await
.map_err(|e| anyhow::anyhow!("failed to fetch remote config from {url}: {e}"))?;
let status = response.status();
if !status.is_success() {
anyhow::bail!("remote config request to {url} failed with status {status}");
}
let body = response
.text()
.await
.map_err(|e| anyhow::anyhow!("failed to read response body from {url}: {e}"))?;
Ok(body)
}

pub async fn load_config_from_source(source: &str) -> anyhow::Result<Config> {
let raw = if is_url(source) {
fetch_remote_config(source).await?
} else {
let path = Path::new(source);
std::fs::read_to_string(path)
.map_err(|e| anyhow::anyhow!("failed to read {}: {e}", path.display()))?
};
let expanded = expand_env_vars(&raw);
let config: Config = toml::from_str(&expanded)
.map_err(|e| anyhow::anyhow!("failed to parse {}: {e}", path.display()))?;
.map_err(|e| anyhow::anyhow!("failed to parse config from {source}: {e}"))?;
Ok(config)
}
8 changes: 3 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ mod reactions;

use serenity::prelude::*;
use std::collections::HashSet;
use std::path::PathBuf;
use std::sync::Arc;
use tracing::info;

Expand All @@ -19,12 +18,11 @@ async fn main() -> anyhow::Result<()> {
)
.init();

let config_path = std::env::args()
let config_source = std::env::args()
.nth(1)
.map(PathBuf::from)
.unwrap_or_else(|| PathBuf::from("config.toml"));
.unwrap_or_else(|| "config.toml".into());

let cfg = config::load_config(&config_path)?;
let cfg = config::load_config_from_source(&config_source).await?;
info!(
agent_cmd = %cfg.agent.command,
pool_max = cfg.pool.max_sessions,
Expand Down