|
| 1 | +use std::{ |
| 2 | + env, fs, |
| 3 | + path::{Path, PathBuf}, |
| 4 | +}; |
| 5 | + |
| 6 | +use anyhow::{Context, Result}; |
| 7 | +use toml::Value; |
| 8 | + |
| 9 | +use crate::{ |
| 10 | + config::{mega_base, template::default_config_template}, |
| 11 | + utils::get_current_bin_name, |
| 12 | +}; |
| 13 | + |
| 14 | +#[derive(Debug, Clone, Copy)] |
| 15 | +pub enum ConfigSource { |
| 16 | + Cli, |
| 17 | + Env, |
| 18 | + Cwd, |
| 19 | + Global, |
| 20 | + DefaultGenerated, |
| 21 | +} |
| 22 | + |
| 23 | +pub struct LoadedConfig { |
| 24 | + pub path: PathBuf, |
| 25 | + pub source: ConfigSource, |
| 26 | +} |
| 27 | + |
| 28 | +#[derive(Debug, Default)] |
| 29 | +pub struct ConfigInput { |
| 30 | + /// CLI --config |
| 31 | + pub cli_path: Option<PathBuf>, |
| 32 | + |
| 33 | + /// ENV: MEGA_CONFIG |
| 34 | + pub env_path: Option<PathBuf>, |
| 35 | +} |
| 36 | + |
| 37 | +pub struct ConfigLoader { |
| 38 | + input: ConfigInput, |
| 39 | +} |
| 40 | + |
| 41 | +impl ConfigLoader { |
| 42 | + pub fn new(input: ConfigInput) -> Self { |
| 43 | + Self { input } |
| 44 | + } |
| 45 | + |
| 46 | + /// Load config path, create default config if not exists |
| 47 | + pub fn load(&self) -> Result<LoadedConfig> { |
| 48 | + if let Some(path) = &self.input.cli_path { |
| 49 | + return Ok(LoadedConfig { |
| 50 | + path: path.clone(), |
| 51 | + source: ConfigSource::Cli, |
| 52 | + }); |
| 53 | + } |
| 54 | + |
| 55 | + if let Some(path) = &self.input.env_path { |
| 56 | + return Ok(LoadedConfig { |
| 57 | + path: path.clone(), |
| 58 | + source: ConfigSource::Env, |
| 59 | + }); |
| 60 | + } |
| 61 | + |
| 62 | + if let Some(path) = Self::cwd_config_path()? { |
| 63 | + return Ok(LoadedConfig { |
| 64 | + path, |
| 65 | + source: ConfigSource::Cwd, |
| 66 | + }); |
| 67 | + } |
| 68 | + |
| 69 | + if let Some(path) = Self::global_config_path()? { |
| 70 | + return Ok(LoadedConfig { |
| 71 | + path, |
| 72 | + source: ConfigSource::Global, |
| 73 | + }); |
| 74 | + } |
| 75 | + |
| 76 | + let path = self.create_default_config()?; |
| 77 | + Ok(LoadedConfig { |
| 78 | + path, |
| 79 | + source: ConfigSource::DefaultGenerated, |
| 80 | + }) |
| 81 | + } |
| 82 | + |
| 83 | + fn cwd_config_path() -> Result<Option<PathBuf>> { |
| 84 | + let cwd = env::current_dir().context("failed to get current dir")?; |
| 85 | + let path = cwd.join("config/config.toml"); |
| 86 | + Ok(path.exists().then_some(path)) |
| 87 | + } |
| 88 | + |
| 89 | + fn global_config_path() -> Result<Option<PathBuf>> { |
| 90 | + let path = mega_base().join("etc/config.toml"); |
| 91 | + Ok(path.exists().then_some(path)) |
| 92 | + } |
| 93 | + |
| 94 | + fn create_default_config(&self) -> Result<PathBuf> { |
| 95 | + let base_dir = mega_base(); |
| 96 | + let etc_dir = base_dir.join("etc"); |
| 97 | + fs::create_dir_all(&etc_dir).with_context(|| format!("failed to create {:?}", etc_dir))?; |
| 98 | + |
| 99 | + let bin_name = get_current_bin_name(); |
| 100 | + let template = default_config_template(&bin_name) |
| 101 | + .with_context(|| format!("no default config template for binary `{}`", bin_name))?; |
| 102 | + |
| 103 | + let config = Self::render_template(template, &base_dir)?; |
| 104 | + let config_path = etc_dir.join("config.toml"); |
| 105 | + |
| 106 | + fs::write(&config_path, config) |
| 107 | + .with_context(|| format!("failed to write {:?}", config_path))?; |
| 108 | + |
| 109 | + eprintln!( |
| 110 | + "config.toml not found, created default config at {:?}", |
| 111 | + config_path |
| 112 | + ); |
| 113 | + |
| 114 | + Ok(config_path) |
| 115 | + } |
| 116 | + |
| 117 | + fn render_template(template: &str, base_dir: &Path) -> Result<String> { |
| 118 | + let mut value: Value = |
| 119 | + toml::from_str(template).context("failed to parse default config template")?; |
| 120 | + |
| 121 | + value["base_dir"] = Value::String(base_dir.to_string_lossy().into()); |
| 122 | + |
| 123 | + toml::to_string_pretty(&value).context("failed to serialize default config") |
| 124 | + } |
| 125 | +} |
0 commit comments