Skip to content

Commit

Permalink
remove unwraps, use files_count directly
Browse files Browse the repository at this point in the history
  • Loading branch information
rawdaGastan committed Sep 25, 2024
1 parent 8b966ca commit a6a50ad
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 51 deletions.
56 changes: 30 additions & 26 deletions docker2fl/src/docker2fl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use bollard::container::{
use bollard::image::{CreateImageOptions, RemoveImageOptions};
use bollard::Docker;
use std::sync::mpsc::Sender;
use tempdir::TempDir;
use walkdir::WalkDir;

use anyhow::{Context, Result};
Expand All @@ -13,7 +14,7 @@ use serde_json::json;
use std::collections::HashMap;
use std::default::Default;
use std::fs;
use std::path::{Path, PathBuf};
use std::path::Path;
use std::process::Command;
use tokio_async_drop::tokio_async_drop;

Expand Down Expand Up @@ -43,41 +44,42 @@ impl Drop for DockerInfo {
}
}

#[derive(Clone)]
pub struct DockerImageToFlist {
meta: Writer,
image_name: String,
credentials: Option<DockerCredentials>,
docker_tmp_dir_path: PathBuf,
files_count: usize,
docker_tmp_dir: TempDir,
}

impl DockerImageToFlist {
pub fn new(
meta: Writer,
image_name: String,
credentials: Option<DockerCredentials>,
docker_tmp_dir_path: PathBuf,
docker_tmp_dir: TempDir,
) -> Self {
DockerImageToFlist {
meta,
image_name,
credentials,
docker_tmp_dir_path,
files_count: 0,
docker_tmp_dir,
}
}

pub fn files_count(&self) -> usize {
self.files_count
WalkDir::new(self.docker_tmp_dir.path()).into_iter().count()
}

pub async fn prepare(&mut self) -> Result<()> {
#[cfg(unix)]
let docker = Docker::connect_with_socket_defaults().context("failed to create docker")?;

let container_file = Path::file_stem(self.docker_tmp_dir_path.as_path()).unwrap();
let container_name = container_file.to_str().unwrap().to_owned();
let container_file =
Path::file_stem(self.docker_tmp_dir.path()).expect("failed to get directory name");
let container_name = container_file
.to_str()
.expect("failed to get container name")
.to_owned();

let docker_info = DockerInfo {
image_name: self.image_name.to_owned(),
Expand All @@ -89,7 +91,7 @@ impl DockerImageToFlist {
&docker_info.docker,
&docker_info.image_name,
&docker_info.container_name,
&self.docker_tmp_dir_path,
self.docker_tmp_dir.path(),
self.credentials.clone(),
)
.await
Expand All @@ -99,18 +101,14 @@ impl DockerImageToFlist {
docker_info.image_name
);

self.files_count = WalkDir::new(self.docker_tmp_dir_path.as_path())
.into_iter()
.count();

Ok(())
}

pub async fn pack<S: Store>(&mut self, store: S, sender: Option<Sender<u32>>) -> Result<()> {
rfs::pack(
self.meta.clone(),
store,
&self.docker_tmp_dir_path,
&self.docker_tmp_dir.path(),
true,
sender,
)
Expand Down Expand Up @@ -246,35 +244,41 @@ async fn container_boot(
let mut env: HashMap<String, String> = HashMap::new();
let mut cwd = String::from("/");

let cmd = container_config.cmd.unwrap();
let cmd = container_config.cmd.expect("failed to get cmd configs");

if container_config.entrypoint.is_some() {
let entrypoint = container_config.entrypoint.unwrap();
command = (entrypoint.first().unwrap()).to_string();
let entrypoint = container_config
.entrypoint
.expect("failed to get entry point");
command = (entrypoint.first().expect("failed to get first entry point")).to_string();

if entrypoint.len() > 1 {
let (_, entries) = entrypoint.split_first().unwrap();
let (_, entries) = entrypoint
.split_first()
.expect("failed to split entry point");
args = entries.to_vec();
} else {
args = cmd;
}
} else {
command = (cmd.first().unwrap()).to_string();
let (_, entries) = cmd.split_first().unwrap();
command = (cmd.first().expect("failed to get first cmd")).to_string();
let (_, entries) = cmd.split_first().expect("failed to split cmd");
args = entries.to_vec();
}

if container_config.env.is_some() {
for entry in container_config.env.unwrap().iter() {
for entry in container_config.env.expect("failed to get env").iter() {
let mut split = entry.split('=');
env.insert(
split.next().unwrap().to_string(),
split.next().unwrap().to_string(),
split.next().expect("failed to get env key").to_string(),
split.next().expect("failed to get env value").to_string(),
);
}
}

let working_dir = container_config.working_dir.unwrap();
let working_dir = container_config
.working_dir
.expect("failed to get working dir");
if !working_dir.is_empty() {
cwd = working_dir;
}
Expand Down
6 changes: 3 additions & 3 deletions docker2fl/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,11 @@ async fn main() -> Result<()> {
let store = parse_router(&opts.store).await?;

let container_name = Uuid::new_v4().to_string();
let docker_tmp_dir = tempdir::TempDir::new(&container_name).unwrap();
let docker_tmp_dir_path = docker_tmp_dir.path().to_owned();
let docker_tmp_dir =
tempdir::TempDir::new(&container_name).expect("failed to create tmp directory");

let mut docker_to_fl =
docker2fl::DockerImageToFlist::new(meta, docker_image, credentials, docker_tmp_dir_path);
docker2fl::DockerImageToFlist::new(meta, docker_image, credentials, docker_tmp_dir);
let res = docker_to_fl.convert(store, None).await;

// remove the file created with the writer if fl creation failed
Expand Down
40 changes: 18 additions & 22 deletions fl-server/src/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,15 +178,10 @@ pub async fn create_flist_handler(
let container_name = Uuid::new_v4().to_string();
let docker_tmp_dir =
tempdir::TempDir::new(&container_name).expect("failed to create tmp dir for docker");
let docker_tmp_dir_path = docker_tmp_dir.path().to_owned();

let (tx, rx) = mpsc::channel();
let mut docker_to_fl = docker2fl::DockerImageToFlist::new(
meta,
docker_image,
credentials,
docker_tmp_dir_path.clone(),
);
let mut docker_to_fl =
docker2fl::DockerImageToFlist::new(meta, docker_image, credentials, docker_tmp_dir);

let res = docker_to_fl.prepare().await;
if res.is_err() {
Expand Down Expand Up @@ -336,25 +331,26 @@ pub async fn list_flists_handler(State(state): State<Arc<config::AppState>>) ->

let rs: Result<Vec<FileInfo>, std::io::Error> =
visit_dir_one_level(&state.config.flist_dir, &state).await;
match rs {
Ok(files) => {
for file in files {
if !file.is_file {
let flists_per_username = visit_dir_one_level(&file.path_uri, &state).await;
match flists_per_username {
Ok(files) => flists.insert(file.name, files),
Err(e) => {
log::error!("failed to list flists per username with error: {}", e);
return Err(ResponseError::InternalServerError);
}
};
};
}
}

let files = match rs {
Ok(files) => files,
Err(e) => {
log::error!("failed to list flists directory with error: {}", e);
return Err(ResponseError::InternalServerError);
}
};

for file in files {
if !file.is_file {
let flists_per_username = visit_dir_one_level(&file.path_uri, &state).await;
match flists_per_username {
Ok(files) => flists.insert(file.name, files),
Err(e) => {
log::error!("failed to list flists per username with error: {}", e);
return Err(ResponseError::InternalServerError);
}
};
};
}

Ok(ResponseResult::Flists(flists))
Expand Down

0 comments on commit a6a50ad

Please sign in to comment.