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
use crate::lib::{
    config::loader::load_cfg,
    fsutil::{copy::copy, paths::get_root},
    structs::config::KelpDotConfig,
    util::{exec::get_root_exec_program, os::get_host_os, scripts::run_script},
};
use kelpdot_macros::*;
use std::{path::Path, process::Command};
pub fn install() -> anyhow::Result<()> {
    let root = get_root()?;
    cyan_print!("[INFO] Installing dotfiles {}", root);
    debug_print!("Building OS list...");
    let os = get_host_os()?;
    cyan_print!("Found OS {}", os.prettyname);
    let config: KelpDotConfig = load_cfg(root.clone())?;
    if let Some(scripts) = config.prerun {
        for script in scripts {
            cyan_print!("[PRERUN] Running script {}", script.path);
            run_script(root.clone(), script)?;
        }
    }
    if let Some(files) = config.homefiles {
        let home_files_path = format!("{}/home", root);
        for file in files {
            if let Some(distro) = &file.onlyon {
                if &os.name != distro && !os.submatches.iter().any(|mat| mat == distro) {
                    debug_print!("Not installing file {} because host != onlyon", file);
                    break;
                }
            }
            let home = std::env::var("HOME")?; // Get $HOME path or crash
            debug_print!("Home: {}", home);
            if Path::new(&format!("{}/{}", home_files_path, file.path)).exists() {
                cyan_print!("[INFO] Installing {}", file);
                copy(
                    format!("{}/{}", home_files_path, file.path),
                    format!("{}/{}", home, file.path),
                )?;
            }
        }
    }
    // The work of rootfiles copy is **really** different:
    // Firstly we check if file exist
    // We create a Shell script with required files copies
    // We execute it as root
    // DONE!
    if let Some(files) = config.rootfiles {
        let mut bash_code = String::from("#!/usr/bin/env sh\n#This script has been auto-generated and will be runned by KelpDot\n#It isn't intended to be modified manually\n");
        for file in files {
            if let Some(distro) = &file.onlyon {
                if &os.name != distro && !os.submatches.iter().any(|mat| mat == distro) {
                    debug_print!("Not installing file {} because host != onlyon", file);
                    break;
                }
            }
            let fpath = format!("{}{}", root, file.path);
            // ShBang isn't really needed, I know
            let path = Path::new(&fpath);
            let dest_parent = Path::new(&file.path).parent().unwrap().to_str().unwrap();
            if path.exists() {
                bash_code = format!(
                    "{}if [[ ! -d {} ]]\nthen\nmkdir -p {}\nfi\ncp -r {} {}\n",
                    bash_code,
                    dest_parent,
                    dest_parent,
                    path.to_str().unwrap(),
                    dest_parent
                );
            }
        }
        std::fs::write("/tmp/kelpdot_install.sh", bash_code)?;
        let rexec = get_root_exec_program()?;
        Command::new(&rexec) // Use SH because some systems symlinks it to bash / zsh / ash
            .arg("sh")
            .arg("/tmp/kelpdot_install.sh")
            .status()?;
    }
    if let Some(scripts) = config.postrun {
        for script in scripts {
            cyan_print!("[POSTRUN] Running script {}", script.path);
            run_script(root.clone(), script)?;
        }
    }
    Ok(())
}