1use crate::{exec, expand_path, is_msys, to_msys_path};
2use which_shell::Shell;
3
4pub fn add_path_to_shell(shell: Shell, path: &str) -> bool {
5 let path = if cfg!(windows) || is_msys() {
6 &to_msys_path(path)
7 } else {
8 path
9 };
10 let (cmd, args) = match shell {
11 which_shell::Shell::Fish => (
12 "fish",
13 [
14 "-c",
15 &format!(
16 r#"echo '
17set -gx PATH "{path}" $PATH
18' >> ~/.config/fish/config.fish"#
19 ),
20 ],
21 ),
22 which_shell::Shell::Zsh => (
23 "sh",
24 [
25 "-c",
26 &format!(
27 r#"echo '
28export PATH="{path}:$PATH"
29' >> ~/.zshrc"#
30 ),
31 ],
32 ),
33 which_shell::Shell::Bash | which_shell::Shell::Sh => (
34 "sh",
35 [
36 "-c",
37 &format!(
38 r#"echo '
39export PATH="{path}:$PATH"
40' >> ~/.bashrc"#
41 ),
42 ],
43 ),
44 _ => return false,
45 };
46
47 if is_admin::is_admin() {
48 exec(
49 "sh",
50 [
51 "-c",
52 &format!(
53 r#"echo '
54export PATH="{path}:$PATH"
55' >> /etc/profile"#
56 ),
57 ],
58 );
59 }
60
61 exec(cmd, args)
62}
63
64pub fn add_path(path: &str) -> Option<Shell> {
65 let path = &expand_path(path);
66 if let Some(shell) = which_shell::which_shell()
68 && add_path_to_shell(shell.shell, path)
69 {
70 return Some(shell.shell);
71 }
72 if add_path_to_shell(Shell::Bash, path) {
73 return Some(Shell::Bash);
74 }
75 None
76}
77
78#[cfg(test)]
79mod test {
80 use super::add_path;
81
82 #[test]
83 fn test_add_path() {
84 let s = "/xxx";
85 let s = add_path(s);
86 assert!(s.is_some());
87 }
88}