moved function to mod.rs; use generic structs in default.rs

This commit is contained in:
Thomas Eppers 2021-11-01 13:00:59 +01:00
parent cb1a1c24b7
commit fe3f0579ad
3 changed files with 33 additions and 35 deletions

View File

@ -70,35 +70,4 @@ impl DockerHub {
next_page: tags.next_page, next_page: tags.next_page,
}) })
} }
/// checks the repo name and may add a prefix for official images
pub fn check_repo(name: &str) -> Result<String, Error> {
let repo = match repo::split_tag_from_repo(name) {
Err(e) => return Err(Error::Converting(format!("{}", e))),
Ok((name, _)) => name,
};
match repo::split_repo_without_tag(name) {
Ok(repo::Repo::Project(s)) => Ok(format!("library/{}", s)),
Ok(_) => Ok(repo.to_string()),
Err(e) => Err(Error::Converting(format!("{}", e))),
}
}
}
#[cfg(test)]
mod tests {
use crate::repository::dockerhub::DockerHub;
#[test]
fn test_check_repo() {
assert_eq!(DockerHub::check_repo("nginx").unwrap(), "library/nginx");
assert_eq!(
DockerHub::check_repo("library/nginx").unwrap(),
"library/nginx"
);
assert_eq!(
DockerHub::check_repo("rocketchat/rocket.chat").unwrap(),
"rocketchat/rocket.chat"
);
}
} }

View File

@ -1,9 +1,11 @@
pub mod dockerhub; mod dockerhub;
use std::fmt; use std::fmt;
use chrono::DateTime; use chrono::DateTime;
use crate::repo;
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub enum Error { pub enum Error {
/// couldn't fetch json with reqwest /// couldn't fetch json with reqwest
@ -148,3 +150,30 @@ fn format_time_nice(time: chrono::Duration) -> String {
format!("{} Sekunden", time.num_seconds()) format!("{} Sekunden", time.num_seconds())
} }
} }
/// checks the repo name and may add a prefix for official images
pub fn check_repo(name: &str) -> Result<String, Error> {
let repo = match repo::split_tag_from_repo(name) {
Err(e) => return Err(Error::Converting(format!("{}", e))),
Ok((name, _)) => name,
};
match repo::split_repo_without_tag(name) {
Ok(repo::Repo::Project(s)) => Ok(format!("library/{}", s)),
Ok(_) => Ok(repo.to_string()),
Err(e) => Err(Error::Converting(format!("{}", e))),
}
}
#[cfg(test)]
mod tests {
#[test]
fn test_check_repo() {
assert_eq!(super::check_repo("nginx").unwrap(), "library/nginx");
assert_eq!(super::check_repo("library/nginx").unwrap(), "library/nginx");
assert_eq!(
super::check_repo("rocketchat/rocket.chat").unwrap(),
"rocketchat/rocket.chat"
);
}
}

View File

@ -7,7 +7,7 @@ use tui::backend::TermionBackend;
use tui::layout::{Constraint, Direction, Layout}; use tui::layout::{Constraint, Direction, Layout};
use tui::Terminal; use tui::Terminal;
use crate::repository::dockerhub; use crate::repository;
use crate::widget::info; use crate::widget::info;
use crate::widget::repo_entry; use crate::widget::repo_entry;
use crate::widget::service_switcher; use crate::widget::service_switcher;
@ -157,7 +157,7 @@ impl Ui {
match ui.services.extract_repo() { match ui.services.extract_repo() {
Err(e) => ui.info.set_info(&format!("{}", e)), Err(e) => ui.info.set_info(&format!("{}", e)),
Ok(s) => { Ok(s) => {
let repo = match dockerhub::DockerHub::check_repo(&s) { let repo = match repository::check_repo(&s) {
Err(e) => { Err(e) => {
ui.info.set_info(&format!("{}", e)); ui.info.set_info(&format!("{}", e));
continue; continue;
@ -178,7 +178,7 @@ impl Ui {
match ui.services.extract_repo() { match ui.services.extract_repo() {
Err(e) => ui.info.set_info(&format!("{}", e)), Err(e) => ui.info.set_info(&format!("{}", e)),
Ok(s) => { Ok(s) => {
let repo = match dockerhub::DockerHub::check_repo(&s) { let repo = match repository::check_repo(&s) {
Err(e) => { Err(e) => {
ui.info.set_info(&format!("{}", e)); ui.info.set_info(&format!("{}", e));
continue; continue;