Skip to content

Commit

Permalink
from to try_from, repo name format error fix
Browse files Browse the repository at this point in the history
  • Loading branch information
vsilent committed Apr 8, 2024
1 parent 41b1ca6 commit b943f4c
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 33 deletions.
2 changes: 1 addition & 1 deletion src/forms/project/docker_image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl fmt::Display for DockerImage {
impl DockerImage {
#[tracing::instrument(name = "is_active")]
pub async fn is_active(&self) -> Result<bool, String> {
DockerHub::from(self).is_active().await
DockerHub::try_from(self)?.is_active().await
}
}

Expand Down
84 changes: 56 additions & 28 deletions src/helpers/dockerhub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ pub struct OfficialRepoResult {
#[derive(Default, Debug, Clone, PartialEq, Serialize, Validate)]
pub struct DockerHub<'a> {
pub(crate) creds: DockerHubCreds<'a>,
#[validate(pattern = r"^[^:]+(:[^:]*)?$")]
pub(crate) repos: String,
pub(crate) image: String,
pub(crate) tag: Option<String>,
Expand Down Expand Up @@ -260,20 +261,22 @@ impl<'a> DockerHub<'a> {
pub async fn is_active(&'a mut self) -> Result<bool, String> {
// if namespace/user is not set change endpoint and return a different response

// let repo = self.repos.clone();
// if self.repos.contains(':') {
// let _repo = repo.split(':')
// .collect::<Vec<&str>>();
// self.repos = _repo.first().expect("split error").to_string();
// self.tag = Some(_repo.last().expect("split error").to_string());
// }

// let n = self.repos.split(':').collect::<Vec<&str>>();
// if let Some(name: &str) == n.first() {
// self.repos = name.to_string();
// }
// if let Some(name: &str) == n.last() {
// self.tag = name.to_string();
// let n = self.repos
// .split(':')
// .map(|x| x.to_string())
// .collect::<Vec<String>>();
//
// match n.len() {
// 1 => {
// self.repos = n.first().unwrap().into();
// }
// 2 => {
// self.repos = n.first().unwrap().to_string();
// self.tag = n.last().map(|s| s.to_string());
// }
// _ => {
// return Err(format!("Wrong format of repository name"));
// }
// }

if self.creds.username.is_empty() {
Expand Down Expand Up @@ -319,8 +322,12 @@ impl<'a> DockerHub<'a> {
}
}

impl<'a> From<&'a DockerImage> for DockerHub<'a> {
fn from(image: &'a DockerImage) -> Self {

impl<'a> TryFrom<&'a DockerImage> for DockerHub<'a> {
type Error = String;

fn try_from(image: &'a DockerImage) -> Result<Self, Self::Error> {

let username = match image.dockerhub_user {
Some(ref username) => username,
None => "",
Expand All @@ -331,24 +338,45 @@ impl<'a> From<&'a DockerImage> for DockerHub<'a> {
};

let name = image.dockerhub_name.clone().unwrap_or("".to_string());
let mut tag = "".to_string();

if name.contains(':') {
let parts = name
.split(':')
.collect::<Vec<&str>>();
let name = parts.first().expect("Could not split").to_string();
tag = parts.last().expect("Could not split").to_string();
}
let n = name
.split(':')
.map(|x| x.to_string())
.collect::<Vec<String>>();

DockerHub {
let (name, tag) = match n.len() {
1 => {
(
n.first().unwrap().into(),
Some("".to_string())
)
}
2 => {
(
n.first().unwrap().to_string(),
n.last().map(|s| s.to_string())
)
}
_ => {
return Err(format!("Wrong format of repository name"));
}
};

let hub = DockerHub {
creds: DockerHubCreds {
username: username,
password: password,
},
repos: name,
image: format!("{}", image),
tag: Some(tag),
tag: tag,
};

if hub.validate().is_ok() {
Ok(hub)
} else {
let errors = hub.validate().unwrap_err();
tracing::debug!("DockerHub image properties are not valid {:?}", errors);
return Err(format!("{:?}", errors));
}
}
}
}
23 changes: 19 additions & 4 deletions src/routes/project/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,33 @@ pub async fn item(
// @todo ACL
let form: ProjectForm= serde_json::from_value(request_json.clone())
.map_err(|err| JsonResponse::bad_request(err.to_string()))?;

if !form.validate().is_ok() {
let errors = form.validate().unwrap_err();
return Err(JsonResponse::bad_request(errors.to_string()));
}

let project_name = form.custom.custom_stack_code.clone();

if let Ok(result) = form.is_readable_docker_image().await {
if false == result.readable {
// if let Ok(result) = form.is_readable_docker_image().await {
// if false == result.readable {
// return Err(JsonResponse::<DockerImageReadResult>::build()
// .set_item(result)
// .bad_request("Can not access docker image"));
// }
// }

match form.is_readable_docker_image().await {
Ok(result) => {
if false == result.readable {
return Err(JsonResponse::<DockerImageReadResult>::build()
.set_item(result)
.bad_request("Can not access docker image"));
}
}
Err(e) => {
return Err(JsonResponse::<DockerImageReadResult>::build()
.set_item(result)
.bad_request("Can not access docker image"));
.bad_request(e));
}
}

Expand Down

0 comments on commit b943f4c

Please sign in to comment.