Skip to content
Snippets Groups Projects
Verified Commit 17ebe9bd authored by Louis's avatar Louis :fire:
Browse files

Special case empty actions array to do a direct file copy

parent 6aa6cfc7
No related branches found
No related tags found
No related merge requests found
#!/usr/bin/env sh
cargo build --release
strip release/target/crunch
chmod u+x release/target/crunch
cp release/target/crunch "$HOME/.local/bin/crunch"
\ No newline at end of file
strip target/release/crunch
chmod u+x target/release/crunch
cp target/release/crunch "$HOME/.local/bin/crunch"
\ No newline at end of file
use glob::{glob_with, MatchOptions};
use std::collections::HashMap;
use std::path::{Path, PathBuf};
......@@ -7,6 +6,7 @@ use serde::{Deserialize, Serialize};
use thiserror::Error;
use crate::cli_args::CrunchCommand;
use crate::format::make_paths;
use crate::{commands, load_image};
#[derive(Error, Debug)]
......@@ -50,19 +50,44 @@ pub fn execute_pipeline<IN: ToString, OUT: ToString>(
file_path: IN,
_outpath: OUT,
) -> anyhow::Result<()> {
let path = file_path.to_string();
if !&path.ends_with(".toml") && !&path.ends_with(".json") {
let path = std::env::current_dir().map(|path| path.join(file_path.to_string()))?;
let path_string = format!("{}", &path.display());
log::debug!("Trying to read from input file: {}", &path.display());
if !&path_string.ends_with(".toml") && !&path_string.ends_with(".json") {
Err(PipelineError::FormatDetection)?;
}
let file_contents = std::fs::read(file_path.to_string())?;
let pipeline_data: PipelineFile = if path.ends_with(".toml") {
let file_contents = std::fs::read(path)?;
log::debug!("Found correct file type and read bytes, trying to parse");
let pipeline_data: PipelineFile = if path_string.ends_with(".toml") {
toml::from_slice(&file_contents)?
} else {
serde_json::from_slice(&file_contents)?
};
log::debug!("Expanding pipeline file into targets");
get_targets(&pipeline_data).for_each(|(input_path, output_path, actions)| {
match make_paths(&output_path) {
Ok(_) => {}
Err(e) => {
log::error!("Failed to create target directory {}; {}", &output_path, e);
return;
}
}
if actions.is_empty() {
match std::fs::copy(&input_path, &output_path) {
Ok(_) => {}
Err(e) => {
log::error!("Failed to copy {} to {}; {}", input_path, output_path, e);
}
};
return;
}
let mut file = match load_image(&input_path, None) {
Ok(image) => image,
Err(e) => {
......@@ -71,6 +96,11 @@ pub fn execute_pipeline<IN: ToString, OUT: ToString>(
}
};
log::debug!(
"Loaded {}, Executing {} actions",
&input_path,
actions.len()
);
let mut count = 1;
for step in actions {
match step {
......@@ -248,15 +278,9 @@ fn get_targets(
.map(|value| (*value).actions.clone())
.flat_map(|actions| {
let mut paths = Vec::new();
for entry in glob_with(
pattern.as_str(),
MatchOptions {
case_sensitive: true,
..Default::default()
},
)
.unwrap()
{
log::debug!("Mapping glob paths for '{}'", pattern.as_str());
for entry in glob::glob(pattern.as_str()).unwrap() {
log::debug!("Found a glob match: [{:?}]", entry);
paths.push((actions.clone(), entry));
}
paths
......
......@@ -64,3 +64,11 @@ pub fn load_image<T: AsRef<Path>>(
Ok(file)
}
pub fn make_paths<T: AsRef<Path>>(path: T) -> anyhow::Result<(), std::io::Error> {
if let Some(target) = path.as_ref().parent() {
std::fs::create_dir_all(target)
} else {
Ok(())
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment