Skip to content

Commit

Permalink
default to using :latest tag for images
Browse files Browse the repository at this point in the history
  • Loading branch information
Carsten König committed Jul 3, 2024
1 parent 835cedf commit da8f405
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 9 deletions.
48 changes: 40 additions & 8 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ImageParseError> 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,
}
Expand All @@ -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:?}"),
}
}
}
Expand All @@ -45,22 +70,29 @@ impl From<BolladError> for AutodokError {
status_code,
message,
} => AutodokError::from_bollard(status_code, message),
_ => AutodokError::DockerError(err),
_ => AutodokError::Docker(err),
}
}
}

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 };
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ mod api_key;
mod credentials;
mod docker;
mod error;
mod parse;
mod random;
mod routes;

Expand Down
13 changes: 13 additions & 0 deletions src/parse.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use crate::error::ImageParseError;

pub fn parse_image_tag(input: String) -> Result<String, ImageParseError> {
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)),
}
}
2 changes: 1 addition & 1 deletion src/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub async fn update_image(
extract::Json(payload): extract::Json<UpdateContainerImage>,
) -> Result<Response, AutodokError> {
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.");
Expand Down

0 comments on commit da8f405

Please sign in to comment.