diff --git a/src/fs-gen/Cargo.toml b/src/fs-gen/Cargo.toml index 23a50ac..0a0b17d 100644 --- a/src/fs-gen/Cargo.toml +++ b/src/fs-gen/Cargo.toml @@ -7,3 +7,6 @@ edition = "2021" [dependencies] clap = { version = "4.5.3", features = ["derive", "wrap_help", "string"] } +once_cell = "1.19.0" +regex = "1.10.4" +validator = { version = "0.17.0", features = ["derive"] } diff --git a/src/fs-gen/src/cli_args.rs b/src/fs-gen/src/cli_args.rs index b7279aa..350f6c7 100644 --- a/src/fs-gen/src/cli_args.rs +++ b/src/fs-gen/src/cli_args.rs @@ -1,12 +1,24 @@ use std::{env, path::PathBuf}; use clap::{command, Parser}; +use regex::Regex; + +use once_cell::sync::Lazy; +use validator::Validate; + +// So, for any of you who may be scared, this is the regex from the OCI Distribution Sepcification for the image name + the tag +static RE_IMAGE_NAME: Lazy = Lazy::new(|| { + Regex::new(r"[a-z0-9]+((\.|_|__|-+)[a-z0-9]+)*(\/[a-z0-9]+((\.|_|__|-+)[a-z0-9]+)*)*:[a-zA-Z0-9_][a-zA-Z0-9._-]{0,127}").unwrap() +}); + /// Convert an OCI image into a CPIO file -#[derive(Parser, Debug)] +#[derive(Parser, Debug, Validate)] #[command(version, about, long_about = None)] pub struct CliArgs { /// The name of the image to download + #[arg(short, long)] + #[validate(regex(path = *RE_IMAGE_NAME))] pub image_name: String, /// The path to the output file @@ -14,6 +26,20 @@ pub struct CliArgs { pub ouput_file: PathBuf, } +impl CliArgs { + /// Get the cli arguments with additional validation + pub fn get_args() -> Self { + let args = CliArgs::parse(); + + let validation = args.validate(); + if validation.is_err() { + panic!("Invalid arguments: {}", validation.expect_err("wut")); + } + + args + } +} + /// Get the default output path for the cpio file. fn get_default_log_path() -> PathBuf { let mut path = env::current_exe().unwrap(); diff --git a/src/fs-gen/src/main.rs b/src/fs-gen/src/main.rs index 8623f70..da6e973 100644 --- a/src/fs-gen/src/main.rs +++ b/src/fs-gen/src/main.rs @@ -5,6 +5,6 @@ use crate::cli_args::CliArgs; mod cli_args; fn main() { - let args = CliArgs::parse(); + let args = CliArgs::get_args(); println!("Hello, world!, {:?}", args); }