#![ allow( clippy::needless_pass_by_value ) ]
use unilang::semantic::VerifiedCommand;
use unilang::data::{ OutputData, ErrorData };
use unilang::interpreter::ExecutionContext;
use genfile_core::TemplateArchive;
#[ allow( clippy::too_many_lines ) ]
pub fn pack_handler(
cmd : VerifiedCommand,
_ctx : ExecutionContext
) -> Result< OutputData, ErrorData >
{
let input = cmd.get_path( "input" )
.ok_or_else( || crate::error::usage_error( "Missing required parameter: input" ) )?;
let output = cmd.get_path( "output" )
.ok_or_else( || crate::error::usage_error( "Missing required parameter: output" ) )?;
let verbosity = cmd.get_integer( "verbosity" ).unwrap_or( 1 );
let dry = cmd.get_boolean( "dry" ).unwrap_or( false );
if !input.exists()
{
return Err( crate::error::file_error( format!( "Input path does not exist: {}", input.display() ) ) );
}
if !input.is_dir()
{
return Err( crate::error::file_error( format!( "Input must be a directory: {}", input.display() ) ) );
}
let archive_name = input
.file_name()
.and_then( | n | n.to_str() )
.unwrap_or( "archive" );
let archive = TemplateArchive::pack_from_dir( archive_name, input )
.map_err( | e | crate::error::format_error( &e, "PACK" ) )?;
let file_count = archive.file_count();
let archive_name = archive.name.clone();
if dry
{
let output_content = match verbosity
{
0 => String::new(),
1 => format!( "Dry run: Would pack to {}", output.display() ),
_ =>
{
format!(
"Dry run: Would pack archive\n\
Input: {}\n\
Output: {}\n\
Archive: {}\n\
Files: {}\n\
Mode: inline (portable)",
input.display(),
output.display(),
archive_name,
file_count
)
}
};
return Ok( OutputData
{
content : output_content,
format : "text".to_string(),
execution_time_ms : None,
} );
}
archive.save_to_file( output )
.map_err( | e | crate::error::format_error( &e, "PACK" ) )?;
let output_content = match verbosity
{
0 => String::new(),
1 => format!( "Packed archive to: {}", output.display() ),
_ =>
{
format!(
"Packed archive: {}\n\
Input: {}\n\
Output: {}\n\
Files: {}\n\
Mode: inline (portable)",
archive_name,
input.display(),
output.display(),
file_count
)
}
};
Ok( OutputData
{
content : output_content,
format : "text".to_string(),
execution_time_ms : None,
} )
}