chore: tests-compatible logging and abstract repo initialization in settings

This commit is contained in:
Aravinth Manivannan 2022-04-27 11:50:06 +05:30
parent 58e928eeda
commit e757e52582
Signed by: realaravinth
GPG Key ID: AD9F0F08E855ED88
2 changed files with 24 additions and 10 deletions

View File

@ -15,7 +15,12 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
use git2::{build::CheckoutBuilder, BranchType, Direction, ObjectType, Repository}; use git2::{build::CheckoutBuilder, BranchType, Direction, ObjectType, Repository};
#[cfg(not(test))]
use log::info; use log::info;
#[cfg(test)]
use std::println as info;
use serde::Deserialize; use serde::Deserialize;
use crate::errors::*; use crate::errors::*;

View File

@ -19,7 +19,12 @@ use std::path::Path;
use std::sync::Arc; use std::sync::Arc;
use config::{Config, Environment, File}; use config::{Config, Environment, File};
use log::warn; #[cfg(not(test))]
use log::{error, warn};
#[cfg(test)]
use std::{println as warn, println as error};
use serde::Deserialize; use serde::Deserialize;
use url::Url; use url::Url;
@ -73,7 +78,7 @@ impl Settings {
} }
if !read_file { if !read_file {
log::warn!("configuration file not found"); warn!("configuration file not found");
} }
s = s.add_source(Environment::with_prefix("PAGES").separator("__")); s = s.add_source(Environment::with_prefix("PAGES").separator("__"));
@ -87,7 +92,13 @@ impl Settings {
Err(e) => warn!("couldn't interpret PORT: {}", e), Err(e) => warn!("couldn't interpret PORT: {}", e),
} }
for (index, page) in settings.pages.iter().enumerate() { settings.init();
Ok(settings)
}
pub fn init(&self) {
for (index, page) in self.pages.iter().enumerate() {
Url::parse(&page.repo).unwrap(); Url::parse(&page.repo).unwrap();
let path = Path::new(&page.path); let path = Path::new(&page.path);
if path.exists() && path.is_file() { if path.exists() && path.is_file() {
@ -97,27 +108,25 @@ impl Settings {
if !path.exists() { if !path.exists() {
std::fs::create_dir_all(&path).unwrap(); std::fs::create_dir_all(&path).unwrap();
} }
for (index2, page2) in settings.pages.iter().enumerate() { for (index2, page2) in self.pages.iter().enumerate() {
if index2 == index { if index2 == index {
continue; continue;
} }
if page.secret == page2.secret { if page.secret == page2.secret {
log::error!("{}", ServiceError::SecretTaken(page.clone(), page2.clone())); error!("{}", ServiceError::SecretTaken(page.clone(), page2.clone()));
} else if page.repo == page2.repo { } else if page.repo == page2.repo {
log::error!( error!(
"{}", "{}",
ServiceError::DuplicateRepositoryURL(page.clone(), page2.clone(),) ServiceError::DuplicateRepositoryURL(page.clone(), page2.clone(),)
); );
} else if page.path == page2.path { } else if page.path == page2.path {
log::error!("{}", ServiceError::PathTaken(page.clone(), page2.clone())); error!("{}", ServiceError::PathTaken(page.clone(), page2.clone()));
} }
} }
if let Err(e) = page.update() { if let Err(e) = page.update() {
log::error!("{e}"); error!("{e}");
} }
} }
Ok(settings)
} }
#[cfg(not(tarpaulin_include))] #[cfg(not(tarpaulin_include))]