use clap::{Arg, Command};
use core_dockpack::cmd_processes:: pull::unpack_files;
use core_dockpack::cmd_processes::build::build_dockerfile;
use core_dockpack::cmd_processes::push::execute_push;
use core_dockpack::utils::docker_commands::build_docker_image;
fn main() {
let matches = Command::new("Docker Unpacker")
.version("0.1.1")
.author("Maxwell Flitton maxwellflitton@gmail.com")
.about("Unpacks Docker images into a specified directory")
.arg(
Arg::new("command")
.help("The command to execute (e.g., pull, push, ls)")
.required(true)
.index(1)
)
.arg(
Arg::new("image")
.short('i')
.long("image")
.value_name("IMAGE")
.help("The name of the Docker image to unpack")
)
.arg(
Arg::new("directory")
.short('d')
.long("directory")
.value_name("DIRECTORY")
.help("The directory to unpack the Docker image into")
)
.get_matches();
let command = matches.get_one::<String>("command").expect("Command argument is required");
match command.as_str() {
"pull" => {
let image = match matches.get_one::<String>("image") {
Some(image) => image,
None => {
eprintln!("Image argument is required for pull");
return;
}
};
let directory = match matches.get_one::<String>("directory") {
Some(directory) => directory,
None => {
eprintln!("Directory argument is required for pull");
return;
}
};
match unpack_files::unpack_files_from_image(image, directory) {
Ok(path) => println!("Successfully unpacked to: {}", path),
Err(e) => eprintln!("Error unpacking image: {}", e),
}
}
"build" => {
let current_dir = std::env::current_dir().expect("Failed to get current directory")
.to_str()
.expect("Failed to convert path to string").to_owned();
let image = match matches.get_one::<String>("image") {
Some(image) => image,
None => {
eprintln!("Image argument is required for push");
return;
}
};
let uuid = uuid::Uuid::new_v4().to_string();
let full_path = std::path::Path::new(¤t_dir).join(uuid).to_str().expect("Failed to convert path to string").to_owned();
match build_dockerfile::create_dockerfile(&full_path) {
Ok(()) => {},
Err(e) => eprintln!("Error unpacking image: {}", e),
}
match build_docker_image(image, &full_path) {
Ok(()) => println!("Successfully created docker image tagged: {}", image),
Err(e) => {
let file_path = std::path::Path::new(¤t_dir).join("Dockerfile").to_str().expect("Failed to convert path to string").to_owned();
std::fs::remove_file(file_path).expect("Failed to remove Dockerfile");
eprintln!("Error creating docker image: {}", e);
},
}
std::fs::remove_file(full_path).expect("Failed to remove Dockerfile");
}
"push" => {
let image = match matches.get_one::<String>("image") {
Some(image) => image,
None => {
eprintln!("Image argument is required for push");
return;
}
};
let _ = execute_push::execute_push_image(image);
}
"ls" => {
println!("listing unpacked images will come when a data store for tracking unpacked images is implemented");
}
_ => {
eprintln!("Unknown command: {}", command);
}
}
}