dotfiles_actions/ohmyzsh_install/
action.rs

1// Copyright (c) 2021-2022 Miguel Barreto and others
2//
3// Permission is hereby granted, free of charge, to any person obtaining
4// a copy of this software and associated documentation files (the
5// "Software"), to deal in the Software without restriction, including
6// without limitation the rights to use, copy, modify, merge, publish,
7// distribute, sublicense, and/or sell copies of the Software, and to
8// permit persons to whom the Software is furnished to do so, subject to
9// the following conditions:
10//
11// The above copyright notice and this permission notice shall be
12// included in all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22//! This module contains the [OhMyZshInstallAction] that sets up ohmyzsh
23
24#![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
31/// [OhMyZshInstallAction] sets up ohmyzsh.
32pub struct OhMyZshInstallAction {
33  skip_chsh: bool,
34}
35
36impl OhMyZshInstallAction {
37  /// Constructs a new [OhMyZshInstallAction]
38  pub fn new(skip_chsh: bool) -> Self {
39    OhMyZshInstallAction { skip_chsh }
40  }
41  /// Returns true if it can find a brew command
42  pub fn check_zsh_is_installed(&self) -> bool {
43    Command::new("which").arg("zsh").status().unwrap().success()
44  }
45  /// Returns true if the $ZSH environment var is set
46  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}