Add documentation

This commit is contained in:
mlemesle 2022-09-07 18:16:33 +02:00
parent 135499f398
commit 5a4f1508d0
2 changed files with 9 additions and 0 deletions

View File

@ -242,15 +242,20 @@ impl Opt {
!self.no_analytics !self.no_analytics
} }
/// Build a new Opt from config file, env vars and cli args.
pub fn build() -> Self { pub fn build() -> Self {
// Parse the args to get the config_file_path.
let mut opts = Opt::parse(); let mut opts = Opt::parse();
if let Some(config_file_path) = opts.config_file_path.as_ref() { if let Some(config_file_path) = opts.config_file_path.as_ref() {
eprintln!("loading config file : {:?}", config_file_path); eprintln!("loading config file : {:?}", config_file_path);
match std::fs::read(config_file_path) { match std::fs::read(config_file_path) {
Ok(config) => { Ok(config) => {
// If the arg is present, and the file successfully read, we deserialize it with `toml`.
let opt_from_config = let opt_from_config =
toml::from_slice::<Opt>(&config).expect("can't read file"); toml::from_slice::<Opt>(&config).expect("can't read file");
// We inject the values from the toml in the corresponding env vars if needs be. Doing so, we respect the priority toml < env vars < cli args.
opt_from_config.export_to_env(); opt_from_config.export_to_env();
// Once injected we parse the cli args once again to take the new env vars into scope.
opts = Opt::parse(); opts = Opt::parse();
} }
Err(err) => eprintln!("can't read {:?} : {}", config_file_path, err), Err(err) => eprintln!("can't read {:?} : {}", config_file_path, err),
@ -260,6 +265,7 @@ impl Opt {
opts opts
} }
/// Exports the opts values to their corresponding env vars if they are not set.
fn export_to_env(self) { fn export_to_env(self) {
export_to_env_if_not_present(MEILI_DB_PATH, self.db_path); export_to_env_if_not_present(MEILI_DB_PATH, self.db_path);
export_to_env_if_not_present(MEILI_HTTP_ADDR, self.http_addr); export_to_env_if_not_present(MEILI_HTTP_ADDR, self.http_addr);
@ -409,6 +415,8 @@ fn load_ocsp(filename: &Option<PathBuf>) -> anyhow::Result<Vec<u8>> {
Ok(ret) Ok(ret)
} }
/// Functions used to get default value for `Opt` fields, needs to be function because of serde's default attribute.
fn default_db_path() -> PathBuf { fn default_db_path() -> PathBuf {
PathBuf::from(DEFAULT_DB_PATH) PathBuf::from(DEFAULT_DB_PATH)
} }

View File

@ -59,6 +59,7 @@ pub struct SchedulerConfig {
} }
impl IndexerOpts { impl IndexerOpts {
/// Exports the values to their corresponding env vars if they are not set.
pub fn export_to_env(self) { pub fn export_to_env(self) {
if let Some(max_indexing_memory) = self.max_indexing_memory.0 { if let Some(max_indexing_memory) = self.max_indexing_memory.0 {
export_to_env_if_not_present( export_to_env_if_not_present(