Skip to content

Commit d5db4a9

Browse files
committed
fix: Accept config to be through file or env vars
1 parent 00af506 commit d5db4a9

File tree

4 files changed

+18
-14
lines changed

4 files changed

+18
-14
lines changed

config/src/config.rs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,16 @@ impl ConfigPrefix {
6868
}
6969

7070
impl Config {
71-
pub fn parse(prefix: ConfigPrefix, filename: &PathBuf) -> Result<Self, String> {
71+
pub fn parse(prefix: ConfigPrefix, filename: &Option<PathBuf>) -> Result<Self, String> {
7272
let config_defaults = include_str!("../default_values.toml");
7373

74-
let config: ConfigWrapper = Figment::new()
75-
.merge(Toml::string(config_defaults))
76-
.merge(Toml::file(filename))
74+
let mut figment_config = Figment::new().merge(Toml::string(config_defaults));
75+
76+
if let Some(path) = filename {
77+
figment_config = figment_config.merge(Toml::file(path));
78+
}
79+
80+
let config: ConfigWrapper = figment_config
7781
.merge(Env::prefixed(prefix.get_prefix()).split("__"))
7882
.extract()
7983
.map_err(|e| e.to_string())?;
@@ -349,7 +353,7 @@ mod tests {
349353
fn test_minimal_config() {
350354
Config::parse(
351355
ConfigPrefix::Service,
352-
&PathBuf::from("minimal-config-example.toml"),
356+
&Some(PathBuf::from("minimal-config-example.toml")),
353357
)
354358
.unwrap();
355359
}
@@ -359,7 +363,7 @@ mod tests {
359363
// Generate full config by deserializing the minimal config and let the code fill in the defaults.
360364
let max_config = Config::parse(
361365
ConfigPrefix::Service,
362-
&PathBuf::from("minimal-config-example.toml"),
366+
&Some(PathBuf::from("minimal-config-example.toml")),
363367
)
364368
.unwrap();
365369
let max_config_file: Config = toml::from_str(
@@ -381,7 +385,7 @@ mod tests {
381385

382386
Config::parse(
383387
ConfigPrefix::Service,
384-
&PathBuf::from("minimal-config-example.toml"),
388+
&Some(PathBuf::from("minimal-config-example.toml")),
385389
)
386390
.unwrap();
387391

@@ -421,7 +425,7 @@ mod tests {
421425
// This should fail because the subgraphs.network.query_url field is missing
422426
Config::parse(
423427
ConfigPrefix::Service,
424-
&PathBuf::from(temp_minimal_config_path.path()),
428+
&Some(PathBuf::from(temp_minimal_config_path.path())),
425429
)
426430
.unwrap_err();
427431

@@ -430,7 +434,7 @@ mod tests {
430434

431435
let config = Config::parse(
432436
ConfigPrefix::Service,
433-
&PathBuf::from(temp_minimal_config_path.path()),
437+
&Some(PathBuf::from(temp_minimal_config_path.path())),
434438
)
435439
.unwrap();
436440

@@ -448,7 +452,7 @@ mod tests {
448452

449453
let config = Config::parse(
450454
ConfigPrefix::Service,
451-
&PathBuf::from("minimal-config-example.toml"),
455+
&Some(PathBuf::from("minimal-config-example.toml")),
452456
)
453457
.unwrap();
454458

@@ -533,7 +537,7 @@ mod tests {
533537
// Parse the config with new datbase vars
534538
let config = Config::parse(
535539
ConfigPrefix::Service,
536-
&PathBuf::from(temp_minimal_config_path.path()),
540+
&Some(PathBuf::from(temp_minimal_config_path.path())),
537541
)
538542
.unwrap();
539543

service/src/cli.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ pub struct Cli {
1010
/// Path to the configuration file.
1111
/// See https://github.com/graphprotocol/indexer-rs/tree/main/service for examples.
1212
#[arg(long, value_name = "FILE", verbatim_doc_comment)]
13-
pub config: PathBuf,
13+
pub config: Option<PathBuf>,
1414
}

service/src/service.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ pub async fn run() -> anyhow::Result<()> {
133133
MainConfig::parse(indexer_config::ConfigPrefix::Service, &cli.config).map_err(|e| {
134134
error!(
135135
"Invalid configuration file `{}`: {}",
136-
cli.config.display(),
136+
cli.config.unwrap_or_default().display(),
137137
e
138138
);
139139
anyhow!(e)

tap-agent/src/config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub struct Cli {
1717
/// Path to the configuration file.
1818
/// See https://github.com/graphprotocol/indexer-rs/tree/main/tap-agent for examples.
1919
#[arg(long, value_name = "FILE", verbatim_doc_comment)]
20-
pub config: PathBuf,
20+
pub config: Option<PathBuf>,
2121
}
2222

2323
impl From<IndexerConfig> for Config {

0 commit comments

Comments
 (0)