Skip to content

Commit

Permalink
feat(validation): verify image name
Browse files Browse the repository at this point in the history
  • Loading branch information
Mathias-Boulay committed Apr 17, 2024
1 parent ab8c8f0 commit 8c39430
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/fs-gen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
28 changes: 27 additions & 1 deletion src/fs-gen/src/cli_args.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,45 @@
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<Regex> = 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
#[arg(short, long, default_value=get_default_log_path().into_os_string())]
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();
Expand Down
2 changes: 1 addition & 1 deletion src/fs-gen/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

0 comments on commit 8c39430

Please sign in to comment.