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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
use std::convert::TryInto;
use std::ffi::{OsStr, OsString};
use std::io::stdout;
use std::path::PathBuf;
use std::result;
use std::str::FromStr;

use clap::{crate_authors, crate_description, crate_name, crate_version};
use directories::ProjectDirs;

use crate::cmd::{ArgProvider, CmdExec};
use crate::errors::{Error, Result};

pub trait DefaultsProvider {
    fn dir(&self) -> &OsStr;
    fn jobs(&self) -> &str;
}

pub struct Defaults {
    dir: OsString,
    jobs: String,
}

impl Defaults {
    pub fn get() -> Result<Self> {
        let dir = match ProjectDirs::from("", "", "rfz") {
            Some(dirs) => dirs.data_dir().as_os_str().to_owned(),
            None => {
                return Err(Error::UserDirectories(
                    "Failed to infer user directory locations".to_string(),
                ))
            }
        };
        let jobs = num_cpus::get().to_string();
        Ok(Defaults { dir, jobs })
    }
}

impl DefaultsProvider for Defaults {
    fn dir(&self) -> &OsStr {
        &self.dir
    }

    fn jobs(&self) -> &str {
        &self.jobs
    }
}

pub struct Cli<'a> {
    defaults: &'a dyn DefaultsProvider,
    args: clap::ArgMatches<'a>,
}

impl<'a> Cli<'a> {
    pub fn init(defaults: &'a dyn DefaultsProvider) -> Self {
        match Self::init_from(defaults, None) {
            Ok(cli) => cli,
            Err(e) => e.exit(),
        }
    }

    fn init_from(
        defaults: &'a dyn DefaultsProvider,
        argv: Option<Vec<&str>>,
    ) -> result::Result<Self, clap::Error> {
        let app = Cli::build_cli(defaults);
        let args = match argv {
            Some(argv) => app.get_matches_from_safe(argv),
            None => app.get_matches_safe(),
        };
        Ok(Cli {
            defaults,
            args: args?,
        })
    }

    fn build_cli(defaults: &'a dyn DefaultsProvider) -> clap::App {
        clap::app_from_crate!()
            .setting(clap::AppSettings::SubcommandRequired)
            .arg(
                clap::Arg::with_name("jobs")
                    .short("j")
                    .long("jobs")
                    .takes_value(true)
                    .global(true)
                    .default_value(defaults.jobs())
                    .help("Number of concurrent jobs to run"),
            )
            .arg(
                clap::Arg::with_name("dir")
                    .short("d")
                    .long("dir")
                    .takes_value(true)
                    .global(true)
                    .default_value_os(defaults.dir())
                    .help("Directory containing IETF html docs"),
            )
            .arg(
                clap::Arg::with_name("verbosity")
                    .short("v")
                    .multiple(true)
                    .global(true)
                    .help("Increase output verbosity"),
            )
            .subcommand(
                clap::SubCommand::with_name("completions")
                    .about("Print shell completion script")
                    .arg(
                        clap::Arg::with_name("shell")
                            .required(true)
                            .possible_values(&clap::Shell::variants())
                            .help("Shell for which to generate completion script"),
                    ),
            )
            .subcommand(
                clap::SubCommand::with_name("index")
                    .about(
                        "List the latest version of each document \
                         with associated metadata",
                    )
                    .arg(
                        clap::Arg::with_name("type")
                            .short("t")
                            .long("type")
                            .takes_value(true)
                            .multiple(true)
                            .possible_values(&["draft", "rfc", "bcp", "std"])
                            .help("Limit output by document type"),
                    ),
            )
            .subcommand(
                clap::SubCommand::with_name("summary")
                    .about("Print a summary of the metadata in <doc>")
                    .arg(
                        clap::Arg::with_name("doc")
                            .required(true)
                            .help("Path to the document"),
                    ),
            )
            .subcommand(
                clap::SubCommand::with_name("sync")
                    .about("Syncronize the local document mirror")
                    .arg(
                        clap::Arg::with_name("remote")
                            .short("r")
                            .long("remote")
                            .default_value("rsync.tools.ietf.org::tools.html")
                            .help("Remote 'rsync' target to sync from"),
                    )
                    .arg(
                        clap::Arg::with_name("command")
                            .long("command")
                            .default_value("rsync")
                            .help("Rsync command"),
                    ),
            )
    }

    pub fn run(&self) -> Result<()> {
        match self.args.subcommand() {
            ("completions", Some(sub_matches)) => {
                self.print_completions(sub_matches);
                Ok(())
            }
            (subcommand, Some(sub_matches)) => {
                let args = CliArgs::from(sub_matches);
                let exec = CmdExec::init(subcommand, &args)?;
                exec.run()
            }
            _ => Err(Error::CliError("No sub-command was found".to_string())),
        }
    }

    fn print_completions(&self, sub_matches: &clap::ArgMatches) {
        let shell = clap::Shell::from_str(sub_matches.value_of("shell").unwrap()).unwrap();
        let mut app = Cli::build_cli(self.defaults);
        let _stdout = stdout();
        #[cfg(not(test))]
        let mut writer = _stdout.lock();
        #[cfg(test)]
        let mut writer = std::io::sink();
        app.gen_completions_to(crate_name!(), shell, &mut writer);
    }
}

struct CliArgs<'a>(&'a clap::ArgMatches<'a>);

impl<'a> CliArgs<'a> {
    fn from(sub_matches: &'a clap::ArgMatches<'a>) -> Self {
        CliArgs(sub_matches)
    }
}

