Archived
1
0
Fork 0

feat(cli): implement list command

... that returns list of directory with migration
This commit is contained in:
Dmitriy Pleshevskiy 2021-02-02 23:50:42 +03:00
parent 38a18180eb
commit 9fb8add4b8
2 changed files with 50 additions and 6 deletions

View file

@ -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<Vec<PathBuf>> {
let mut entries = self
.directory_path()
.read_dir()?
.map(|res| res.map(|e| e.path()))
.collect::<Result<Vec<_>, 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::<Vec<_>>();
Ok(migration_dir_entries)
}
}

View file

@ -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<dyn std::error::Error>> {
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<dyn std::error::Error>> {
}
}
}
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!();
}
}