Skip to content

Commit 73eef03

Browse files
Merge branch 'graphprotocol:main' into main
2 parents 5aab1a8 + 9e44ad4 commit 73eef03

File tree

4 files changed

+35
-23
lines changed

4 files changed

+35
-23
lines changed

config/src/config.rs

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -70,17 +70,19 @@ impl ConfigPrefix {
7070
}
7171

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

76-
let mut config_content = std::fs::read_to_string(filename)
77-
.map_err(|e| format!("Failed to read config file: {}", e))?;
76+
let mut figment_config = Figment::new().merge(Toml::string(config_defaults));
7877

79-
config_content = Self::substitute_env_vars(config_content)?;
78+
if let Some(path) = filename {
79+
let mut config_content = std::fs::read_to_string(path)
80+
.map_err(|e| format!("Failed to read config file: {}", e))?;
81+
config_content = Self::substitute_env_vars(config_content)?;
82+
figment_config = figment_config.merge(Toml::string(&config_content));
83+
}
8084

81-
let config: ConfigWrapper = Figment::new()
82-
.merge(Toml::string(config_defaults))
83-
.merge(Toml::string(&config_content))
85+
let config: ConfigWrapper = figment_config
8486
.merge(Env::prefixed(prefix.get_prefix()).split("__"))
8587
.extract()
8688
.map_err(|e| e.to_string())?;
@@ -386,7 +388,7 @@ mod tests {
386388
fn test_minimal_config() {
387389
Config::parse(
388390
ConfigPrefix::Service,
389-
&PathBuf::from("minimal-config-example.toml"),
391+
Some(PathBuf::from("minimal-config-example.toml")).as_ref(),
390392
)
391393
.unwrap();
392394
}
@@ -396,7 +398,7 @@ mod tests {
396398
// Generate full config by deserializing the minimal config and let the code fill in the defaults.
397399
let max_config = Config::parse(
398400
ConfigPrefix::Service,
399-
&PathBuf::from("minimal-config-example.toml"),
401+
Some(PathBuf::from("minimal-config-example.toml")).as_ref(),
400402
)
401403
.unwrap();
402404
let max_config_file: Config = toml::from_str(
@@ -418,7 +420,7 @@ mod tests {
418420

419421
Config::parse(
420422
ConfigPrefix::Service,
421-
&PathBuf::from("minimal-config-example.toml"),
423+
Some(PathBuf::from("minimal-config-example.toml")).as_ref(),
422424
)
423425
.unwrap();
424426

@@ -458,7 +460,7 @@ mod tests {
458460
// This should fail because the subgraphs.network.query_url field is missing
459461
Config::parse(
460462
ConfigPrefix::Service,
461-
&PathBuf::from(temp_minimal_config_path.path()),
463+
Some(PathBuf::from(temp_minimal_config_path.path())).as_ref(),
462464
)
463465
.unwrap_err();
464466

@@ -467,7 +469,7 @@ mod tests {
467469

468470
let config = Config::parse(
469471
ConfigPrefix::Service,
470-
&PathBuf::from(temp_minimal_config_path.path()),
472+
Some(PathBuf::from(temp_minimal_config_path.path())).as_ref(),
471473
)
472474
.unwrap();
473475

@@ -485,7 +487,7 @@ mod tests {
485487

486488
let config = Config::parse(
487489
ConfigPrefix::Service,
488-
&PathBuf::from("minimal-config-example.toml"),
490+
Some(PathBuf::from("minimal-config-example.toml")).as_ref(),
489491
)
490492
.unwrap();
491493

@@ -569,7 +571,7 @@ mod tests {
569571
// This should fail because the QUERY_URL env variable is not setup
570572
Config::parse(
571573
ConfigPrefix::Service,
572-
&PathBuf::from(temp_minimal_config_path.path()),
574+
Some(PathBuf::from(temp_minimal_config_path.path())).as_ref(),
573575
)
574576
.unwrap_err();
575577

@@ -578,7 +580,7 @@ mod tests {
578580

579581
let config = Config::parse(
580582
ConfigPrefix::Service,
581-
&PathBuf::from(temp_minimal_config_path.path()),
583+
Some(PathBuf::from(temp_minimal_config_path.path())).as_ref(),
582584
)
583585
.unwrap();
584586

@@ -663,7 +665,7 @@ mod tests {
663665
// Parse the config with new datbase vars
664666
let config = Config::parse(
665667
ConfigPrefix::Service,
666-
&PathBuf::from(temp_minimal_config_path.path()),
668+
Some(PathBuf::from(temp_minimal_config_path.path())).as_ref(),
667669
)
668670
.unwrap();
669671

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: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,11 +129,12 @@ pub async fn run() -> anyhow::Result<()> {
129129
// Load the json-rpc service configuration, which is a combination of the
130130
// general configuration options for any indexer service and specific
131131
// options added for JSON-RPC
132-
let config =
133-
MainConfig::parse(indexer_config::ConfigPrefix::Service, &cli.config).map_err(|e| {
132+
let config = MainConfig::parse(indexer_config::ConfigPrefix::Service, cli.config.as_ref())
133+
.map_err(|e| {
134134
error!(
135-
"Invalid configuration file `{}`: {}",
136-
cli.config.display(),
135+
"Invalid configuration file `{}`: {}, if a value is missing you can also use \
136+
--config to fill the rest of the values",
137+
cli.config.unwrap_or_default().display(),
137138
e
138139
);
139140
anyhow!(e)

tap-agent/src/config.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use indexer_config::{Config as IndexerConfig, ConfigPrefix};
66
use reqwest::Url;
77
use std::path::PathBuf;
88
use std::{collections::HashMap, str::FromStr};
9+
use tracing::error;
910

1011
use anyhow::Result;
1112
use thegraph_core::{Address, DeploymentId};
@@ -17,7 +18,7 @@ pub struct Cli {
1718
/// Path to the configuration file.
1819
/// See https://github.com/graphprotocol/indexer-rs/tree/main/tap-agent for examples.
1920
#[arg(long, value_name = "FILE", verbatim_doc_comment)]
20-
pub config: PathBuf,
21+
pub config: Option<PathBuf>,
2122
}
2223

2324
impl From<IndexerConfig> for Config {
@@ -182,7 +183,15 @@ impl Config {
182183
pub fn from_cli() -> Result<Self> {
183184
let cli = Cli::parse();
184185
let indexer_config =
185-
IndexerConfig::parse(ConfigPrefix::Tap, &cli.config).map_err(|e| anyhow::anyhow!(e))?;
186+
IndexerConfig::parse(ConfigPrefix::Tap, cli.config.as_ref()).map_err(|e| {
187+
error!(
188+
"Invalid configuration file `{}`: {}, if a value is missing you can also use \
189+
--config to fill the rest of the values",
190+
cli.config.unwrap_or_default().display(),
191+
e
192+
);
193+
anyhow::anyhow!(e)
194+
})?;
186195
let config: Config = indexer_config.into();
187196

188197
// Enables tracing under RUST_LOG variable

0 commit comments

Comments
 (0)