1
0
mirror of https://github.com/pcvolkmer/osc-variant.git synced 2025-04-19 19:56:50 +00:00

refactor: extract command handling into functions

This commit is contained in:
Paul-Christian Volkmer 2025-01-08 00:00:41 +01:00
parent 949606456e
commit dd4e1499b7

View File

@ -23,7 +23,7 @@ use crate::file_io::{FileError, FileReader, InputFile};
use crate::model::onkostar_editor::OnkostarEditor; use crate::model::onkostar_editor::OnkostarEditor;
use crate::profile::Profile; use crate::profile::Profile;
use clap::CommandFactory; use clap::CommandFactory;
use clap_complete::generate; use clap_complete::{generate, Shell};
use console::style; use console::style;
use dialoguer::Confirm; use dialoguer::Confirm;
use quick_xml::se::Serializer; use quick_xml::se::Serializer;
@ -36,6 +36,60 @@ use std::io::Write;
use std::ops::Add; use std::ops::Add;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
pub fn handle(command: SubCommand) -> Result<(), Box<dyn Error>> {
match command {
SubCommand::Completion { shell } => handle_completion(shell),
SubCommand::List {
inputfile,
sorted,
filter,
} => handle_list(inputfile, sorted, filter)?,
SubCommand::Tree {
inputfile,
sorted,
filter,
} => handle_tree(inputfile, sorted, filter)?,
SubCommand::Modify {
inputfile,
profile,
outputfile,
compact,
sorted,
strip,
interactive,
fix,
} => handle_modify(
inputfile,
profile,
outputfile,
compact,
sorted,
strip,
interactive,
fix,
)?,
SubCommand::Diff {
inputfile_a,
inputfile_b,
strict,
} => handle_diff(&inputfile_a, &inputfile_b, strict)?,
SubCommand::Sha256Sum { inputfile } => handle_sha256sum(inputfile),
SubCommand::Check {
file,
list,
password,
} => handle_check(file, list, password),
#[cfg(feature = "unzip-osb")]
SubCommand::UnzipOsb {
file,
password,
dir,
} => handle_unzip_osb(&file, password, dir),
}
Ok(())
}
fn write_outputfile(filename: String, content: &String) -> Result<(), FileError> { fn write_outputfile(filename: String, content: &String) -> Result<(), FileError> {
OpenOptions::new() OpenOptions::new()
.read(false) .read(false)
@ -49,9 +103,7 @@ fn write_outputfile(filename: String, content: &String) -> Result<(), FileError>
Ok(()) Ok(())
} }
pub fn handle(command: SubCommand) -> Result<(), Box<dyn Error>> { fn handle_completion(shell: Shell) {
match command {
SubCommand::Completion { shell } => {
let command = &mut Cli::command(); let command = &mut Cli::command();
generate( generate(
shell, shell,
@ -59,12 +111,14 @@ pub fn handle(command: SubCommand) -> Result<(), Box<dyn Error>> {
command.get_name().to_string(), command.get_name().to_string(),
&mut std::io::stdout(), &mut std::io::stdout(),
); );
} }
SubCommand::List {
inputfile, fn handle_list(
sorted, inputfile: String,
filter, sorted: bool,
} => match InputFile::read(inputfile, None)? { filter: Option<String>,
) -> Result<(), Box<dyn Error>> {
match InputFile::read(inputfile, None)? {
osc @ InputFile::Osc { .. } => { osc @ InputFile::Osc { .. } => {
let mut content: OnkostarEditor = osc.try_into()?; let mut content: OnkostarEditor = osc.try_into()?;
if sorted { if sorted {
@ -121,12 +175,16 @@ pub fn handle(command: SubCommand) -> Result<(), Box<dyn Error>> {
"Nur OSB- und OSC-Dateien werden unterstützt".to_string(), "Nur OSB- und OSC-Dateien werden unterstützt".to_string(),
))) )))
} }
}, }
SubCommand::Tree { Ok(())
inputfile, }
sorted,
filter, fn handle_tree(
} => match InputFile::read(inputfile, None)? { inputfile: String,
sorted: bool,
filter: Option<String>,
) -> Result<(), Box<dyn Error>> {
match InputFile::read(inputfile, None)? {
osc @ InputFile::Osc { .. } => { osc @ InputFile::Osc { .. } => {
let mut content: OnkostarEditor = osc.try_into()?; let mut content: OnkostarEditor = osc.try_into()?;
if sorted { if sorted {
@ -138,28 +196,34 @@ pub fn handle(command: SubCommand) -> Result<(), Box<dyn Error>> {
} }
OnkostarEditor::print_tree(&content); OnkostarEditor::print_tree(&content);
} }
InputFile::Osb { filename, .. } => return Err(Box::new(FileError::Reading( InputFile::Osb { filename, .. } => {
return Err(Box::new(FileError::Reading(
filename, filename,
"Nur OSC-Dateien werden unterstützt. OSB-Dateien erzeugen eine zu lange Ausgabe." "Nur OSC-Dateien werden unterstützt. OSB-Dateien erzeugen eine zu lange Ausgabe."
.to_string(), .to_string(),
))), )))
}
InputFile::Yaml { filename, .. } | InputFile::Other { filename, .. } => { InputFile::Yaml { filename, .. } | InputFile::Other { filename, .. } => {
return Err(Box::new(FileError::Reading( return Err(Box::new(FileError::Reading(
filename, filename,
"Nur OSC-Dateien werden unterstützt".to_string(), "Nur OSC-Dateien werden unterstützt".to_string(),
))) )))
} }
}, }
SubCommand::Modify {
inputfile, Ok(())
profile, }
outputfile,
compact, fn handle_modify(
sorted, inputfile: String,
strip, profile: Option<String>,
interactive, outputfile: Option<String>,
fix, compact: bool,
} => { sorted: bool,
strip: bool,
interactive: bool,
fix: bool,
) -> Result<(), Box<dyn Error>> {
let mut data: OnkostarEditor = InputFile::read(inputfile, None)?.try_into()?; let mut data: OnkostarEditor = InputFile::read(inputfile, None)?.try_into()?;
if let Some(profile) = profile { if let Some(profile) = profile {
@ -187,9 +251,7 @@ pub fn handle(command: SubCommand) -> Result<(), Box<dyn Error>> {
sorted = matches!( sorted = matches!(
Confirm::new() Confirm::new()
.with_prompt( .with_prompt("Sortiere Kataloge und Formulare nach Name und Abhängigkeiten?")
"Sortiere Kataloge und Formulare nach Name und Abhängigkeiten?"
)
.default(sorted) .default(sorted)
.interact(), .interact(),
Ok(true) Ok(true)
@ -225,9 +287,8 @@ pub fn handle(command: SubCommand) -> Result<(), Box<dyn Error>> {
serializer.indent(' ', 2); serializer.indent(' ', 2);
} }
data.serialize(serializer).map_err(|_| { data.serialize(serializer)
FileError::Writing("Cannot serialize result".to_string(), String::new()) .map_err(|_| FileError::Writing("Cannot serialize result".to_string(), String::new()))?;
})?;
let output = &"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" let output = &"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
.to_string() .to_string()
@ -244,25 +305,28 @@ pub fn handle(command: SubCommand) -> Result<(), Box<dyn Error>> {
None => { None => {
println!("{output}"); println!("{output}");
} }
} };
}
SubCommand::Diff { Ok(())
inputfile_a, }
inputfile_b,
strict, fn handle_diff(inputfile_a: &str, inputfile_b: &str, strict: bool) -> Result<(), Box<dyn Error>> {
} => {
println!( println!(
"Vergleiche Datei A ({}) mit Datei B ({})", "Vergleiche Datei A ({}) mit Datei B ({})",
style(&inputfile_a).yellow(), style(&inputfile_a).yellow(),
style(&inputfile_b).yellow() style(&inputfile_b).yellow()
); );
let data_a = &mut FileReader::<OnkostarEditor>::read(&inputfile_a)?; let data_a = &mut FileReader::<OnkostarEditor>::read(inputfile_a)?;
let data_b = &mut FileReader::<OnkostarEditor>::read(&inputfile_b)?; let data_b = &mut FileReader::<OnkostarEditor>::read(inputfile_b)?;
data_a.print_diff(data_b, strict); data_a.print_diff(data_b, strict);
}
SubCommand::Sha256Sum { inputfile } => match fs::read(inputfile.clone()) { Ok(())
}
fn handle_sha256sum(inputfile: String) {
match fs::read(inputfile.clone()) {
Ok(content) => { Ok(content) => {
let mut hasher = Sha256::new(); let mut hasher = Sha256::new();
hasher.update(content.as_slice()); hasher.update(content.as_slice());
@ -280,12 +344,10 @@ pub fn handle(command: SubCommand) -> Result<(), Box<dyn Error>> {
Err(err) => { Err(err) => {
eprintln!("{}", FileError::Reading(inputfile, err.to_string())); eprintln!("{}", FileError::Reading(inputfile, err.to_string()));
} }
}, }
SubCommand::Check { }
file,
list, fn handle_check(file: Option<String>, list: bool, password: Option<String>) {
password,
} => {
if list { if list {
print(); print();
} else { } else {
@ -310,17 +372,10 @@ pub fn handle(command: SubCommand) -> Result<(), Box<dyn Error>> {
} }
} }
} }
} }
#[cfg(feature = "unzip-osb")]
SubCommand::UnzipOsb { #[cfg(feature = "unzip-osb")]
file, fn handle_unzip_osb(file: &str, password: Option<String>, dir: Option<String>) {
password, use crate::unzip_osb::unzip_osb;
dir, unzip_osb(file, dir.unwrap_or_default().as_str(), password);
} => {
use crate::unzip_osb::unzip_osb;
unzip_osb(file.as_str(), dir.unwrap_or_default().as_str(), password)
}
}
Ok(())
} }