-
Notifications
You must be signed in to change notification settings - Fork 184
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
refactor: improve path handling in iroh dir #1345
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,11 +2,12 @@ | |
|
||
use std::{ | ||
collections::HashMap, | ||
env, | ||
env, fmt, | ||
path::{Path, PathBuf}, | ||
str::FromStr, | ||
}; | ||
|
||
use anyhow::{anyhow, Result}; | ||
use anyhow::{anyhow, bail, Result}; | ||
use config::{Environment, File, Value}; | ||
use iroh_net::{ | ||
defaults::{default_eu_derp_region, default_na_derp_region}, | ||
|
@@ -22,6 +23,65 @@ pub const CONFIG_FILE_NAME: &str = "iroh.config.toml"; | |
/// For example, `IROH_PATH=/path/to/config` would set the value of the `Config.path` field | ||
pub const ENV_PREFIX: &str = "IROH"; | ||
|
||
/// Paths to files or directory within the [`iroh_data_root`] used by Iroh. | ||
#[derive(Debug, Clone, Eq, PartialEq)] | ||
pub enum IrohPaths { | ||
/// Path to the node's private key for the [`iroh_net::PeerId`]. | ||
Keypair, | ||
/// Path to the node's [flat-file store](iroh::baomap::flat) for complete blobs. | ||
BaoFlatStoreComplete, | ||
/// Path to the node's [flat-file store](iroh::baomap::flat) for partial blobs. | ||
BaoFlatStorePartial, | ||
} | ||
impl From<&IrohPaths> for &'static str { | ||
fn from(value: &IrohPaths) -> Self { | ||
match value { | ||
IrohPaths::Keypair => "keypair", | ||
IrohPaths::BaoFlatStoreComplete => "blobs.v0", | ||
IrohPaths::BaoFlatStorePartial => "blobs-partial.v0", | ||
} | ||
} | ||
} | ||
impl FromStr for IrohPaths { | ||
type Err = anyhow::Error; | ||
fn from_str(s: &str) -> Result<Self> { | ||
Ok(match s { | ||
"keypair" => Self::Keypair, | ||
"blobs.v0" => Self::BaoFlatStoreComplete, | ||
"blobs-partial.v0" => Self::BaoFlatStorePartial, | ||
_ => bail!("unknown file or directory"), | ||
}) | ||
} | ||
} | ||
impl fmt::Display for IrohPaths { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
let s: &str = self.into(); | ||
write!(f, "{s}") | ||
} | ||
} | ||
impl AsRef<Path> for IrohPaths { | ||
fn as_ref(&self) -> &Path { | ||
let s: &str = self.into(); | ||
Path::new(s) | ||
} | ||
} | ||
impl IrohPaths { | ||
/// Get the path for this [`IrohPath`] by joining the name to `IROH_DATA_DIR` environment variable. | ||
pub fn with_env(self) -> Result<PathBuf> { | ||
let mut root = iroh_data_root()?; | ||
if !root.is_absolute() { | ||
root = std::env::current_dir()?.join(root); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. shouldn't this use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This comes from current main, I only copied it from There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hmm, okay lets leave it as is for now |
||
} | ||
Ok(self.with_root(root)) | ||
} | ||
|
||
/// Get the path for this [`IrohPath`] by joining the name to a root directory. | ||
pub fn with_root(self, root: impl AsRef<Path>) -> PathBuf { | ||
let path = root.as_ref().join(self); | ||
path | ||
} | ||
} | ||
|
||
/// The configuration for the iroh cli. | ||
#[derive(PartialEq, Eq, Debug, Deserialize, Serialize, Clone)] | ||
#[serde(default)] | ||
|
@@ -195,4 +255,20 @@ mod tests { | |
|
||
assert_eq!(config.derp_regions.len(), 2); | ||
} | ||
|
||
#[test] | ||
fn test_iroh_paths_parse_roundtrip() { | ||
let kinds = [ | ||
IrohPaths::BaoFlatStoreComplete, | ||
IrohPaths::BaoFlatStorePartial, | ||
IrohPaths::Keypair, | ||
]; | ||
for iroh_path in &kinds { | ||
let root = PathBuf::from("/tmp"); | ||
let path = root.join(iroh_path); | ||
let fname = path.file_name().unwrap().to_str().unwrap(); | ||
let parsed = IrohPaths::from_str(fname).unwrap(); | ||
assert_eq!(*iroh_path, parsed); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would be good to have a basic test for this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added a encode-parse roundtrip test