Skip to content

Commit

Permalink
Move config to a separate crate for reuse.
Browse files Browse the repository at this point in the history
  • Loading branch information
John-LittleBearLabs committed Aug 11, 2023
1 parent 454858c commit f1f30eb
Show file tree
Hide file tree
Showing 19 changed files with 549 additions and 94 deletions.
93 changes: 71 additions & 22 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ blake2 = { version = "0.10.6", default-features = false }
bytes = "1.1"
cid = { version = "0.9", default-features = false }
clap = { version = "4.0.15", features = ["derive"] }
derive-error = "0.0.5"
env_logger = "0.10.0"
figment = { version = "0.10", features = ["toml"] }
file-hashing = "0.1.1"
Expand Down
2 changes: 1 addition & 1 deletion config/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ dirs = "5.0.1"
figment.workspace = true
log.workspace = true
serde.workspace = true
transports.workspace = true
transports.workspace = true
18 changes: 14 additions & 4 deletions config/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use figment::{
};
use log::debug;
use serde::{Deserialize, Serialize};
use std::path::PathBuf;
use transports::MAX_MTU;

#[derive(Debug, Deserialize, Serialize, Clone)]
Expand Down Expand Up @@ -39,7 +40,7 @@ impl Default for Config {
// Default retry timeout of 120_000 ms = 120 s = 2 minutes
retry_timeout_duration: 120_000,
// Default storage dir
storage_path: "storage".to_string(),
storage_path: default_storage_path(),
// Default MTU appropriate for dev radio
// Maxes out at 1024 * 3 bytes
mtu: 512,
Expand All @@ -55,8 +56,17 @@ impl Default for Config {
}
}
}

fn default_path() -> Option<String> {
fn default_storage_path() -> String {
dirs::cache_dir()
.and_then(|d: PathBuf| {
d.join("myceli")
.into_os_string()
.to_str()
.map(|s| s.to_owned())
})
.unwrap_or_else(|| "storage".to_owned())
}
fn default_config_path() -> Option<String> {
if let Some(d) = dirs::config_dir() {
let f = d.join("myceli").join("myceli.toml");
if f.is_file() {
Expand All @@ -68,7 +78,7 @@ fn default_path() -> Option<String> {
impl Config {
pub fn parse(path: Option<String>) -> Result<Self> {
let mut config = Figment::from(Serialized::defaults(Config::default()));
if let Some(path) = path.or(default_path()) {
if let Some(path) = path.or(default_config_path()) {
let toml_values = Toml::file(&path);
debug!("Config values in file {}: {:?}", &path, toml_values.data());
config = config.merge(toml_values);
Expand Down
1 change: 1 addition & 0 deletions controller/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ impl Cli {
match transport.receive() {
Ok((msg, _)) => {
info!("Received: {msg:?}");
println!("Received: {msg:?}");
return Ok(());
}
Err(e) => bail!("{e:?}"),
Expand Down
Loading

0 comments on commit f1f30eb

Please sign in to comment.