From da8f40549d77f01725837d2c1ff48e8a3b91fe37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carsten=20K=C3=B6nig?= Date: Wed, 3 Jul 2024 20:11:26 +0200 Subject: [PATCH] default to using :latest tag for images --- src/error.rs | 48 ++++++++++++++++++++++++++++++++++++++++-------- src/main.rs | 1 + src/parse.rs | 13 +++++++++++++ src/routes.rs | 2 +- 4 files changed, 55 insertions(+), 9 deletions(-) create mode 100644 src/parse.rs diff --git a/src/error.rs b/src/error.rs index 6dc5c6e..aed64bb 100644 --- a/src/error.rs +++ b/src/error.rs @@ -6,18 +6,42 @@ use std::fmt; #[derive(Debug)] pub enum AutodokError { - DockerError(BolladError), - DockerResponseServerError { + Docker(BolladError), + DockerResponseServer { status_code: StatusCode, message: String, }, + Input(ImageParseError), +} + +#[derive(Debug)] +pub enum ImageParseError { + EmptyImage, + EmptyPart(String), +} + +impl Error for ImageParseError {} + +impl fmt::Display for ImageParseError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match self { + Self::EmptyImage => write!(f, "image missing"), + Self::EmptyPart(image) => write!(f, "invalid image: {image}"), + } + } +} + +impl From for AutodokError { + fn from(err: ImageParseError) -> Self { + AutodokError::Input(err) + } } impl AutodokError { fn from_bollard(code: u16, message: String) -> Self { let status_code = StatusCode::from_u16(code).unwrap_or(StatusCode::INTERNAL_SERVER_ERROR); - Self::DockerResponseServerError { + Self::DockerResponseServer { status_code, message, } @@ -29,11 +53,12 @@ impl Error for AutodokError {} impl fmt::Display for AutodokError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { - Self::DockerError(e) => write!(f, "Docker error: {}", e), - Self::DockerResponseServerError { + Self::Docker(e) => write!(f, "Docker error: {}", e), + Self::DockerResponseServer { status_code, message, } => write!(f, "DockerContainerNotFound {status_code} {message}"), + Self::Input(e) => write!(f, "InputError: {e:?}"), } } } @@ -45,7 +70,7 @@ impl From for AutodokError { status_code, message, } => AutodokError::from_bollard(status_code, message), - _ => AutodokError::DockerError(err), + _ => AutodokError::Docker(err), } } } @@ -53,14 +78,21 @@ impl From for AutodokError { impl IntoResponse for AutodokError { fn into_response(self) -> Response { let (status_code, message) = match self { - AutodokError::DockerError(e) => ( + AutodokError::Docker(e) => ( StatusCode::INTERNAL_SERVER_ERROR, format!("something went wrong: {e}"), ), - AutodokError::DockerResponseServerError { + AutodokError::DockerResponseServer { status_code, message, } => (status_code, message), + AutodokError::Input(err) => { + let message = match err { + ImageParseError::EmptyImage => "image missing".to_string(), + ImageParseError::EmptyPart(image) => format!("invalid image: {}", image), + }; + (StatusCode::BAD_REQUEST, message) + } }; let msg = crate::routes::Msg { message }; diff --git a/src/main.rs b/src/main.rs index 8f9a0b1..10cf098 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,6 +19,7 @@ mod api_key; mod credentials; mod docker; mod error; +mod parse; mod random; mod routes; diff --git a/src/parse.rs b/src/parse.rs new file mode 100644 index 0000000..f005e55 --- /dev/null +++ b/src/parse.rs @@ -0,0 +1,13 @@ +use crate::error::ImageParseError; + +pub fn parse_image_tag(input: String) -> Result { + if input.is_empty() { + return Err(ImageParseError::EmptyImage); + } + + match input.split_once(':') { + Some(("", _)) | Some((_, "")) => Err(ImageParseError::EmptyPart(input)), + Some((image, tag)) => Ok(format!("{}:{}", image, tag)), + None => Ok(format!("{}:latest", input)), + } +} diff --git a/src/routes.rs b/src/routes.rs index e637e4f..a231ce6 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -25,7 +25,7 @@ pub async fn update_image( extract::Json(payload): extract::Json, ) -> Result { let container = payload.container; - let image = payload.image; + let image = crate::parse::parse_image_tag(payload.image)?; docker.inspect_container(&container, None).await?; info!(" Container '{container}' found.");