11//! Configuration for the iroh CLI.
22
33use std:: {
4- env,
54 net:: SocketAddr ,
65 path:: { Path , PathBuf } ,
7- str:: FromStr ,
86 sync:: Arc ,
97 time:: Duration ,
108} ;
119
12- use anyhow:: { anyhow , Result } ;
10+ use anyhow:: Result ;
1311use iroh:: {
1412 net:: { RelayMap , RelayNode } ,
1513 node:: GcPolicy ,
1614} ;
15+ use iroh_node_util:: { config:: config_root, logging:: env_file_rust_log} ;
1716use serde:: Deserialize ;
1817
19- const ENV_CONFIG_DIR : & str = "IROH_CONFIG_DIR" ;
20- const ENV_FILE_RUST_LOG : & str = "IROH_FILE_RUST_LOG" ;
18+ /// BIN_NAME is the name of the binary. This is used in various places, e.g. for the home directory
19+ /// and for environment variables.
20+ pub ( crate ) const BIN_NAME : & str = "iroh" ;
2121
2222/// CONFIG_FILE_NAME is the name of the optional config file located in the iroh home directory
2323pub ( crate ) const CONFIG_FILE_NAME : & str = "iroh.config.toml" ;
@@ -52,7 +52,7 @@ pub(crate) struct NodeConfig {
5252 /// Bind address on which to serve Prometheus metrics
5353 pub ( crate ) metrics_addr : Option < SocketAddr > ,
5454 /// Configuration for the logfile.
55- pub ( crate ) file_logs : super :: logging:: FileLogging ,
55+ pub ( crate ) file_logs : iroh_node_util :: logging:: FileLogging ,
5656 /// Path to dump metrics to in CSV format.
5757 pub ( crate ) metrics_dump_path : Option < PathBuf > ,
5858}
@@ -81,7 +81,7 @@ impl NodeConfig {
8181 /// default config file will be loaded. If that is not present the default config will
8282 /// be used.
8383 pub ( crate ) async fn load ( file : Option < & Path > ) -> Result < NodeConfig > {
84- let default_config = iroh_config_path ( CONFIG_FILE_NAME ) ? ;
84+ let default_config = config_root ( BIN_NAME ) ? . join ( CONFIG_FILE_NAME ) ;
8585
8686 let config_file = match file {
8787 Some ( file) => Some ( file) ,
@@ -101,7 +101,7 @@ impl NodeConfig {
101101 } ;
102102
103103 // override from env var
104- if let Some ( env_filter) = env_file_rust_log ( ) . transpose ( ) ? {
104+ if let Some ( env_filter) = env_file_rust_log ( BIN_NAME ) . transpose ( ) ? {
105105 config. file_logs . rust_log = env_filter;
106106 }
107107 Ok ( config)
@@ -144,112 +144,17 @@ impl From<GcPolicyConfig> for GcPolicy {
144144 }
145145}
146146
147- /// Parse [`ENV_FILE_RUST_LOG`] as [`tracing_subscriber::EnvFilter`]. Returns `None` if not
148- /// present.
149- fn env_file_rust_log ( ) -> Option < Result < crate :: logging:: EnvFilter > > {
150- match env:: var ( ENV_FILE_RUST_LOG ) {
151- Ok ( s) => Some ( crate :: logging:: EnvFilter :: from_str ( & s) . map_err ( Into :: into) ) ,
152- Err ( e) => match e {
153- env:: VarError :: NotPresent => None ,
154- e @ env:: VarError :: NotUnicode ( _) => Some ( Err ( e. into ( ) ) ) ,
155- } ,
156- }
157- }
158-
159- /// Name of directory that wraps all iroh files in a given application directory
160- const IROH_DIR : & str = "iroh" ;
161-
162- /// Returns the path to the user's iroh config directory.
163- ///
164- /// If the `IROH_CONFIG_DIR` environment variable is set it will be used unconditionally.
165- /// Otherwise the returned value depends on the operating system according to the following
166- /// table.
167- ///
168- /// | Platform | Value | Example |
169- /// | -------- | ------------------------------------- | -------------------------------- |
170- /// | Linux | `$XDG_CONFIG_HOME` or `$HOME`/.config/iroh | /home/alice/.config/iroh |
171- /// | macOS | `$HOME`/Library/Application Support/iroh | /Users/Alice/Library/Application Support/iroh |
172- /// | Windows | `{FOLDERID_RoamingAppData}`/iroh | C:\Users\Alice\AppData\Roaming\iroh |
173- pub ( crate ) fn iroh_config_root ( ) -> Result < PathBuf > {
174- if let Some ( val) = env:: var_os ( ENV_CONFIG_DIR ) {
175- return Ok ( PathBuf :: from ( val) ) ;
176- }
177- let cfg = dirs_next:: config_dir ( )
178- . ok_or_else ( || anyhow ! ( "operating environment provides no directory for configuration" ) ) ?;
179- Ok ( cfg. join ( IROH_DIR ) )
180- }
181-
182- /// Path that leads to a file in the iroh config directory.
183- pub ( crate ) fn iroh_config_path ( file_name : impl AsRef < Path > ) -> Result < PathBuf > {
184- let path = iroh_config_root ( ) ?. join ( file_name) ;
185- Ok ( path)
186- }
187-
188- /// Returns the path to the user's iroh data directory.
189- ///
190- /// If the `IROH_DATA_DIR` environment variable is set it will be used unconditionally.
191- /// Otherwise the returned value depends on the operating system according to the following
192- /// table.
193- ///
194- /// | Platform | Value | Example |
195- /// | -------- | --------------------------------------------- | ---------------------------------------- |
196- /// | Linux | `$XDG_DATA_HOME`/iroh or `$HOME`/.local/share/iroh | /home/alice/.local/share/iroh |
197- /// | macOS | `$HOME`/Library/Application Support/iroh | /Users/Alice/Library/Application Support/iroh |
198- /// | Windows | `{FOLDERID_RoamingAppData}/iroh` | C:\Users\Alice\AppData\Roaming\iroh |
199- pub ( crate ) fn iroh_data_root ( ) -> Result < PathBuf > {
200- let path = if let Some ( val) = env:: var_os ( "IROH_DATA_DIR" ) {
201- PathBuf :: from ( val)
202- } else {
203- let path = dirs_next:: data_dir ( ) . ok_or_else ( || {
204- anyhow ! ( "operating environment provides no directory for application data" )
205- } ) ?;
206- path. join ( IROH_DIR )
207- } ;
208- let path = if !path. is_absolute ( ) {
209- std:: env:: current_dir ( ) ?. join ( path)
210- } else {
211- path
212- } ;
213- Ok ( path)
214- }
215-
216- /// Returns the path to the user's iroh cache directory.
217- ///
218- /// If the `IROH_CACHE_DIR` environment variable is set it will be used unconditionally.
219- /// Otherwise the returned value depends on the operating system according to the following
220- /// table.
221- ///
222- /// | Platform | Value | Example |
223- /// | -------- | --------------------------------------------- | ---------------------------------------- |
224- /// | Linux | `$XDG_CACHE_HOME`/iroh or `$HOME`/.cache/iroh | /home/.cache/iroh |
225- /// | macOS | `$HOME`/Library/Caches/iroh | /Users/Alice/Library/Caches/iroh |
226- /// | Windows | `{FOLDERID_LocalAppData}/iroh` | C:\Users\Alice\AppData\Roaming\iroh |
227- #[ allow( dead_code) ]
228- pub ( crate ) fn iroh_cache_root ( ) -> Result < PathBuf > {
229- if let Some ( val) = env:: var_os ( "IROH_CACHE_DIR" ) {
230- return Ok ( PathBuf :: from ( val) ) ;
231- }
232- let path = dirs_next:: cache_dir ( ) . ok_or_else ( || {
233- anyhow ! ( "operating environment provides no directory for application data" )
234- } ) ?;
235- Ok ( path. join ( IROH_DIR ) )
236- }
237-
238- /// Path that leads to a file in the iroh cache directory.
239- #[ allow( dead_code) ]
240- pub ( crate ) fn iroh_cache_path ( file_name : & Path ) -> Result < PathBuf > {
241- let path = iroh_cache_root ( ) ?. join ( file_name) ;
242- Ok ( path)
243- }
244-
245147#[ cfg( test) ]
246148mod tests {
247- use std:: net:: { Ipv4Addr , Ipv6Addr } ;
149+ use std:: {
150+ net:: { Ipv4Addr , Ipv6Addr } ,
151+ str:: FromStr ,
152+ } ;
248153
154+ use iroh_node_util:: logging:: { EnvFilter , Rotation } ;
249155 use url:: Url ;
250156
251157 use super :: * ;
252- use crate :: logging:: { EnvFilter , Rotation } ;
253158
254159 #[ test]
255160 fn test_toml_invalid_field ( ) {
0 commit comments