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

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

/// 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: &'static 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");
    let _ = match File::open(&bashrc) {
        Ok(mut f) => {
            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 mut contents_saved = contents.clone();

            let should_write: bool = contents.lines().fold(true, |acc, next| acc && (next != "\n#manpath updated by cli-setup") );

            if !should_write {
                contents_saved.push_str("\n#manpath updated by cli-setup\nexport MANPATH=~/.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.clone();
    man_dir.push(".local");
    man_dir.push("share");
    man_dir.push("man");
    man_dir.push("man1");
    let mut man_path = man_dir.clone();
    
    let _ = create_dir_all(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()),
    }
}