dotfiles_actions/ohmyzsh_install/
action.rs1#![cfg(unix)]
25use dotfiles_core::action::Action;
26use dotfiles_core::error::DotfilesError;
27use dotfiles_core::exec_wrapper::execute_commands;
28use std::process::Command;
29use subprocess::Exec;
30
31pub struct OhMyZshInstallAction {
33  skip_chsh: bool,
34}
35
36impl OhMyZshInstallAction {
37  pub fn new(skip_chsh: bool) -> Self {
39    OhMyZshInstallAction { skip_chsh }
40  }
41  pub fn check_zsh_is_installed(&self) -> bool {
43    Command::new("which").arg("zsh").status().unwrap().success()
44  }
45  pub fn check_oh_my_zsh_is_installed(&self) -> bool {
47    matches!(std::env::var("ZSH"), Ok(_))
48  }
49}
50
51impl Action<'_> for OhMyZshInstallAction {
52  fn execute(&self) -> Result<(), DotfilesError> {
53    if self.check_oh_my_zsh_is_installed() {
54      log::info!("oh-my-zsh is already installed, no need to reinstall");
55      return Ok(());
56    }
57    if !self.check_zsh_is_installed() {
58      #[cfg(target_os = "linux")]
59      let cmd = Exec::shell("sudo apt install zsh");
60      #[cfg(target_os = "macos")]
61      let cmd = Exec::shell("brew install zsh");
62      execute_commands(
63        vec![cmd],
64        "Couldn't run zsh installation",
65        "Unexpected error while running zsh installation",
66      )?;
67    }
68
69    if !self.skip_chsh {
70      execute_commands(
71        vec![Exec::shell("chsh -s $(which zsh)")],
72        "Couldn't run chsh to set the shell to zsh",
73        "Unexpected error while running chsh to set the shell to zsh",
74      )?;
75    }
76    execute_commands(
77      vec![Exec::shell(
78        "sh -c \"$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)\"",
79      )],
80      "Couldn't install ohmyzsh",
81      "Unexpected error while installing ohmyzsh",
82    )
83  }
84}