diff --git a/migra-cli/src/config.rs b/migra-cli/src/config.rs index 76d419f..01a09b1 100644 --- a/migra-cli/src/config.rs +++ b/migra-cli/src/config.rs @@ -1,8 +1,7 @@ use migra_core::path::PathBuilder; use serde::{Deserialize, Serialize}; -use std::fs; -use std::io; use std::path::{Path, PathBuf}; +use std::{fs, io}; const MIGRA_TOML_FILENAME: &str = "Migra.toml"; @@ -57,7 +56,7 @@ impl Config { 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()?, }; @@ -88,3 +87,32 @@ impl Config { Ok(()) } } + +impl Config { + pub fn directory_path(&self) -> PathBuf { + PathBuilder::from(&self.root) + .append(&self.directory) + .build() + } + + pub fn migration_dirs(&self) -> io::Result> { + let mut entries = self + .directory_path() + .read_dir()? + .map(|res| res.map(|e| e.path())) + .collect::, io::Error>>()?; + + entries.sort(); + + let migration_dir_entries = entries + .into_iter() + .filter(|entry| { + entry.is_dir() + && PathBuilder::from(entry).append("up.sql").build().exists() + && PathBuilder::from(entry).append("down.sql").build().exists() + }) + .collect::>(); + + Ok(migration_dir_entries) + } +} diff --git a/migra-cli/src/main.rs b/migra-cli/src/main.rs index 029112f..7fb5fc7 100644 --- a/migra-cli/src/main.rs +++ b/migra-cli/src/main.rs @@ -4,6 +4,7 @@ mod config; mod opts; use config::Config; +use migra_core::path::PathBuilder; use opts::{AppOpt, ApplyCommandOpt, Command, StructOpt}; use std::fs; @@ -19,8 +20,7 @@ fn main() -> Result<(), Box> { let mut client = migra_core::database::connect(&config.database.connection)?; - let file_path = migra_core::path::PathBuilder::from(config.root) - .append(config.directory) + let file_path = PathBuilder::from(config.directory_path()) .append(file_name) .default_extension("sql") .build(); @@ -36,7 +36,23 @@ fn main() -> Result<(), Box> { } } } - Command::List | Command::Upgrade | Command::Downgrade => { + Command::List => { + let config = Config::read(opt.config)?; + + let migration_dirs = config.migration_dirs()?; + if migration_dirs.is_empty() { + println!( + "You haven't migrations in {}", + config.directory_path().to_str().unwrap() + ); + } else { + migration_dirs.iter().for_each(|dir| { + let file_name = dir.file_name().and_then(|name| name.to_str()).unwrap(); + println!("{}", file_name); + }); + } + } + Command::Upgrade | Command::Downgrade => { unimplemented!(); } }