Archived
1
0
Fork 0

feat: add support for custom path

... for config file initialization
This commit is contained in:
Dmitriy Pleshevskiy 2021-02-08 07:28:35 +03:00
parent 032e1b7287
commit edb234d44f
2 changed files with 19 additions and 6 deletions

View file

@ -83,17 +83,30 @@ impl Config {
} }
} }
pub fn initialize() -> Result<(), Box<dyn std::error::Error>> { pub fn initialize(config_path: Option<PathBuf>) -> Result<(), Box<dyn std::error::Error>> {
if Path::new(MIGRA_TOML_FILENAME).exists() { let config_path = config_path
println!("{} already exists", MIGRA_TOML_FILENAME); .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(()); return Ok(());
} else if let Some(dirs) = config_path.parent() {
fs::create_dir_all(dirs)?;
} }
let config = Config::default(); let config = Config::default();
let content = toml::to_string(&config)?; 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(()) Ok(())
} }

View file

@ -16,7 +16,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
match opt.command { match opt.command {
Command::Init => { Command::Init => {
Config::initialize()?; Config::initialize(opt.config)?;
} }
Command::Apply(ApplyCommandOpt { file_name }) => { Command::Apply(ApplyCommandOpt { file_name }) => {
let config = Config::read(opt.config)?; let config = Config::read(opt.config)?;