Archived
1
0
Fork 0

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:
Dmitriy Pleshevskiy 2021-01-31 03:23:43 +03:00
parent 2811b8b293
commit ac27bfc4dd
3 changed files with 50 additions and 2 deletions

View file

@ -9,3 +9,5 @@ edition = "2018"
[dependencies] [dependencies]
migra-core = { path = '../migra-core' } migra-core = { path = '../migra-core' }
structopt = "0.3.21" structopt = "0.3.21"
serde = { version = "1.0", features = ["derive"] }
toml = "0.5.8"

39
migra-cli/src/config.rs Normal file
View 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(())
}
}

View file

@ -1,16 +1,23 @@
#![deny(clippy::all)] #![deny(clippy::all)]
mod config;
mod opts; mod opts;
use config::Config;
use opts::{AppOpt, StructOpt}; use opts::{AppOpt, StructOpt};
fn main() { fn main() -> Result<(), Box<dyn std::error::Error>> {
let opt = AppOpt::from_args(); let opt = AppOpt::from_args();
dbg!(&opt); dbg!(&opt);
let config = Config::read();
dbg!(&config);
match opt { match opt {
AppOpt::Init => { AppOpt::Init => {
println!("unimplemented"); Config::initialize()?;
} }
} }
Ok(())
} }