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
extern crate colored;
extern crate dirs;

use colored::*;
use dirs::home_dir;
use std::fs::create_dir_all;
use std::fs::File;
use std::io::prelude::*;
use std::path::PathBuf;

/// Set up rules for [fuck](https://pypi.python.org/pypi/thefuck/) that work with `clap-rs`.
///
/// ```
/// use cli_setup::*;
///
/// setup_thefuck();
/// ```
pub fn setup_thefuck() {
    pub const THEFUCK_STRING: &str = include_str!("../py/clap-rs.py");
    let home_dir = match home_dir() {
        Some(p) => p,
        None => PathBuf::from("."),
    };

    let mut config_path = home_dir;
    config_path.push(".config");
    config_path.push("thefuck");
    config_path.push("rules");
    if config_path.exists() && config_path.is_dir() {
        config_path.push("clap-rs");
        config_path.set_extension("py");
        if let Ok(mut f) = File::create(&config_path) {
            let mut thefuck_string = THEFUCK_STRING.to_string();
            match f.write(thefuck_string.as_bytes()) {
                Ok(_) => (),
                _ => eprintln!("{}: file write failed", "Warning".yellow()),
            };
        } else {
            eprintln!(
                "{}: failed to open file at {}",
                "Warning".yellow(),
                &config_path.display()
            );
        };
    }
}

/// Set up given the manpage contents and an executable name. This function is
/// intended to be called inside the project `build.rs`.
///
/// ```
/// use cli_setup::*;
///
/// pub const MANPAGE: &str = include_str!("man/executable.1");
/// setup_manpages(MANPAGE, "executable");
/// ```
pub fn setup_manpages(man: &str, exe_name: &str) {
    let home_dir = match home_dir() {
        Some(p) => p,
        None => PathBuf::from("."),
    };

    let mut bashrc = home_dir.clone();
    bashrc.push(".bashrc");
    if let Ok(mut f) = File::open(&bashrc) {
        let mut contents = String::new();
        match f.read_to_string(&mut contents) {
            Ok(_) => (),
            Err(_) => eprintln!(
                "{}: failed to open file, not installing manual pages",
                "Warning".yellow()
            ),
        }
        let should_write: bool = contents
            .lines()
            .all(|line| line != "\n#manpath updated by cli-setup");

        let mut contents_saved = contents;

        if !should_write {
            contents_saved.push_str(
                "\n#manpath updated by cli-setup\nexport MANPATH=\"$HOME\"/.local/share:$MANPATH",
            );
            match File::create(&bashrc) {
                Ok(mut file) => match file.write(contents_saved.as_bytes()) {
                    Ok(_) => (),
                    Err(_) => eprintln!(
                        "{}: failed to open file, not installing manual pages",
                        "Warning".yellow()
                    ),
                },
                _ => eprintln!(
                    "{}: failed to open file at {}, not installing manual pages",
                    "Warning".yellow(),
                    &bashrc.display()
                ),
            };
        };
    };

    let mut man_dir = home_dir;
    man_dir.push(".local");
    man_dir.push("share");
    man_dir.push("man");
    man_dir.push("man1");

    let _ = create_dir_all(&man_dir);
    let mut man_path = man_dir;

    man_path.push(exe_name);
    man_path.set_extension("1");

    let pre_f = File::create(man_path);
    match pre_f {
        Ok(mut f) => {
            let res = f.write(man.as_bytes());
            match res {
                Ok(_) => (),
                Err(_) => eprintln!(
                    "{}: failed to open file, not installing manual pages",
                    "Warning".yellow()
                ),
            }
        }
        Err(_) => eprintln!(
            "{}: failed to open file, not installing manual pages",
            "Warning".yellow()
        ),
    }
}