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>> {
if Path::new(MIGRA_TOML_FILENAME).exists() {
println!("{} already exists", MIGRA_TOML_FILENAME);
pub fn initialize(config_path: Option<PathBuf>) -> Result<(), Box<dyn std::error::Error>> {
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(())
}

View file

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