migra/migra-cli/src/config.rs

140 lines
4.0 KiB
Rust
Raw Normal View History

2021-02-23 18:44:37 +03:00
use crate::database::migration::Migration;
2021-02-22 23:06:08 +03:00
use crate::error::{Error, MigraResult};
use serde::{Deserialize, Serialize};
use std::path::{Path, PathBuf};
use std::{env, fs, io};
pub(crate) const MIGRA_TOML_FILENAME: &str = "Migra.toml";
pub(crate) const DEFAULT_DATABASE_CONNECTION_ENV: &str = "$DATABASE_URL";
#[derive(Debug, Serialize, Deserialize)]
pub(crate) struct Config {
#[serde(skip)]
manifest_root: PathBuf,
root: PathBuf,
#[serde(default)]
pub database: DatabaseConfig,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub(crate) enum SupportedDatabaseClient {
Postgres,
2021-01-31 13:40:02 +03:00
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
2021-01-31 13:40:02 +03:00
pub(crate) struct DatabaseConfig {
pub client: Option<SupportedDatabaseClient>,
pub connection: Option<String>,
}
impl DatabaseConfig {
2021-02-22 23:06:08 +03:00
pub fn client(&self) -> MigraResult<SupportedDatabaseClient> {
Ok(SupportedDatabaseClient::Postgres)
}
2021-02-22 23:06:08 +03:00
pub fn connection_string(&self) -> MigraResult<String> {
let connection = self
.connection
.clone()
.unwrap_or_else(|| String::from(DEFAULT_DATABASE_CONNECTION_ENV));
if let Some(connection_env) = connection.strip_prefix("$") {
2021-02-22 23:06:08 +03:00
env::var(connection_env).map_err(|_| Error::MissedEnvVar(connection_env.to_string()))
} else {
Ok(connection)
}
}
}
impl Default for Config {
fn default() -> Config {
Config {
manifest_root: PathBuf::default(),
root: PathBuf::from("database"),
2021-01-31 13:40:02 +03:00
database: DatabaseConfig {
connection: Some(String::from(DEFAULT_DATABASE_CONNECTION_ENV)),
..Default::default()
},
}
}
}
2021-02-22 23:06:08 +03:00
fn search_for_directory_containing_file(path: &Path, file_name: &str) -> MigraResult<PathBuf> {
let file_path = path.join(file_name);
if file_path.is_file() {
Ok(path.to_owned())
} else {
path.parent()
.ok_or(Error::RootNotFound)
.and_then(|p| search_for_directory_containing_file(p, file_name))
}
}
2021-02-22 23:06:08 +03:00
fn recursive_find_project_root() -> MigraResult<PathBuf> {
let current_dir = std::env::current_dir()?;
2021-02-22 23:06:08 +03:00
search_for_directory_containing_file(&current_dir, MIGRA_TOML_FILENAME)
2021-02-02 00:53:33 +03:00
}
2021-02-02 00:53:33 +03:00
impl Config {
2021-02-22 23:06:08 +03:00
pub fn read(config_path: Option<PathBuf>) -> MigraResult<Config> {
2021-02-02 00:53:33 +03:00
let config_path = match config_path {
2021-03-25 00:41:22 +03:00
Some(config_path) if config_path.is_dir() => {
Some(config_path.join(MIGRA_TOML_FILENAME))
}
Some(config_path) => Some(config_path),
2021-03-25 00:41:22 +03:00
None => recursive_find_project_root()
.map(|path| path.join(MIGRA_TOML_FILENAME))
.ok(),
2021-02-02 00:53:33 +03:00
};
match config_path {
None => Ok(Config::default()),
Some(config_path) => {
let content = fs::read_to_string(&config_path)?;
2021-02-02 00:53:33 +03:00
let mut config: Config = toml::from_str(&content).expect("Cannot parse Migra.toml");
config.manifest_root = config_path
.parent()
.unwrap_or_else(|| Path::new(""))
.to_path_buf();
2021-02-02 00:53:33 +03:00
Ok(config)
}
}
}
}
impl Config {
pub fn directory_path(&self) -> PathBuf {
2021-02-22 23:13:50 +03:00
self.manifest_root.join(&self.root)
}
pub fn migration_dir_path(&self) -> PathBuf {
2021-02-22 23:13:50 +03:00
self.directory_path().join("migrations")
}
2021-02-22 23:06:08 +03:00
pub fn migrations(&self) -> MigraResult<Vec<Migration>> {
2021-02-10 01:13:27 +03:00
let mut entries = match self.migration_dir_path().read_dir() {
Err(e) if e.kind() == io::ErrorKind::NotFound => return Ok(Vec::new()),
entries => entries?
.map(|res| res.map(|e| e.path()))
.collect::<Result<Vec<_>, io::Error>>()?,
};
if entries.is_empty() {
return Ok(vec![]);
}
entries.sort();
let migrations = entries
.iter()
2021-02-21 17:53:43 +03:00
.filter_map(|path| Migration::new(&path))
.collect::<Vec<_>>();
Ok(migrations)
}
}