1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
//! # randselect
//! This crate provides a simple command line utility for randomly selecting N
//! files from a directory and copying/moving them to a target directory.
//!
//! `randselect` operates (inefficiently) by generating a random permutation of
//! the files in a given directory, then moving/copying the first N files in
//! the resulting permutation to a target directory.
//!
//! ```txt
//! randselect
//! Tool for randomly selecting files from a directory.
//!
//! USAGE:
//!     randselect [FLAGS] [OPTIONS] -i <IN_DIR> -n <N> -o <OUT_DIR>
//!
//! FLAGS:
//!     -g, --go          Execute the copy or move. Specify a seed for deterministic behavior.
//!     -h, --help        Prints help information
//!     -m                Whether to move the selected files rather than copy.
//!     -c, --no_color    Disable colorized output. Only supported in Unix-like OSes.
//!     -V, --version     Prints version information
//!     -v                Sets the level of verbosity
//!
//! OPTIONS:
//!     -i <IN_DIR>         The input directory to select from.
//!     -n <N>              The number of files to select.
//!     -o <OUT_DIR>        The directory to output to. Will be created if it doesn't exist.
//!     -s <SEED>           The seed to use for the PRNG (u64).
//! ```

extern crate chrono;
extern crate colored;
#[macro_use]
extern crate log;
extern crate fern;
extern crate rand;

pub mod utils;

use std::fs;
use std::io::{Error, ErrorKind};

use colored::Colorize;
use rand::prelude::{SeedableRng, SliceRandom, StdRng};
use rand::FromEntropy;

#[derive(Debug)]
pub struct Args {
    pub verbosity: u8,
    pub out_dir: String,
    pub in_dir: String,
    pub num_files: usize,
    pub move_files: bool,
    pub go: bool,
    pub no_color: bool,
    pub seed: Option<u64>,
}

/// Return a shuffled vector of paths based on the seed, if provided.
fn get_shuffled_paths(args: &Args) -> Result<Vec<fs::DirEntry>, Error> {
    match fs::read_dir(&args.in_dir) {
        Ok(paths) => {
            // Use seed if provided, else random entropy
            let mut rng: StdRng = match args.seed {
                // NOTE: Not cryptographically secure, but good enough for us.
                Some(seed) => SeedableRng::seed_from_u64(seed),
                None => StdRng::from_entropy(),
            };

            let mut vec_paths: Vec<_> = paths.map(|r| r.unwrap()).collect();

            // Generate a random permutation of the files
            vec_paths.shuffle(&mut rng);
            trace!{"Shuffled: {:#?}", vec_paths};
            return Ok(vec_paths);
        }
        Err(e) => Err(e),
    }
}

fn paths_are_valid(in_dir: &str, out_dir: &str) -> Result<(), Error> {
    let in_path = fs::canonicalize(in_dir)?;
    if !in_path.is_dir() {
        error!{"Input directory is not a directory: {}", in_dir};
        return Err(Error::new(
            ErrorKind::InvalidInput,
            "Input directory is not a directory.",
        ));
    }

    // Only check output directory if it exists
    if let Ok(out_path) = fs::canonicalize(out_dir) {
        if in_path == out_path {
            error!(
                "The output directory cannot be the same as the input directory.\n{} == {}",
                in_path.to_str().unwrap(),
                out_path.to_str().unwrap()
            );
            return Err(Error::new(
                ErrorKind::InvalidInput,
                "Output and input directory were the same.",
            ));
        }
    }

    Ok(())
}

/// The primary driver of the library to process the provided Args.
pub fn run(args: &mut Args) -> Result<(), Error> {
    debug!{"Input args: {:#?}", args};

    paths_are_valid(args.in_dir.as_str(), args.out_dir.as_str())?;

    if args.no_color {
        colored::control::set_override(false);
    }

    match get_shuffled_paths(args) {
        Ok(paths) => {
            let selected_files: Vec<&fs::DirEntry> = paths.iter().take(args.num_files).collect();
            for file in selected_files {
                let dest = format!(
                    "{}/{}",
                    args.out_dir,
                    file.file_name().into_string().unwrap()
                );
                // Delete file if move
                if args.move_files {
                    println!("-- {}", file.path().to_str().unwrap().red());
                }
                println!("++ {}", dest.green());
                if args.go {
                    // Create output dir
                    fs::create_dir_all(&args.out_dir).unwrap();

                    // Copy file
                    fs::copy(file.path(), &dest).unwrap();

                    // Delete file if move
                    if args.move_files {
                        fs::remove_file(file.path()).unwrap();
                    }
                }
            }
            if !args.go {
                println!("Re-run randselect with --go to write these changes to the filesystem.");
            }
            return Ok(());
        }
        Err(e) => Err(e),
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn test_valid_paths() {
        paths_are_valid(".", "/tmp/randselect").expect("Paths are valid.");
    }

    #[test]
    fn test_invalid_paths() {
        if let Ok(_) = paths_are_valid(".", ".") {
            panic!("Should have failed with same paths");
        }
    }

}