feat(cli): add toml config
- [x] init migra.toml with serialized default config parameters - [x] read and desirealize config from migra.toml
This commit is contained in:
parent
2811b8b293
commit
ac27bfc4dd
3 changed files with 50 additions and 2 deletions
|
@ -9,3 +9,5 @@ edition = "2018"
|
|||
[dependencies]
|
||||
migra-core = { path = '../migra-core' }
|
||||
structopt = "0.3.21"
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
toml = "0.5.8"
|
||||
|
|
39
migra-cli/src/config.rs
Normal file
39
migra-cli/src/config.rs
Normal file
|
@ -0,0 +1,39 @@
|
|||
use serde::{Deserialize, Serialize};
|
||||
use std::fs;
|
||||
use std::path::Path;
|
||||
|
||||
const MIGRA_TOML_FILENAME: &str = "migra.toml";
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub(crate) struct Config {
|
||||
directory: String,
|
||||
}
|
||||
|
||||
impl Default for Config {
|
||||
fn default() -> Config {
|
||||
Config {
|
||||
directory: String::from("database"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Config {
|
||||
pub fn read() -> Config {
|
||||
fs::read_to_string(MIGRA_TOML_FILENAME)
|
||||
.ok()
|
||||
.and_then(|content| toml::from_str(&content).ok())
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
pub fn initialize() -> Result<(), Box<dyn std::error::Error>> {
|
||||
if Path::new(MIGRA_TOML_FILENAME).exists() {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let config = Config::default();
|
||||
let content = toml::to_string(&config)?;
|
||||
fs::write(MIGRA_TOML_FILENAME, content)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
|
@ -1,16 +1,23 @@
|
|||
#![deny(clippy::all)]
|
||||
|
||||
mod config;
|
||||
mod opts;
|
||||
|
||||
use config::Config;
|
||||
use opts::{AppOpt, StructOpt};
|
||||
|
||||
fn main() {
|
||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let opt = AppOpt::from_args();
|
||||
dbg!(&opt);
|
||||
|
||||
let config = Config::read();
|
||||
dbg!(&config);
|
||||
|
||||
match opt {
|
||||
AppOpt::Init => {
|
||||
println!("unimplemented");
|
||||
Config::initialize()?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
Reference in a new issue