From 5a4f1508d0a09e8a7e5ad123deb8c96bf9f9e413 Mon Sep 17 00:00:00 2001 From: mlemesle Date: Wed, 7 Sep 2022 18:16:33 +0200 Subject: [PATCH] Add documentation --- meilisearch-http/src/option.rs | 8 ++++++++ meilisearch-lib/src/options.rs | 1 + 2 files changed, 9 insertions(+) diff --git a/meilisearch-http/src/option.rs b/meilisearch-http/src/option.rs index c16856b58..3351e8b92 100644 --- a/meilisearch-http/src/option.rs +++ b/meilisearch-http/src/option.rs @@ -242,15 +242,20 @@ impl Opt { !self.no_analytics } + /// Build a new Opt from config file, env vars and cli args. pub fn build() -> Self { + // Parse the args to get the config_file_path. let mut opts = Opt::parse(); if let Some(config_file_path) = opts.config_file_path.as_ref() { eprintln!("loading config file : {:?}", config_file_path); match std::fs::read(config_file_path) { Ok(config) => { + // If the arg is present, and the file successfully read, we deserialize it with `toml`. let opt_from_config = toml::from_slice::(&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(); + // Once injected we parse the cli args once again to take the new env vars into scope. opts = Opt::parse(); } Err(err) => eprintln!("can't read {:?} : {}", config_file_path, err), @@ -260,6 +265,7 @@ impl Opt { opts } + /// Exports the opts values to their corresponding env vars if they are not set. fn export_to_env(self) { export_to_env_if_not_present(MEILI_DB_PATH, self.db_path); export_to_env_if_not_present(MEILI_HTTP_ADDR, self.http_addr); @@ -409,6 +415,8 @@ fn load_ocsp(filename: &Option) -> anyhow::Result> { 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 { PathBuf::from(DEFAULT_DB_PATH) } diff --git a/meilisearch-lib/src/options.rs b/meilisearch-lib/src/options.rs index 4e208187d..5aa7edf37 100644 --- a/meilisearch-lib/src/options.rs +++ b/meilisearch-lib/src/options.rs @@ -59,6 +59,7 @@ pub struct SchedulerConfig { } impl IndexerOpts { + /// Exports the values to their corresponding env vars if they are not set. pub fn export_to_env(self) { if let Some(max_indexing_memory) = self.max_indexing_memory.0 { export_to_env_if_not_present(