From edb234d44f57403cc9a9fc6eeec052c5563156f2 Mon Sep 17 00:00:00 2001 From: Dmitriy Pleshevskiy Date: Mon, 8 Feb 2021 07:28:35 +0300 Subject: [PATCH] feat: add support for custom path ... for config file initialization --- migra-cli/src/config.rs | 23 ++++++++++++++++++----- migra-cli/src/main.rs | 2 +- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/migra-cli/src/config.rs b/migra-cli/src/config.rs index cbf58b0..a0e09fd 100644 --- a/migra-cli/src/config.rs +++ b/migra-cli/src/config.rs @@ -83,17 +83,30 @@ impl Config { } } - pub fn initialize() -> Result<(), Box> { - if Path::new(MIGRA_TOML_FILENAME).exists() { - println!("{} already exists", MIGRA_TOML_FILENAME); + pub fn initialize(config_path: Option) -> Result<(), Box> { + let config_path = config_path + .map(|mut config_path| { + let ext = config_path.extension(); + if config_path.is_dir() || ext.is_none() { + config_path.push(MIGRA_TOML_FILENAME); + } + + config_path + }) + .unwrap_or_else(|| PathBuf::from(MIGRA_TOML_FILENAME)); + + if config_path.exists() { + println!("{} already exists", config_path.to_str().unwrap()); return Ok(()); + } else if let Some(dirs) = config_path.parent() { + fs::create_dir_all(dirs)?; } let config = Config::default(); let content = toml::to_string(&config)?; - fs::write(MIGRA_TOML_FILENAME, content)?; + fs::write(&config_path, content)?; - println!("Created {}", MIGRA_TOML_FILENAME); + println!("Created {}", config_path.to_str().unwrap()); Ok(()) } diff --git a/migra-cli/src/main.rs b/migra-cli/src/main.rs index d734dbc..fbb4ab2 100644 --- a/migra-cli/src/main.rs +++ b/migra-cli/src/main.rs @@ -16,7 +16,7 @@ fn main() -> Result<(), Box> { match opt.command { Command::Init => { - Config::initialize()?; + Config::initialize(opt.config)?; } Command::Apply(ApplyCommandOpt { file_name }) => { let config = Config::read(opt.config)?;