Archived
1
0
Fork 0

feat(cli): add config path option

This commit is contained in:
Dmitriy Pleshevskiy 2021-02-02 00:53:33 +03:00
parent 02f49a5d67
commit d175bfa8f0
3 changed files with 63 additions and 29 deletions

View file

@ -31,8 +31,7 @@ impl Default for Config {
} }
} }
impl Config { fn recursive_find_config_file() -> io::Result<PathBuf> {
pub fn read() -> io::Result<Config> {
let current_dir = std::env::current_dir()?; let current_dir = std::env::current_dir()?;
let mut read_dir = Some(current_dir.as_path()); let mut read_dir = Some(current_dir.as_path());
@ -45,16 +44,33 @@ impl Config {
continue; continue;
} }
let content = fs::read_to_string(migra_file_path)?; return Ok(migra_file_path);
let mut config: Config = toml::from_str(&content).expect("Cannot parse Migra.toml");
config.root = PathBuf::from(dir);
return Ok(config);
} else { } else {
return Err(io::Error::from(io::ErrorKind::NotFound)); return Err(io::Error::from(io::ErrorKind::NotFound));
} }
} }
}
impl Config {
pub fn read(config_path: Option<PathBuf>) -> io::Result<Config> {
let config_path = match config_path {
Some(mut config_path) if config_path.is_dir() => {
config_path.push(MIGRA_TOML_FILENAME);
config_path
},
Some(config_path) => config_path,
None => recursive_find_config_file()?,
};
let content = fs::read_to_string(&config_path)?;
let mut config: Config = toml::from_str(&content).expect("Cannot parse Migra.toml");
config.root = config_path
.parent()
.unwrap_or_else(|| Path::new(""))
.to_path_buf();
Ok(config)
} }
pub fn initialize() -> Result<(), Box<dyn std::error::Error>> { pub fn initialize() -> Result<(), Box<dyn std::error::Error>> {

View file

@ -4,18 +4,18 @@ mod config;
mod opts; mod opts;
use config::Config; use config::Config;
use opts::{AppOpt, ApplyOpt, StructOpt}; use opts::{AppOpt, ApplyCommandOpt, Command, StructOpt};
use std::fs; use std::fs;
fn main() -> Result<(), Box<dyn std::error::Error>> { fn main() -> Result<(), Box<dyn std::error::Error>> {
let opt = AppOpt::from_args(); let opt = AppOpt::from_args();
match opt { match opt.command {
AppOpt::Init => { Command::Init => {
Config::initialize()?; Config::initialize()?;
} }
AppOpt::Apply(ApplyOpt { file_name }) => { Command::Apply(ApplyCommandOpt { file_name }) => {
let config = Config::read()?; let config = Config::read(opt.config)?;
let mut client = migra_core::database::connect(&config.database.connection)?; let mut client = migra_core::database::connect(&config.database.connection)?;
@ -36,6 +36,9 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
} }
} }
} }
Command::List => {
unimplemented!();
}
} }
Ok(()) Ok(())

View file

@ -1,13 +1,28 @@
use std::path::PathBuf;
pub use structopt::StructOpt; pub use structopt::StructOpt;
#[derive(Debug, StructOpt)] #[derive(Debug, StructOpt)]
pub(crate) enum AppOpt { #[structopt(bin_name = "migra", name = "Migra")]
Init, pub(crate) struct AppOpt {
Apply(ApplyOpt), #[structopt(short, long)]
pub config: Option<PathBuf>,
#[structopt(subcommand)]
pub command: Command,
} }
#[derive(Debug, StructOpt)] #[derive(Debug, StructOpt)]
pub(crate) struct ApplyOpt { pub(crate) enum Command {
Init,
Apply(ApplyCommandOpt),
#[structopt(name = "list", visible_alias = "ls")]
List,
}
#[derive(Debug, StructOpt)]
pub(crate) struct ApplyCommandOpt {
#[structopt(parse(from_str))] #[structopt(parse(from_str))]
pub file_name: String, pub file_name: String,
} }