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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
use {
std::{
fs,
io,
os,
path::{Path, PathBuf},
},
crate::{
cli,
conf,
errors::ProgramError,
mad_skin,
},
termimad::{mad_print_inline, MadSkin,},
};
mod util;
mod bash;
mod fish;
const MD_INSTALL_REQUEST: &str = r#"
**Broot** should be launched using a shell function (see *https://github.com/Canop/broot* for explanations).
The function is either missing, old or badly installed.
Can I install it now? [**Y** n]
"#;
const MD_INSTALL_DONE : &str = r#"
The **br** function has been installed.
You may have to restart your shell or source your shell init files.
Afterwards, you should start broot with `br` in order to use its full power.
"#;
const REFUSED_FILE_CONTENT: &str = r#"
This file tells broot you refused the installation of the companion shell function.
If you want to install it run
broot -- install
"#;
const INSTALLED_FILE_CONTENT: &str = r#"
This file tells broot the installation of the br function was done.
If there's a problem and you want to install it again run
broot -- install
"#;
pub struct ShellInstall {
force_install: bool,
skin: MadSkin,
pub should_quit: bool,
authorization: Option<bool>,
done: bool,
}
fn get_refused_path() -> PathBuf {
conf::dir().join("launcher").join("refused")
}
fn get_installed_path() -> PathBuf {
conf::dir().join("launcher").join("installed-v1")
}
impl ShellInstall {
pub fn new(launch_args: &cli::AppLaunchArgs) -> Self {
let force_install = launch_args.install;
Self {
force_install,
skin: mad_skin::make_cli_mad_skin(),
should_quit: false,
authorization: if force_install { Some(true) } else { None },
done: false,
}
}
pub fn check(&mut self) -> Result<(), ProgramError> {
let installed_path = get_installed_path();
if self.force_install {
self.skin.print_text("You requested a clean (re)install.");
self.remove(&get_refused_path())?;
self.remove(&installed_path)?;
} else {
if installed_path.exists() {
debug!("Shell script already installed. Doing nothing.");
return Ok(());
}
debug!("No 'installed' : we ask if we can install");
if !self.can_do()? {
debug!("User refuses the installation. Doing nothing.");
return Ok(());
}
}
debug!("Starting install");
bash::install(self)?;
fish::install(self)?;
self.should_quit = true;
if self.done {
fs::create_dir_all(installed_path.parent().unwrap())?;
fs::write(&installed_path, INSTALLED_FILE_CONTENT)?;
self.skin.print_text(MD_INSTALL_DONE);
}
Ok(())
}
pub fn remove(&self, path: &Path) -> io::Result<()> {
if fs::read_link(path).is_ok() || path.exists() {
let path_str = path.to_string_lossy();
mad_print_inline!(self.skin, "Removing `$0`.\n", &path_str);
fs::remove_file(path)?;
}
Ok(())
}
fn can_do(&mut self) -> Result<bool, ProgramError> {
if let Some(authorization) = self.authorization {
return Ok(authorization);
}
let refused_path = get_refused_path();
if refused_path.exists() {
debug!("User already refused the installation");
return Ok(false);
}
self.skin.print_text(MD_INSTALL_REQUEST);
let proceed = cli::ask_authorization()?;
debug!("proceed: {:?}", proceed);
self.authorization = Some(proceed);
if !proceed {
fs::create_dir_all(refused_path.parent().unwrap())?;
fs::write(
&refused_path,
REFUSED_FILE_CONTENT,
)?;
self.skin.print_text("**Installation cancelled**. If you change your mind, run `broot --install`.");
}
Ok(proceed)
}
fn write_script(
&self,
script_path: &Path,
content: &str,
) -> Result<(), ProgramError> {
self.remove(&script_path)?;
info!("Writing `br` shell function in `{:?}`", &script_path);
let script_path_str = script_path.to_string_lossy();
mad_print_inline!(&self.skin, "Writing *br* shell function in `$0`.\n", &script_path_str);
fs::create_dir_all(script_path.parent().unwrap())?;
fs::write(&script_path, content)?;
Ok(())
}
fn create_link(
&self,
link_path: &Path,
script_path: &Path,
) -> Result<(), ProgramError> {
info!("Creating link from {:?} to {:?}", &link_path, &script_path);
self.remove(&link_path)?;
let link_path_str = link_path.to_string_lossy();
let script_path_str = script_path.to_string_lossy();
mad_print_inline!(
&self.skin,
"Creating link from `$0` to `$1`.\n",
&link_path_str,
&script_path_str,
);
fs::create_dir_all(link_path.parent().unwrap())?;
#[cfg(unix)]
os::unix::fs::symlink(&script_path, &link_path)?;
#[cfg(windows)]
os::windows::fs::symlink_file(&script_path, &link_path)?;
Ok(())
}
}