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
//! Clap's CLI interface interaction

use crate::files;
use clap::{crate_name, crate_version, App, Arg};

/// Build app and get matches from it
/// 
/// Possible matches:
/// - `pash`;
/// - `pash help`;
/// - `pash --help`;
/// - `pash --init`;
/// - `pash create`;
/// - `pash help create`;
/// - `pash create NAME`;
/// - _etc_.
fn build_matches() -> clap::ArgMatches<'static> {
    let app = App::new(crate_name!())
        .version(crate_version!())
        .author("Shift <me@shift-d.tk>")
        .about("")
        .arg(
            Arg::with_name("init")
                .help("Init important files")
                .long("init"),
        )
        .subcommand(
            App::new("create")
                .about("Create new password")
                .arg(
                    Arg::with_name("NAME")
                        .help("Password's name")
                        .required(true),
                )
                .arg(
                    Arg::with_name("category")
                        .help("Password's category")
                        .required(false)
                        .short("c")
                        .long("category")
                        .takes_value(true),
                ),
        );
    app.get_matches()
}

/// Interaction with given Clap's CLI matches.
/// Execute function if we have passed ... argument.
pub fn matches_interaction() {
    let matches = build_matches();

    if let Some(create_subcommand) = matches.subcommand_matches("create") {
        if let Some(name_value) = create_subcommand.value_of("NAME") {
            let content = files::edit::FileContent::default();
            let password_name = name_value.to_string();
            let category_name = create_subcommand.value_of("category");
            content.save_passwords(category_name, password_name);
        }
    } else if matches.is_present("init") {
        files::FilePaths::init();
        files::edit::FileContent::default().init_config();
    }
}