Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

return exit code depending on the operation result #7

Merged
merged 1 commit into from
Jul 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions src/handlers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,49 +15,49 @@ mod operations;
mod utils;

fn file_iteration_handler(environment: &Environment,
operation: &dyn FileOperation<Context=FileOperationContext>) {
operation: &dyn FileOperation<Context=FileOperationContext>) -> Result<(), String> {
iterate_files(environment.current_dir(),
&FileOperationContext::create(environment),
operation)
}

pub fn check(environment: &Environment,
logger: &Logger) {
logger: &Logger) -> Result<(), String> {
file_iteration_handler(environment,
&LoggedOperation::wrap(logger,
&CheckFileOperation {}))
}

pub fn link(environment: &Environment,
logger: &Logger) {
logger: &Logger) -> Result<(), String> {
file_iteration_handler(environment,
&LoggedOperation::wrap(logger,
&LinkFileOperation {}))
}

pub fn unlink(environment: &Environment,
logger: &Logger) {
logger: &Logger) -> Result<(), String> {
file_iteration_handler(environment,
&LoggedOperation::wrap(logger,
&UnlinkFileOperation {}))
}

pub fn list(environment: &Environment,
logger: &Logger) {
logger: &Logger) -> Result<(), String> {
file_iteration_handler(environment,
&LoggedOperation::wrap(logger,
&ListFileOperation {}))
}

pub fn list_backup(environment: &Environment,
logger: &Logger) {
logger: &Logger) -> Result<(), String> {
file_iteration_handler(environment,
&LoggedOperation::wrap(logger,
&ListBackupOperation {}))
}

pub fn remove_backup(environment: &Environment,
logger: &Logger) {
logger: &Logger) -> Result<(), String> {
file_iteration_handler(environment,
&LoggedOperation::wrap(logger,
&RemoveBackupOperation {}))
Expand Down
4 changes: 1 addition & 3 deletions src/handlers/operations/link_operation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,7 @@ impl FileOperation for LinkFileOperation {
.map_err(|e| e.to_string())?;
}
match symlink::symlink_file(repository_file_path, &home_file_path) {
Ok(_) => {
Ok(())
}
Ok(_) => Ok(()),
Err(e) => {
match backup_file_result {
None => {}
Expand Down
15 changes: 10 additions & 5 deletions src/handlers/utils/file_operation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,18 @@ pub trait FileOperation {
pub fn iterate_files<TContext>(root: &PathBuf,
context: &TContext,
file_operation: &dyn FileOperation<Context=TContext>,
) {
for entry in WalkDir::new(root)
) -> Result<(), String> {
WalkDir::new(root)
.sort_by(|a, b| a.file_name().cmp(b.file_name()))
.into_iter()
.filter(|entry| entry.is_ok())
.map(|entry| entry.unwrap())
.filter(|entry| !entry.file_type().is_dir()) {
let _ = file_operation.call(context, &entry);
}
.filter(|entry| !entry.file_type().is_dir())
.fold(Ok(()), |result, entry| {
let operation_result = file_operation.call(context, &entry);
match operation_result {
Ok(_) => result,
Err(_) => operation_result
}
})
}
4 changes: 2 additions & 2 deletions src/handlers/utils/logged_operation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ impl FileOperation for LoggedOperation<'_, FileOperationContext> {
self.logger.log(LogLevel::Error,
&format!("{} - {}",
entry_path_str,
result.unwrap_err()))
result.as_ref().unwrap_err()))
} else {
self.logger.log(LogLevel::Info,
&format!("{}",
entry_path_str))
}
Ok(())
result
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::cli_arguments::{BackupSubcommand};
use crate::cli_arguments::Command::{Backup, Link, List, Unlink, Check};
use std::process::exit;

mod environment;
mod cli_arguments;
Expand All @@ -11,7 +12,7 @@ fn main() {
let cli_arguments = cli_arguments::arguments();
let logger = log::create(cli_arguments.verbose());

match cli_arguments.command() {
let result = match cli_arguments.command() {
Link(_) => handlers::link(&environment, &logger),
Unlink(_) => handlers::unlink(&environment, &logger),
List(_) => handlers::list(&environment, &logger),
Expand All @@ -24,5 +25,6 @@ fn main() {
}
}
Check(_) => handlers::check(&environment, &logger),
}
};
exit(result.map_or(1, |_| 0));
}