fn run_with_args(args: &ArgMatches) {
    match args.subcommand() {
        (SUBCOMMAND_SHARE, Some(sub_matches)) => {
            // Create subcommand main arguments
            let file = sub_matches.value_of(ARG_INPUT).unwrap();

            match std::fs::metadata(file) {
                Ok(metadata) => {
                    if metadata.len() == 0 {
                        println!(
                            "'{}' is an empty file. Secret cannot be an empty file. Aborting.",
                            file
                        );
                        println!("Aborting");
                        return;
                    }
                }
                Err(e) => {
                    println!("Error reading in secret input file '{}': {}", file, e);
                    println!("Aborting");
                    return;
                }
            }

            let out_file_stem = sub_matches.value_of(ARG_OUTPUT_STEM).unwrap_or(
                Path::new(sub_matches.value_of(ARG_INPUT).unwrap())
                    .file_stem()
                    .expect("Invalid input file path.")
                    .to_str()
                    .unwrap(),
            );

            let out_dir = sub_matches.value_of(ARG_OUTPUT_DIR).unwrap_or(".");

            let shares_to_create = sub_matches
                .value_of(ARG_SHARES_TO_CREATE)
                .unwrap()
                .parse::<u8>()
                .unwrap();

            let shares_needed = sub_matches
                .value_of(ARG_SHARES_NEEDED)
                .unwrap_or(sub_matches.value_of(ARG_SHARES_TO_CREATE).unwrap())
                .parse::<u8>()
                .unwrap();

            let confirm = sub_matches.is_present(ARG_NO_CONFIRM_RECON);

            // Error checking of number of shares values
            if shares_to_create < shares_needed {
                println!(
                    "Shares to create must be greater than or equal to shares needed:
                         Shares to create: {}
                         Shares needed for reconstruction: {}",
                    shares_to_create, shares_needed
                );
                println!("{}", CREATE_ABORT);
                return;
            }
            if shares_to_create < 2 || shares_needed < 2 {
                println!(
                    "At least two shares are needed to split a secret:
                         Shares to create: {}
                         Shares needed for reconstruction: {}",
                    shares_to_create, shares_needed
                );
                println!("{}", CREATE_ABORT);
                return;
            }

            let secret = Secret::point_at_file(file);

            // Create a vec of writable files for sharing.
            let mut dests: Vec<Box<dyn Write>> = (0..(shares_to_create as usize))
                .map(|share_num| {
                    let path =
                        Path::new(out_dir).join(format!("{}_{}.sss", out_file_stem, share_num));
                    Box::new(
                        std::fs::OpenOptions::new()
                            .create(true)
                            .write(true)
                            .open(&path)
                            .expect(&format!(
                                "Failed to open file '{}' for writing.",
                                path.as_path().to_str().unwrap()
                            )),
                    ) as Box<dyn Write>
                })
                .collect();
            share_to_writables(secret, &mut dests, shares_needed, shares_to_create, confirm)
                .expect("Failed to share secret.");

        }
        (SUBCOMMAND_RECONSTRUCT, Some(sub_matches)) => {
            let mut src_len = 0u64;
            let mut input_files: Vec<Box<dyn Read>> = sub_matches
                .values_of(ARG_INPUTS)
                .expect("No input values received?")
                .map(|path| {
                    src_len = std::fs::metadata(&path).expect(&format!("Could not open file '{}'", &path)).len();
                    Box::new(
                    File::open(&path).expect(&format!("Could not open file '{}'", &path))) as Box<dyn Read> 
                })
                .collect();
            
            let confirm = sub_matches.is_present(ARG_NO_CONFIRM_RECON);

            let secret_out = sub_matches.value_of(ARG_OUTPUT_FILE).unwrap();
            let mut secret = Secret::point_at_file(secret_out);
            reconstruct_from_srcs(&mut secret, &mut input_files, src_len, confirm)
                .expect("Reconstruction failed");

        }
        _ => (), // No subcommand was run?
    }
}
// This exists as a separate function so a GUI interface can be developed and make use of this
// in the future.
This exists as a separate function so a GUI interface can be developed and make use of this
in the future.
This exists as a separate function so a GUI interface can be developed and make use of this in the future .
fn run_with_args(args: &ArgMatches) {
    match args.subcommand() {
        (SUBCOMMAND_SHARE, Some(sub_matches)) => {
            // Create subcommand main arguments
            let file = sub_matches.value_of(ARG_INPUT).unwrap();
            match std::fs::metadata(file) {
                Ok(metadata) => {
                    if metadata.len() == 0 {
                        println!(
                            "'{}' is an empty file. Secret cannot be an empty file. Aborting.",
                            file
                        );
                        println!("Aborting");
                        return;
                    }
                }
                Err(e) => {
                    println!("Error reading in secret input file '{}': {}", file, e);
                    println!("Aborting");
                    return;
                }
            }
            let out_file_stem = sub_matches.value_of(ARG_OUTPUT_STEM).unwrap_or(
                Path::new(sub_matches.value_of(ARG_INPUT).unwrap())
                    .file_stem()
                    .expect("Invalid input file path.")
                    .to_str()
                    .unwrap(),
            );
            let out_dir = sub_matches.value_of(ARG_OUTPUT_DIR).unwrap_or(".");
            let shares_to_create = sub_matches
                .value_of(ARG_SHARES_TO_CREATE)
                .unwrap()
                .parse::<u8>()
                .unwrap();
            let shares_needed = sub_matches
                .value_of(ARG_SHARES_NEEDED)
                .unwrap_or(sub_matches.value_of(ARG_SHARES_TO_CREATE).unwrap())
                .parse::<u8>()
                .unwrap();
            let confirm = sub_matches.is_present(ARG_NO_CONFIRM_RECON);
            // Error checking of number of shares values
            if shares_to_create < shares_needed {
                println!(
                    "Shares to create must be greater than or equal to shares needed:
                         Shares to create: {}
                         Shares needed for reconstruction: {}",
                    shares_to_create, shares_needed
                );
                println!("{}", CREATE_ABORT);
                return;
            }
            if shares_to_create < 2 || shares_needed < 2 {
                println!(
                    "At least two shares are needed to split a secret:
                         Shares to create: {}
                         Shares needed for reconstruction: {}",
                    shares_to_create, shares_needed
                );
                println!("{}", CREATE_ABORT);
                return;
            }
            let secret = Secret::point_at_file(file);
            // Create a vec of writable files for sharing.
            let mut dests: Vec<Box<dyn Write>> = (0..(shares_to_create as usize))
                .map(|share_num| {
                    let path =
                        Path::new(out_dir).join(format!("{}_{}.sss", out_file_stem, share_num));
                    Box::new(
                        std::fs::OpenOptions::new()
                            .create(true)
                            .write(true)
                            .open(&path)
                            .expect(&format!(
                                "Failed to open file '{}' for writing.",
                                path.as_path().to_str().unwrap()
                            )),
                    ) as Box<dyn Write>
                })
                .collect();
            share_to_writables(secret, &mut dests, shares_needed, shares_to_create, confirm)
                .expect("Failed to share secret.");
        }
        (SUBCOMMAND_RECONSTRUCT, Some(sub_matches)) => {
            let mut src_len = 0u64;
            let mut input_files: Vec<Box<dyn Read>> = sub_matches
                .values_of(ARG_INPUTS)
                .expect("No input values received?")
                .map(|path| {
                    src_len = std::fs::metadata(&path).expect(&format!("Could not open file '{}'", &path)).len();
                    Box::new(
                    File::open(&path).expect(&format!("Could not open file '{}'", &path))) as Box<dyn Read> 
                })
                .collect();
            let confirm = sub_matches.is_present(ARG_NO_CONFIRM_RECON);
            let secret_out = sub_matches.value_of(ARG_OUTPUT_FILE).unwrap();
            let mut secret = Secret::point_at_file(secret_out);
            reconstruct_from_srcs(&mut secret, &mut input_files, src_len, confirm)
                .expect("Reconstruction failed");
        }
        _ => (), // No subcommand was run?
    }
}
fn run_with_args ( args : & ArgMatches ) { match args . subcommand ( ) { ( SUBCOMMAND_SHARE , Some ( sub_matches ) ) => { // Create subcommand main arguments let file = sub_matches . value_of ( ARG_INPUT ) . unwrap ( ) ; match std :: fs :: metadata ( file ) { Ok ( metadata ) => { if metadata . len ( ) == 0 { println ! ( " " file ) ; println ! ( " " ) ; return ; } } Err ( e ) => { println ! ( " " file e ) ; println ! ( " " ) ; return ; } } let out_file_stem = sub_matches . value_of ( ARG_OUTPUT_STEM ) . unwrap_or ( Path :: new ( sub_matches . value_of ( ARG_INPUT ) . unwrap ( ) ) . file_stem ( ) . expect ( " " ) . to_str ( ) . unwrap ( ) , ) ; let out_dir = sub_matches . value_of ( ARG_OUTPUT_DIR ) . unwrap_or ( " " ) ; let shares_to_create = sub_matches . value_of ( ARG_SHARES_TO_CREATE ) . unwrap ( ) . parse :: < u8 > ( ) . unwrap ( ) ; let shares_needed = sub_matches . value_of ( ARG_SHARES_NEEDED ) . unwrap_or ( sub_matches . value_of ( ARG_SHARES_TO_CREATE ) . unwrap ( ) ) . parse :: < u8 > ( ) . unwrap ( ) ; let confirm = sub_matches . is_present ( ARG_NO_CONFIRM_RECON ) ; // Error checking of number of shares values if shares_to_create < shares_needed { println ! ( " " shares_to_create shares_needed ) ; println ! ( " " CREATE_ABORT ) ; return ; } if shares_to_create < 2 || shares_needed < 2 { println ! ( " " shares_to_create shares_needed ) ; println ! ( " " CREATE_ABORT ) ; return ; } let secret = Secret :: point_at_file ( file ) ; // Create a vec of writable files for sharing. let mut dests : Vec < Box < dyn Write > > = ( 0 .. ( shares_to_create as usize ) ) . map ( | share_num | { let path = Path :: new ( out_dir ) . join ( format ! ( " " out_file_stem share_num ) ) ; Box :: new ( std :: fs :: OpenOptions :: new ( ) . create ( true ) . write ( true ) . open ( & path ) . expect ( & format ! ( " " path as_path ( ) to_str ( ) unwrap ( ) ) ) , ) as Box < dyn Write > } ) . collect ( ) ; share_to_writables ( secret , & mut dests , shares_needed , shares_to_create , confirm ) . expect ( " " ) ; } ( SUBCOMMAND_RECONSTRUCT , Some ( sub_matches ) ) => { let mut src_len = 0u64 ; let mut input_files : Vec < Box < dyn Read > > = sub_matches . values_of ( ARG_INPUTS ) . expect ( " " ) . map ( | path | { src_len = std :: fs :: metadata ( & path ) . expect ( & format ! ( " " path ) ) . len ( ) ; Box :: new ( File :: open ( & path ) . expect ( & format ! ( " " path ) ) ) as Box < dyn Read > } ) . collect ( ) ; let confirm = sub_matches . is_present ( ARG_NO_CONFIRM_RECON ) ; let secret_out = sub_matches . value_of ( ARG_OUTPUT_FILE ) . unwrap ( ) ; let mut secret = Secret :: point_at_file ( secret_out ) ; reconstruct_from_srcs ( & mut secret , & mut input_files , src_len , confirm ) . expect ( " " ) ; } _ => ( ) , // No subcommand was run? } }
This exists as a separate function so a GUI interface can be developed and make use of this
in the future.
This exists as a separate function so a GUI interface can be developed and make use of this in the future .
args
&ArgMatches
args
&ArgMatches