merged Error into 1; simplified some logic
This commit is contained in:
parent
8ab2690d17
commit
c98558c41d
26
src/error.rs
Normal file
26
src/error.rs
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
use thiserror::Error;
|
||||||
|
|
||||||
|
#[derive(Debug, Error)]
|
||||||
|
pub enum Error {
|
||||||
|
#[error("Expected a tag")]
|
||||||
|
NoTagFound,
|
||||||
|
|
||||||
|
#[error("Unexpected input")]
|
||||||
|
MisformedInput,
|
||||||
|
|
||||||
|
/// a serde error
|
||||||
|
#[error("Converting error: {0}")]
|
||||||
|
Converting(String),
|
||||||
|
|
||||||
|
/// invalid repos show a valid json with 0 tags
|
||||||
|
#[error("Given Repo does not exists or has 0 tags.")]
|
||||||
|
NoTagsFound,
|
||||||
|
|
||||||
|
/// converting serde error
|
||||||
|
#[error("Serde error: {0}")]
|
||||||
|
Serde(#[from] serde_json::Error),
|
||||||
|
|
||||||
|
/// error while handling requests
|
||||||
|
#[error("reqwest error: {0}")]
|
||||||
|
Reqwest(#[from] reqwest::Error),
|
||||||
|
}
|
@ -1,4 +1,5 @@
|
|||||||
mod common;
|
mod common;
|
||||||
|
mod error;
|
||||||
mod repo;
|
mod repo;
|
||||||
mod repository;
|
mod repository;
|
||||||
mod ui;
|
mod ui;
|
||||||
|
17
src/repo.rs
17
src/repo.rs
@ -1,21 +1,6 @@
|
|||||||
use std::fmt;
|
|
||||||
|
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
|
|
||||||
#[derive(Debug, PartialEq)]
|
use crate::error::Error;
|
||||||
pub enum Error {
|
|
||||||
NoTagFound,
|
|
||||||
MisformedInput,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl fmt::Display for Error {
|
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
||||||
match self {
|
|
||||||
Error::NoTagFound => write!(f, "Expected a tag"),
|
|
||||||
Error::MisformedInput => write!(f, "Unexpected input"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, PartialEq)]
|
#[derive(Debug, PartialEq)]
|
||||||
pub enum Repo {
|
pub enum Repo {
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
|
||||||
use crate::repository::Error;
|
use crate::error::Error;
|
||||||
|
|
||||||
#[derive(Deserialize, Debug, Clone)]
|
#[derive(Deserialize, Debug, Clone)]
|
||||||
struct ImageDetails {
|
struct ImageDetails {
|
||||||
@ -53,17 +53,10 @@ impl DockerHub {
|
|||||||
|
|
||||||
/// fetches tag information from a url
|
/// fetches tag information from a url
|
||||||
pub fn with_url(url: &str) -> Result<super::Repo, Error> {
|
pub fn with_url(url: &str) -> Result<super::Repo, Error> {
|
||||||
let response = match reqwest::blocking::get(url) {
|
let response = reqwest::blocking::get(url)?;
|
||||||
Ok(result) => result,
|
|
||||||
Err(e) => return Err(Error::Fetching(format!("reqwest error: {}", e))),
|
|
||||||
};
|
|
||||||
|
|
||||||
//convert it to json
|
//convert it to json
|
||||||
let tags = match response.json::<Self>() {
|
let tags = response.json::<Self>()?;
|
||||||
Ok(result) => result,
|
|
||||||
Err(e) => return Err(Error::Converting(format!("invalid json: {}", e))),
|
|
||||||
};
|
|
||||||
|
|
||||||
if tags.results.is_empty() {
|
if tags.results.is_empty() {
|
||||||
return Err(Error::NoTagsFound);
|
return Err(Error::NoTagsFound);
|
||||||
}
|
}
|
||||||
|
@ -1,24 +1,11 @@
|
|||||||
mod dockerhub;
|
mod dockerhub;
|
||||||
|
|
||||||
use chrono::DateTime;
|
use chrono::DateTime;
|
||||||
use thiserror::Error;
|
|
||||||
|
|
||||||
use crate::common::display_duration_ext::DisplayDurationExt;
|
use crate::common::display_duration_ext::DisplayDurationExt;
|
||||||
|
use crate::error::Error;
|
||||||
use crate::repo;
|
use crate::repo;
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Error)]
|
|
||||||
pub enum Error {
|
|
||||||
/// couldn't fetch json with reqwest
|
|
||||||
#[error("Fetching error: {0}")]
|
|
||||||
Fetching(String),
|
|
||||||
/// a serde error
|
|
||||||
#[error("Converting error: {0}")]
|
|
||||||
Converting(String),
|
|
||||||
/// invalid repos show a valid json with 0 tags
|
|
||||||
#[error("Given Repo does not exists or has 0 tags.")]
|
|
||||||
NoTagsFound,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, PartialEq)]
|
#[derive(Clone, PartialEq)]
|
||||||
pub struct TagDetails {
|
pub struct TagDetails {
|
||||||
pub arch: Option<String>,
|
pub arch: Option<String>,
|
||||||
@ -77,7 +64,9 @@ impl Repo {
|
|||||||
if registry.unwrap_or_default().is_empty() {
|
if registry.unwrap_or_default().is_empty() {
|
||||||
dockerhub::DockerHub::create_repo(&repo)
|
dockerhub::DockerHub::create_repo(&repo)
|
||||||
} else {
|
} else {
|
||||||
Err(Error::Converting("This registry is not supported".into()))
|
Err(Error::Converting(
|
||||||
|
"This registry is not supported yet".into(),
|
||||||
|
))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -91,26 +80,23 @@ impl Repo {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn next_page(&self) -> Option<Self> {
|
pub fn next_page(&self) -> Option<Self> {
|
||||||
match &self.next_page {
|
if let Some(url) = &self.next_page {
|
||||||
Some(url) => match Self::with_url(url) {
|
match Self::with_url(url) {
|
||||||
Ok(tags) => Some(tags),
|
Ok(tags) => return Some(tags),
|
||||||
Err(_) => None,
|
Err(e) => println!("Encountered error: {e}"),
|
||||||
},
|
|
||||||
None => None,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
None
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// checks the repo name and may add a prefix for official images
|
/// checks the repo name and may add a prefix for official images
|
||||||
pub fn check_repo(name: &str) -> Result<String, Error> {
|
pub fn check_repo(name: &str) -> Result<String, Error> {
|
||||||
let repo = match repo::split_tag_from_repo(name) {
|
let repo = repo::split_tag_from_repo(name)?;
|
||||||
Err(e) => return Err(Error::Converting(format!("{}", e))),
|
|
||||||
Ok((name, _)) => name,
|
|
||||||
};
|
|
||||||
|
|
||||||
match repo::split_repo_without_tag(name) {
|
match repo::split_repo_without_tag(name) {
|
||||||
Ok(repo::Repo::Project(s)) => Ok(format!("library/{}", s)),
|
Ok(repo::Repo::Project(s)) => Ok(format!("library/{}", s)),
|
||||||
Ok(_) => Ok(repo.to_string()),
|
Ok(_) => Ok(repo.0.to_string()),
|
||||||
Err(e) => Err(Error::Converting(format!("{}", e))),
|
Err(e) => Err(Error::Converting(format!("{}", e))),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user