Refactor build method and flag

This commit is contained in:
mlemesle 2022-09-06 14:50:49 +02:00
parent 403226a029
commit 6520d3c474

View File

@ -4,7 +4,7 @@ use std::path::PathBuf;
use std::sync::Arc; use std::sync::Arc;
use byte_unit::Byte; use byte_unit::Byte;
use clap::{Arg, Command, Parser}; use clap::Parser;
use meilisearch_lib::{ use meilisearch_lib::{
export_to_env_if_not_present, export_to_env_if_not_present,
options::{IndexerOpts, SchedulerConfig}, options::{IndexerOpts, SchedulerConfig},
@ -193,6 +193,12 @@ pub struct Opt {
#[serde(flatten)] #[serde(flatten)]
#[clap(flatten)] #[clap(flatten)]
pub scheduler_options: SchedulerConfig, pub scheduler_options: SchedulerConfig,
/// The path to a configuration file that should be used to setup the engine.
/// Format must be TOML.
#[serde(skip_serializing)]
#[clap(long)]
config_file_path: Option<PathBuf>,
} }
impl Opt { impl Opt {
@ -203,29 +209,21 @@ impl Opt {
} }
pub fn build() -> Self { pub fn build() -> Self {
let args = Command::new("config") let mut opts = Opt::parse();
.arg( if let Some(config_file_path) = opts.config_file_path.as_ref() {
Arg::new("config_file_path") eprintln!("loading config file : {:?}", config_file_path);
.long("config-file-path") match std::fs::read(config_file_path) {
.takes_value(true) Ok(config) => {
.default_value("./config.toml") let opt_from_config =
.help("Path to a config file, must be TOML format"), toml::from_slice::<Opt>(&config).expect("can't read file");
) opt_from_config.export_to_env();
.get_matches(); opts = Opt::parse();
let config_file_path = args }
.value_of("config_file_path") Err(err) => eprintln!("can't read {:?} : {}", config_file_path, err),
.expect("default value present");
if let Some(Ok(opts_from_file)) = match std::fs::read_to_string(config_file_path) {
Ok(config_str) => Some(toml::from_str::<Opt>(&config_str)),
Err(err) => {
log::debug!("can't read {} : {}", config_file_path, err);
None
} }
} {
opts_from_file.export_to_env();
} }
Opt::parse() opts
} }
fn export_to_env(self) { fn export_to_env(self) {