Skip to content
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

Connor/default config location #1494

Merged
merged 11 commits into from
Mar 25, 2022
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions forest/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ ticker = "0.1"
byte-unit = "4.0"
rug = "1.13"
toml = "0.5"
directories = "4.0.1"

[dependencies.jsonrpc-v2]
version = "0.10"
Expand Down
64 changes: 53 additions & 11 deletions forest/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ pub(super) use self::sync_cmd::SyncCommands;
pub(super) use self::wallet_cmd::WalletCommands;

use byte_unit::Byte;
use directories::ProjectDirs;
use fil_types::FILECOIN_PRECISION;
use jsonrpc_v2::Error as JsonRpcError;
use log::{info, warn};
use num_bigint::BigInt;
use rug::float::ParseFloatError;
use rug::Float;
Expand Down Expand Up @@ -158,17 +160,10 @@ impl CliOpts {
// Parse and return the configuration file
read_toml(&toml)?
}
None => {
// Check ENV VAR for config file
if let Ok(config_file) = std::env::var("FOREST_CONFIG_PATH") {
// Read from config file
let toml = read_file_to_string(&PathBuf::from(&config_file))?;
// Parse and return the configuration file
read_toml(&toml)?
} else {
Config::default()
}
}
None => match find_default_config() {
Some(cfg) => cfg,
None => Config::default(),
},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
None => match find_default_config() {
Some(cfg) => cfg,
None => Config::default(),
},
None => find_default_config().unwrap_or_default()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that's a good idea, will sneak it in now

};
if let Some(genesis_file) = &self.genesis {
cfg.genesis_file = Some(genesis_file.to_owned());
Expand Down Expand Up @@ -224,6 +219,53 @@ impl CliOpts {
}
}

fn find_default_config() -> Option<Config> {
if let Ok(config_file) = std::env::var("FOREST_CONFIG_PATH") {
info!(
"FOREST_CONFIG_PATH is set! Using configuration at {}",
config_file
);
let path = PathBuf::from(config_file);
if path.exists() {
return read_config_or_none(path);
}
};

if let Some(dir) = ProjectDirs::from("com", "ChainSafe", "Forest") {
let mut config_dir = dir.config_dir().to_path_buf();
config_dir.push("/forest/config.toml");
lemmih marked this conversation as resolved.
Show resolved Hide resolved
if config_dir.exists() {
info!("Found config file at {}", config_dir.display());
return read_config_or_none(config_dir);
}
}

warn!("No configuration found! Using default");

None
}

fn read_config_or_none(path: PathBuf) -> Option<Config> {
let toml = match read_file_to_string(&path) {
Ok(t) => t,
Err(e) => {
warn!("An error occured while reading configuration file at {}. Resorting to default configuration. Error was {}", path.display(), e);
return None;
}
};

match read_toml(&toml) {
Ok(cfg) => Some(cfg),
Err(e) => {
warn!(
"Error reading configuration, opting to default. Error was {} ",
e
);
None
}
}
}

/// Blocks current thread until ctrl-c is received
pub async fn block_until_sigint() {
let (ctrlc_send, ctrlc_oneshot) = futures::channel::oneshot::channel();
Expand Down