Skip to content

fix(iroh-relay): write default config if config path is set but does not exist #3245

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
15 changes: 13 additions & 2 deletions iroh-relay/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use iroh_relay::{
use n0_future::FutureExt;
use serde::{Deserialize, Serialize};
use tokio_rustls_acme::{caches::DirCache, AcmeConfig};
use tracing::debug;
use tracing::{debug, info};
use tracing_subscriber::{prelude::*, EnvFilter};

/// The default `http_bind_port` when using `--dev`.
Expand Down Expand Up @@ -415,13 +415,18 @@ impl Config {
let config_path = if let Some(config_path) = &opts.config_path {
config_path
} else {
info!("using default config");
return Ok(Config::default());
};

if config_path.exists() {
info!("reading config from {}", config_path.to_string_lossy());
Self::read_from_file(&config_path).await
} else {
Ok(Config::default())
let config = Config::default();
config.write_to_file(&config_path).await?;
info!("saved default config to {}", config_path.to_string_lossy());
Ok(config)
}
}

Expand All @@ -438,6 +443,12 @@ impl Config {
.context("unable to read config")?;
Self::from_str(&config_ser)
}

async fn write_to_file(&self, path: impl AsRef<Path>) -> Result<()> {
let s = toml::to_string(self)?;
tokio::fs::write(path, s).await?;
Ok(())
}
}

#[tokio::main]
Expand Down
Loading