Main: Make content checking optional for overriding file
This commit is contained in:
parent
6bef09b8c0
commit
2d8121510d
1 changed files with 60 additions and 39 deletions
99
src/main.rs
99
src/main.rs
|
@ -55,13 +55,33 @@ impl Display for CharacterSet {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, ValueEnum)]
|
#[derive(Debug, Clone, ValueEnum)]
|
||||||
enum BranchType {
|
enum WhenFileExists {
|
||||||
|
Exit,
|
||||||
|
Error,
|
||||||
|
Override,
|
||||||
|
|
||||||
|
CheckContent
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Display for WhenFileExists {
|
||||||
|
fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
|
||||||
|
match self {
|
||||||
|
Self::Exit => write!(formatter, "Exit"),
|
||||||
|
Self::Error => write!(formatter, "Error"),
|
||||||
|
Self::Override => write!(formatter, "Override"),
|
||||||
|
Self::CheckContent => write!(formatter, "CheckInvalidContent"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, ValueEnum)]
|
||||||
|
enum WhenFileContentIsInvalid {
|
||||||
Exit,
|
Exit,
|
||||||
Error,
|
Error,
|
||||||
Override,
|
Override,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Display for BranchType {
|
impl Display for WhenFileContentIsInvalid {
|
||||||
fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
|
||||||
match self {
|
match self {
|
||||||
Self::Exit => write!(formatter, "Exit"),
|
Self::Exit => write!(formatter, "Exit"),
|
||||||
|
@ -71,6 +91,7 @@ impl Display for BranchType {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// Generate random strings and put them in a file
|
/// Generate random strings and put them in a file
|
||||||
#[derive(Parser, Debug)]
|
#[derive(Parser, Debug)]
|
||||||
#[command(author, version, about, long_about = None)]
|
#[command(author, version, about, long_about = None)]
|
||||||
|
@ -89,11 +110,11 @@ struct Arguments {
|
||||||
|
|
||||||
/// What to do when a file already exists at the given path
|
/// What to do when a file already exists at the given path
|
||||||
#[arg(long, env, default_value = "error")]
|
#[arg(long, env, default_value = "error")]
|
||||||
when_file_exists: BranchType,
|
when_file_exists: WhenFileExists,
|
||||||
|
|
||||||
/// What to do when the content of a file at the given path does not match the character set or length
|
/// What to do when the content of a file at the given path does not match the character set or length
|
||||||
#[arg(long, env, default_value = "error")]
|
#[arg(long, env, default_value = "error")]
|
||||||
when_file_content_does_not_match: BranchType,
|
when_file_content_is_invalid: WhenFileContentIsInvalid,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() -> io::Result<()> {
|
fn main() -> io::Result<()> {
|
||||||
|
@ -118,49 +139,49 @@ fn main() -> io::Result<()> {
|
||||||
|
|
||||||
if exists {
|
if exists {
|
||||||
match arguments.when_file_exists {
|
match arguments.when_file_exists {
|
||||||
BranchType::Exit => {
|
WhenFileExists::Exit => {
|
||||||
log::info!("File exists, exiting.");
|
log::info!("File exists, exiting.");
|
||||||
process::exit(0);
|
process::exit(0);
|
||||||
},
|
},
|
||||||
BranchType::Error => {
|
WhenFileExists::Error => {
|
||||||
log::error!("File exists, exiting.");
|
log::error!("File exists, exiting.");
|
||||||
process::exit(1);
|
process::exit(1);
|
||||||
}
|
},
|
||||||
BranchType::Override => (),
|
WhenFileExists::CheckContent => {
|
||||||
};
|
let content = fs::read_to_string(path.clone().unwrap())?;
|
||||||
|
|
||||||
let content = fs::read_to_string(path.clone().unwrap())?;
|
let non_matching_characters = character_set.non_matching_characters(&content);
|
||||||
|
let content_length = content.len();
|
||||||
|
|
||||||
let non_matching_characters = character_set.non_matching_characters(&content);
|
if non_matching_characters.is_empty() && content_length == length {
|
||||||
let content_length = content.len();
|
log::debug!("File exists, valid character set and length, exiting.");
|
||||||
|
|
||||||
if non_matching_characters.is_empty() && content_length == length {
|
|
||||||
log::debug!("File exists, matches character set and length, exiting.");
|
|
||||||
|
|
||||||
return Ok(());
|
|
||||||
} else {
|
|
||||||
let found_non_matching = non_matching_characters.iter().take(5).collect::<String>();
|
|
||||||
let found_non_matching = escape_string::escape(found_non_matching.as_str());
|
|
||||||
let found_non_matching = if non_matching_characters.len() > 5 {
|
|
||||||
found_non_matching + "..."
|
|
||||||
} else {
|
|
||||||
found_non_matching
|
|
||||||
};
|
|
||||||
|
|
||||||
match arguments.when_file_content_does_not_match {
|
|
||||||
BranchType::Exit => {
|
|
||||||
log::info!("File exists, does not match character set (found [{}]) or length (found {}), exiting.", found_non_matching, content_length);
|
|
||||||
process::exit(0);
|
process::exit(0);
|
||||||
},
|
} else {
|
||||||
BranchType::Error => {
|
let found_non_matching = non_matching_characters.iter().take(5).collect::<String>();
|
||||||
log::error!("File exists, does not match character set (found [{}]) or length (found {}), exiting.", found_non_matching, content_length);
|
let found_non_matching = escape_string::escape(found_non_matching.as_str());
|
||||||
process::exit(1);
|
let found_non_matching = if non_matching_characters.len() > 5 {
|
||||||
},
|
found_non_matching + "..."
|
||||||
BranchType::Override => {
|
} else {
|
||||||
log::debug!("File exists, does not match character set (found [{}]) or length (found {}).", found_non_matching, content_length);
|
found_non_matching
|
||||||
},
|
};
|
||||||
};
|
|
||||||
}
|
match arguments.when_file_content_is_invalid {
|
||||||
|
WhenFileContentIsInvalid::Exit => {
|
||||||
|
log::info!("File exists, invalid character set (found [{}]) or length (found {}), exiting.", found_non_matching, content_length);
|
||||||
|
process::exit(0);
|
||||||
|
},
|
||||||
|
WhenFileContentIsInvalid::Error => {
|
||||||
|
log::error!("File exists, invalid character set (found [{}]) or length (found {}), exiting.", found_non_matching, content_length);
|
||||||
|
process::exit(1);
|
||||||
|
},
|
||||||
|
WhenFileContentIsInvalid::Override => {
|
||||||
|
log::debug!("File exists, invalid character set (found [{}]) or length (found {}).", found_non_matching, content_length);
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
},
|
||||||
|
WhenFileExists::Override => (),
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
log::debug!("Generating using {} with length {}", character_set, length);
|
log::debug!("Generating using {} with length {}", character_set, length);
|
||||||
|
|
Loading…
Reference in a new issue