From 0e1ea242ca9b923ef14601f9f7b9ec23cda87d94 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Tue, 4 Jul 2017 18:16:50 +0200 Subject: [PATCH] Fix: Getting entries from appropriate subcommand --- imag-link/src/main.rs | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/imag-link/src/main.rs b/imag-link/src/main.rs index d286a659..240b59bb 100644 --- a/imag-link/src/main.rs +++ b/imag-link/src/main.rs @@ -133,20 +133,9 @@ fn handle_internal_linking(rt: &Runtime) { debug!("Listing ready!"); }, None => { - let mut from = match get_from_entry(&rt) { - None => warn_exit("No 'from' entry", 1), - Some(s) => s, - }; - debug!("Link from = {:?}", from.deref()); - - let to = match get_to_entries(&rt) { - None => warn_exit("No 'to' entry", 1), - Some(to) => to, - }; - debug!("Link to = {:?}", to.iter().map(|f| f.deref()).collect::>()); - match cmd.subcommand_name() { Some("add") => { + let (mut from, to) = get_from_to_entry(&rt, "add"); for mut to_entry in to { if let Err(e) = to_entry.add_internal_link(&mut from) { trace_error_exit(&e, 1); @@ -155,6 +144,7 @@ fn handle_internal_linking(rt: &Runtime) { }, Some("remove") => { + let (mut from, to) = get_from_to_entry(&rt, "remove"); for mut to_entry in to { if let Err(e) = to_entry.remove_internal_link(&mut from) { trace_error_exit(&e, 1); @@ -168,11 +158,27 @@ fn handle_internal_linking(rt: &Runtime) { } } -fn get_from_entry<'a>(rt: &'a Runtime) -> Option> { +fn get_from_to_entry<'a>(rt: &'a Runtime, subcommand: &str) -> (FileLockEntry<'a>, Vec>) { + let from = match get_from_entry(&rt, subcommand) { + None => warn_exit("No 'from' entry", 1), + Some(s) => s, + }; + debug!("Link from = {:?}", from.deref()); + + let to = match get_to_entries(&rt, subcommand) { + None => warn_exit("No 'to' entry", 1), + Some(to) => to, + }; + debug!("Link to = {:?}", to.iter().map(|f| f.deref()).collect::>()); + + (from, to) +} + +fn get_from_entry<'a>(rt: &'a Runtime, subcommand: &str) -> Option> { rt.cli() .subcommand_matches("internal") .unwrap() // safe, we know there is an "internal" subcommand" - .subcommand_matches("add") + .subcommand_matches(subcommand) .unwrap() // safe, we know there is an "add" subcommand .value_of("from") .and_then(|from_name| { @@ -188,11 +194,11 @@ fn get_from_entry<'a>(rt: &'a Runtime) -> Option> { }) } -fn get_to_entries<'a>(rt: &'a Runtime) -> Option>> { +fn get_to_entries<'a>(rt: &'a Runtime, subcommand: &str) -> Option>> { rt.cli() .subcommand_matches("internal") .unwrap() // safe, we know there is an "internal" subcommand" - .subcommand_matches("add") + .subcommand_matches(subcommand) .unwrap() // safe, we know there is an "add" subcommand .values_of("to") .map(|values| {