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
use crate::lib::{
config::loader::load_cfg,
fsutil::{
copy::copy,
paths::{get_ins_root, get_root},
},
structs::{config::KelpDotConfig, pm::PackageManager},
util::{
exec::get_root_exec_program,
os::{get_host_os, is_os},
pm::get_distro_pm,
scripts::run_script,
},
};
use anyhow::Context;
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()?;
let insroot = get_ins_root()?;
debug_print!("Install root: {}", insroot);
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").with_context(|| red!("Unable to get env var $HOME!"))?;
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!("{}/{}/{}", insroot, home, file.path),
)?;
}
}
}
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);
let path = Path::new(&fpath);
let inspath = format!("{}/{}", insroot, &file.path);
let dest_parent = Path::new(&inspath).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)
.arg("sh")
.arg("/tmp/kelpdot_install.sh")
.status()
.with_context(|| red!("Unable to call rootfiles install script!"))?;
}
if let Some(packages) = config.packages {
let mut pm = get_distro_pm()?;
if let Some(gentoo) = packages.gentoo {
if is_os("gentoo")? {
if let Some(pkgs) = gentoo.packages {
pm.install_packages(pkgs)?;
}
if let Some(file) = gentoo.with_file {
let mut packages: Vec<String> = Vec::new();
let filepath = format!("{}/{}", root, file);
let fpath = Path::new(&filepath);
if fpath.exists() {
let contents = std::fs::read_to_string(filepath)?;
contents.lines().into_iter().for_each(|x| {
packages.push(String::from(x));
});
}
pm.install_packages(packages)?;
}
}
}
}
if let Some(scripts) = config.postrun {
for script in scripts {
cyan_print!("[POSTRUN] Running script {}", script.path);
run_script(root.clone(), script)?;
}
}
Ok(())
}