Skip to content

Commit 4f6c296

Browse files
committed
refactor: util config/data paths return Results
1 parent 9e41428 commit 4f6c296

File tree

9 files changed

+38
-26
lines changed

9 files changed

+38
-26
lines changed

iroh-ctl/src/main.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ enum Commands {
5353
async fn main() -> anyhow::Result<()> {
5454
let cli = Cli::parse();
5555

56-
let sources = vec![iroh_config_path(CONFIG_FILE_NAME), cli.cfg.clone()];
56+
let cfg_path = iroh_config_path(CONFIG_FILE_NAME)?;
57+
let sources = vec![Some(cfg_path), cli.cfg.clone()];
5758
let config = make_config(
5859
// default
5960
Config::default(),

iroh-gateway/src/main.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ use tracing::{debug, error};
1717
async fn main() -> Result<()> {
1818
let args = Args::parse();
1919

20-
let sources = vec![iroh_config_path(CONFIG_FILE_NAME), args.cfg.clone()];
20+
let cfg_path = iroh_config_path(CONFIG_FILE_NAME)?;
21+
let sources = vec![Some(cfg_path), args.cfg.clone()];
2122
let mut config = make_config(
2223
// default
2324
Config::default(),

iroh-one/src/main.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ use tracing::{debug, error};
2121
async fn main() -> Result<()> {
2222
let args = Args::parse();
2323

24-
let sources = vec![iroh_config_path(CONFIG_FILE_NAME), args.cfg.clone()];
24+
let cfg_path = iroh_config_path(CONFIG_FILE_NAME)?;
25+
let sources = vec![Some(cfg_path), args.cfg.clone()];
2526
let mut config = make_config(
2627
// default
2728
Config::default(),

iroh-p2p/src/keys.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ impl Keychain<MemoryStorage> {
115115
impl Keychain<DiskStorage> {
116116
/// Creates a new on disk keychain, with the root defaulting to the iroh config directory
117117
pub async fn new() -> Result<Self> {
118-
let root = iroh_config_root().ok_or_else(|| anyhow!("missing iroh configuration path"))?;
118+
let root = iroh_config_root()?;
119119
Self::with_root(root).await
120120
}
121121

iroh-p2p/src/main.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ async fn main() -> anyhow::Result<()> {
1515
let args = Args::parse();
1616

1717
// TODO: configurable network
18-
let sources = vec![iroh_config_path(CONFIG_FILE_NAME), args.cfg.clone()];
18+
let cfg_path = iroh_config_path(CONFIG_FILE_NAME)?;
19+
let sources = vec![Some(cfg_path), args.cfg.clone()];
1920
let network_config = make_config(
2021
// default
2122
Config::default_grpc(),

iroh-store/src/config.rs

+10-6
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@ pub const ENV_PREFIX: &str = "IROH_STORE";
1919

2020
/// the path to data directory. If arg_path is `None`, the default iroh_data_path()/store is used
2121
/// iroh_data_path() returns an operating system-specific directory
22-
/// falls back to an empty path in the unlikely scenario where iroh_data_path() is `None`
23-
/// likely b/c iroh is not running on a Linux/Windows/MacOS system
24-
pub fn config_data_path(arg_path: Option<PathBuf>) -> PathBuf {
25-
arg_path.unwrap_or_else(|| iroh_data_path("store").unwrap_or_else(|| PathBuf::from("")))
22+
pub fn config_data_path(arg_path: Option<PathBuf>) -> Result<PathBuf> {
23+
match arg_path {
24+
Some(p) => Ok(p),
25+
None => iroh_data_path("store"),
26+
}
2627
}
2728

2829
/// The configuration for the store.
@@ -141,10 +142,13 @@ mod tests {
141142
#[test]
142143
fn test_config_data_path() {
143144
let path = PathBuf::new().join("arg_path");
144-
let path_given = config_data_path(Some(path.clone()));
145+
let path_given = config_data_path(Some(path.clone())).expect("config data path error");
145146
assert_eq!(path_given.display().to_string(), path.display().to_string());
146147

147-
let no_path_given = config_data_path(None).display().to_string();
148+
let no_path_given = config_data_path(None)
149+
.expect("config data path error")
150+
.display()
151+
.to_string();
148152
assert!(no_path_given.ends_with("store"));
149153
}
150154
}

iroh-store/src/main.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@ async fn main() -> anyhow::Result<()> {
1515
let version = env!("CARGO_PKG_VERSION");
1616
println!("Starting iroh-store, version {version}");
1717

18-
let sources = vec![iroh_config_path(CONFIG_FILE_NAME), args.cfg.clone()];
18+
let config_path = iroh_config_path(CONFIG_FILE_NAME)?;
19+
let sources = vec![Some(config_path), args.cfg.clone()];
20+
let config_data_path = config_data_path(args.path.clone())?;
1921
let config = make_config(
2022
// default
21-
Config::new_grpc(config_data_path(args.path.clone())),
23+
Config::new_grpc(config_data_path),
2224
// potential config files
2325
sources,
2426
// env var prefix for this config

iroh-util/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ futures = "0.3.21"
1414
anyhow = "1.0.57"
1515
toml = "0.5.9"
1616
serde = { version = "1.0", features = ["derive"] }
17-
dirs = "4.0.0"
1817
config = "0.13.1"
1918
tracing = "0.1.34"
2019
temp-env = "0.2.0"

iroh-util/src/lib.rs

+15-12
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::{
88
},
99
};
1010

11-
use anyhow::Result;
11+
use anyhow::{anyhow, Result};
1212
use cid::{
1313
multihash::{Code, MultihashDigest},
1414
Cid,
@@ -53,15 +53,16 @@ pub async fn block_until_sigint() {
5353
/// | Linux | `$XDG_CONFIG_HOME` or `$HOME`/.config/iroh | /home/alice/.config/iroh |
5454
/// | macOS | `$HOME`/Library/Application Support/iroh | /Users/Alice/Library/Application Support/iroh |
5555
/// | Windows | `{FOLDERID_RoamingAppData}`/iroh | C:\Users\Alice\AppData\Roaming\iroh |
56-
pub fn iroh_config_root() -> Option<PathBuf> {
57-
let cfg = dirs_next::config_dir()?;
58-
Some(cfg.join(&IROH_DIR))
56+
pub fn iroh_config_root() -> Result<PathBuf> {
57+
let cfg = dirs_next::config_dir()
58+
.ok_or_else(|| anyhow!("operating environment provides no directory for configuration"))?;
59+
Ok(cfg.join(&IROH_DIR))
5960
}
6061

6162
// Path that leads to a file in the iroh config directory.
62-
pub fn iroh_config_path(file_name: &str) -> Option<PathBuf> {
63+
pub fn iroh_config_path(file_name: &str) -> Result<PathBuf> {
6364
let path = iroh_config_root()?.join(file_name);
64-
Some(path)
65+
Ok(path)
6566
}
6667

6768
/// Returns the path to the user's iroh data directory.
@@ -73,15 +74,17 @@ pub fn iroh_config_path(file_name: &str) -> Option<PathBuf> {
7374
/// | Linux | `$XDG_DATA_HOME`/iroh or `$HOME`/.local/share/iroh | /home/alice/.local/share/iroh |
7475
/// | macOS | `$HOME`/Library/Application Support/iroh | /Users/Alice/Library/Application Support/iroh |
7576
/// | Windows | `{FOLDERID_RoamingAppData}/iroh` | C:\Users\Alice\AppData\Roaming\iroh |
76-
pub fn iroh_data_root() -> Option<PathBuf> {
77-
let path = dirs_next::data_dir()?;
78-
Some(path.join(&IROH_DIR))
77+
pub fn iroh_data_root() -> Result<PathBuf> {
78+
let path = dirs_next::data_dir().ok_or_else(|| {
79+
anyhow!("operating environment provides no directory for application data")
80+
})?;
81+
Ok(path.join(&IROH_DIR))
7982
}
8083

81-
// Path that leads to a file in the iroh data directory.
82-
pub fn iroh_data_path(file_name: &str) -> Option<PathBuf> {
84+
/// Path that leads to a file in the iroh data directory.
85+
pub fn iroh_data_path(file_name: &str) -> Result<PathBuf> {
8386
let path = iroh_data_root()?.join(file_name);
84-
Some(path)
87+
Ok(path)
8588
}
8689

8790
/// insert a value into a `config::Map`

0 commit comments

Comments
 (0)