#![allow(dead_code)]
use anyhow::Result;
use iroh::SecretKey;
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, EnvFilter};
pub fn get_or_generate_secret_key() -> Result<SecretKey> {
use std::{env, str::FromStr};
use anyhow::Context;
if let Ok(secret) = env::var("IROH_SECRET") {
SecretKey::from_str(&secret).context("Invalid secret key format")
} else {
let secret_key = SecretKey::generate(&mut rand::rng());
println!(
"Generated new secret key: {}",
hex::encode(secret_key.to_bytes())
);
println!("To reuse this key, set the IROH_SECRET environment variable to this value");
Ok(secret_key)
}
}
pub fn setup_logging() {
tracing_subscriber::registry()
.with(tracing_subscriber::fmt::layer().with_writer(std::io::stderr))
.with(EnvFilter::from_default_env())
.try_init()
.ok();
}