impl ArgProvider for CliArgs<'_> {
    fn jobs(&self) -> usize {
        usize::from_str(self.0.value_of("jobs").unwrap()).unwrap()
    }

    fn dir(&self) -> PathBuf {
        PathBuf::from(self.0.value_of("dir").unwrap())
    }

    fn verbosity(&self) -> usize {
        match self.0.occurrences_of("verbosity").try_into() {
            Ok(n) => n,
            Err(_) => usize::MAX,
        }
    }

    fn path(&self) -> PathBuf {
        PathBuf::from(self.0.value_of("doc").unwrap())
    }

    fn rsync_cmd(&self) -> &str {
        self.0.value_of("command").unwrap()
    }

    fn rsync_remote(&self) -> &str {
        self.0.value_of("remote").unwrap()
    }

    fn types(&self) -> Option<Vec<&str>> {
        match self.0.values_of("type") {
            Some(values) => Some(values.collect()),
            None => None,
        }
    }
}

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

    use crate::test::resource_path;

    use std::str::FromStr;

    struct DummyDefaults;

    impl DefaultsProvider for DummyDefaults {
        fn jobs(&self) -> &str {
            "1"
        }
        fn dir(&self) -> &OsStr {
            OsStr::new("/home/foo/rfz")
        }
    }

    #[test]
    fn test_cli_defaults() -> Result<()> {
        let defaults = Defaults::get()?;
        assert!(usize::from_str(defaults.jobs()).unwrap() > 0);
        Ok(())
    }

    #[test]
    fn test_empty_args() {
        let defaults = DummyDefaults {};
        let argv = Some(vec!["rfz"]);
        match Cli::init_from(&defaults, argv) {
            Err(e) => assert_eq!(e.kind, clap::ErrorKind::MissingSubcommand),
            Ok(_) => panic!("Expected MissingSubcommand Error"),
        }
    }

    #[test]
    fn test_dummy_index() {
        let defaults = DummyDefaults {};
        let argv = Some(vec!["rfz", "index"]);
        let cli = Cli::init_from(&defaults, argv).unwrap();
        match cli.args.subcommand() {
            (subcommand, Some(args)) => {
                assert_eq!(subcommand, "index");
                let cli_args = CliArgs::from(args);
                assert_eq!(cli_args.jobs(), 1);
                assert_eq!(cli_args.dir(), PathBuf::from("/home/foo/rfz"));
                assert_eq!(cli_args.types(), None);
            }
            _ => panic!("Cli parsing failed"),
        }
    }

    #[test]
    fn test_dummy_index_filtered() {
        let defaults = DummyDefaults {};
        let argv = Some(vec!["rfz", "index", "--type", "rfc"]);
        let cli = Cli::init_from(&defaults, argv).unwrap();
        match cli.args.subcommand() {
            (subcommand, Some(args)) => {
                assert_eq!(subcommand, "index");
                let cli_args = CliArgs::from(args);
                assert_eq!(cli_args.jobs(), 1);
                assert_eq!(cli_args.dir(), PathBuf::from("/home/foo/rfz"));
                assert_eq!(cli_args.types(), Some(vec!["rfc"]));
            }
            _ => panic!("Cli parsing failed"),
        }
    }

    #[test]
    fn test_dummy_summary() {
        let defaults = DummyDefaults {};
        let argv = Some(vec!["rfz", "summary", "/home/foo/rfz/bar.html"]);
        let cli = Cli::init_from(&defaults, argv).unwrap();
        match cli.args.subcommand() {
            (subcommand, Some(args)) => {
                assert_eq!(subcommand, "summary");
                let cli_args = CliArgs::from(args);
                assert_eq!(cli_args.path(), PathBuf::from("/home/foo/rfz/bar.html"));
            }
            _ => panic!("Cli parsing failed"),
        }
    }

    #[test]
    fn test_dummy_sync() {
        let defaults = DummyDefaults {};
        let argv = Some(vec!["rfz", "sync", "-v"]);
        let cli = Cli::init_from(&defaults, argv).unwrap();
        match cli.args.subcommand() {
            (subcommand, Some(args)) => {
                assert_eq!(subcommand, "sync");
                let cli_args = CliArgs::from(args);
                assert_eq!(cli_args.rsync_cmd(), "rsync");
                assert_eq!(cli_args.rsync_remote(), "rsync.tools.ietf.org::tools.html");
                assert_eq!(cli_args.verbosity(), 1)
            }
            _ => panic!("Cli parsing failed"),
        }
    }

    #[test]
    fn test_exec_index() -> Result<()> {
        let defaults = Defaults::get()?;
        let dir = resource_path("");
        let argv = Some(vec!["rfz", "index", "-d", dir.to_str().unwrap()]);
        let cli = Cli::init_from(&defaults, argv).unwrap();
        cli.run()
    }

    #[test]
    fn test_exec_completions() -> Result<()> {
        let defaults = Defaults::get()?;
        let argv = Some(vec!["rfz", "completions", "bash"]);
        let cli = Cli::init_from(&defaults, argv).unwrap();
        cli.run()
    }

    #[test]
    fn test_exec_unknown_shell() -> Result<()> {
        let defaults = Defaults::get()?;
        let argv = Some(vec!["rfz", "completions", "crash"]);
        match Cli::init_from(&defaults, argv) {
            Err(e) => assert_eq!(e.kind, clap::ErrorKind::InvalidValue),
            Ok(_) => panic!("Expected InvalidValue Error"),
        };
        Ok(())
    }
}