irongit

fix: installer when sudo is required

huncholanehuncholaneauthored
parent 8c91c66commit 1dace095768bf64d49a9a959dcb79925d44f5361Browse files

2 files changed, +114 -78

+106-78install.sh
@@ -2,21 +2,10 @@
22
33 BLA_clock=(๐Ÿ•› ๐Ÿ• ๐Ÿ•‘ ๐Ÿ•’ ๐Ÿ•“ ๐Ÿ•” ๐Ÿ•• ๐Ÿ•– ๐Ÿ•— ๐Ÿ•˜ ๐Ÿ•™ ๐Ÿ•š)
44
5-animate() {
6- eval "$1" &>/tmp/last_output &
7- local pid=$!
8- local spin='-\|/'
9- local i=0
10- while kill -0 $pid 2>/dev/null; do
11- for frame in "${BLA_clock[@]}"; do
12- printf "\r%s" "${frame}"
13- sleep 0.2
14- done
15- done
16- printf "\r"
17- wait $pid
18- return $?
19-}
5+# Use $prefix for commands that might need sudo
6+prefix="$([[ $(whoami) == "root" ]] && echo "" || echo "sudo")"
7+LAST_OUTPUT=$(mktemp)
8+[ -d "$HOME" ] && echor "$HOME is not a directory. You are cancelled." && exit 1
209
2110 # Colored echos
2211 echop() {
@@ -34,12 +23,38 @@ echot() {
3423 echob() {
3524 echo -e "\e[34$1\e[0m"
3625 }
26+echoy() {
27+ echo -e "\e[33m$1\e[0m"
28+}
29+
30+# Ensure sudo and run command in background with animation
31+# $1: command used to check if the 2nd command should be ran ("" is always true)
32+# $2: command to run in the background
33+# $3: text to append to animation
34+# $4: success message
35+# $5: failure message (after LAST_OUTPUT)
36+animate() {
37+ # Return when check is success
38+ [[ -z "$1" ]] || eval "$1" && echog "โœ… $4" && return 1
3739
38-# Select sudo or not
39-[ "$(whoami)" == "root" ] && prefix="" || prefix="sudo"
40+ # Ensure sudo
41+ [[ "$(whoami)" == "root" ]] || sudo -v || exit 1
4042
41-login_sudo() {
42- [ "$prefix" != "sudo" ] || sudo -v
43+ # Run with output to tmp
44+ eval "$2" &>"$LAST_OUTPUT" &
45+
46+ # Wait and animate the command
47+ local pid=$!
48+ while kill -0 $pid 2>/dev/null; do
49+ for frame in "${BLA_clock[@]}"; do
50+ printf "\r%s %s " "${frame}" "$3"
51+ sleep 0.2
52+ done
53+ done
54+
55+ # Clear and output error if needed
56+ printf "\033[2K\r"
57+ { wait $pid && echog "โœ… $4"; } || { cat "$LAST_OUTPUT" && echor "\n๐Ÿ”ด $5" && exit 1; }
4358 }
4459
4560 echo "Ensuring base tools are installed."
@@ -47,19 +62,22 @@ echo "Ensuring base tools are installed."
4762 # Install basic tools on debian
4863 if [ -f /etc/debian_version ]; then
4964 # Set the installer prefix
50- installer="$prefix apt-get"
51- install_cmd="$installer install -y"
65+ installer="$prefix apt-get -y"
5266
5367 # Update system
5468 echop "Detected Debian. Updating and upgrading now!"
55- animate "$installer update -y && $installer upgrade -y"
56- echog "โœ… System up to date!"
69+ animate "" \
70+ "$installer update && $installer upgrade" \
71+ "Updating system" \
72+ "System up to date!" \
73+ "Failed to update system"
5774
5875 # Install build essential
59- dpkg -s build-essential &>/dev/null ||
60- { echo "Installing build-essential" && animate "$install_cmd build-essential" && printf "\e[1A\e[2K"; } ||
61- { cat /tmp/last_output && echor "Failed to install build-essential" && exit 1; }
62- echog "โœ… build-essential installed"
76+ animate "dpkg -s build-essential &>/dev/null" \
77+ "$installer install build-essential" \
78+ "Installing build-essential" \
79+ "build-essential installed" \
80+ "Failed to install build-essential"
6381
6482 # Install basic tools for arch
6583 elif [ -f /etc/arch-release ]; then
@@ -69,16 +87,19 @@ elif [ -f /etc/arch-release ]; then
6987
7088 # Update system
7189 echob "Arch Detected. You are elite. Updating and upgrading now!"
90+ login_sudo
7291 animate "$installer -Syu"
7392 echog "โœ… System up to date!"
7493
7594 # Install base-devel
95+ login_sudo
7696 pacman -Q base-devel &>/dev/null ||
7797 { echo "Installing base-devel" && animate "$installer base-devel" && printf "\e[1A\e[2K"; } ||
7898 { cat /tmp/last_output && echor "Failed to install base-devel"; }
7999 echog "โœ… base-devel installed"
80100
81101 # Install paru
102+ login_sudo
82103 command -v paru &>/dev/null ||
83104 { echo "Installing paru" && animate "git clone https://aur.archlinux.org/paru.git /tmp/paru &&
84105 cd /tmp/paru && makepkg -si" && printf "\e[1A\e[2K"; } ||
@@ -87,68 +108,76 @@ elif [ -f /etc/arch-release ]; then
87108 fi
88109
89110 # Install zsh
90-command -v zsh &>/dev/null ||
91- { echo "Installing zsh" && animate "$install_cmd zsh" && printf "\e[1A\e[2K"; } ||
92- { cat /tmp/last_output && echor "Failed to install zsh" && exit 1; }
93-echog "โœ… zsh installed"
111+animate "command -v zsh &>/dev/null" \
112+ "$installer install zsh" \
113+ "Installing zsh" \
114+ "zsh installed" \
115+ "Failed to install zsh"
94116
95117 # Install curl (crazy but this happens sometimes)
96-command -v curl &>/dev/null ||
97- { echo "Installing curl" && animate "$install_cmd curl" && printf "\e[1A\e[2K"; } ||
98- { cat /tmp/last_output && echor "Failed to install curl"; }
99-echog "โœ… curl installed"
118+animate "command -v curl &>/dev/null" \
119+ "$installer install curl" \
120+ "Installing curl" \
121+ "curl installed" \
122+ "Failed to install curl"
100123
101124 # Install unzip
102-command -v unzip &>/dev/null ||
103- { echo "Installing unzip" && animate "$install_cmd unzip" && printf "\e[1A\e[2K"; } ||
104- { cat /tmp/last_output && echor "Failed to install unzip"; }
105-echog "โœ… unzip installed"
125+animate "command -v unzip &>/dev/null" \
126+ "$installer install unzip" \
127+ "Installing unzip" \
128+ "unzip installed" \
129+ "Failed to install unzip"
106130
107131 # Install git
108-command -v git &>/dev/null ||
109- { echo "Installing git" && animate "$install_cmd git" && printf "\e[1A\e[2K"; } ||
110- { cat /tmp/last_output && echor "Failed to install git"; }
111-echog "โœ… git installed"
132+animate "command -v git &>/dev/null" \
133+ "$installer install git" \
134+ "Installing git" \
135+ "git installed" \
136+ "Failed to install git"
112137
113138 # Install cargo
114-[ -f ~/.cargo/env ] ||
115- { echo "Installing cargo" && animate "curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y" && printf "\e[1A\e[2K"; } ||
116- { cat /tmp/last_output && echor "Failed to install cargo" && exit 1; }
117-echog "โœ… cargo installed"
139+animate "[ -f ~/.cargo/env ]" \
140+ "curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | $prefix sh -s -- -y" \
141+ "Installing cargo" \
142+ "cargo installed" \
143+ "Failed to install cargo"
118144 . ~/.cargo/env
119145
120146 # Install lsd with cargo
121-command -v lsd &>/dev/null ||
122- { echo "Installing lsd" && animate "cargo install lsd" && printf "\e[1A\e[2K"; } ||
123- { cat /tmp/last_output && echor "Failed to install lsd" && exit 1; }
124-echog "โœ… lsd installed"
147+animate "command -v lsd &>/dev/null" \
148+ "cargo install lsd" \
149+ "Installing lsd" \
150+ "lsd installed" \
151+ "Failed to install cargo"
125152
126153 # Install golang
127-export PATH=$PATH:/usr/local/go/bin:/usr/local/go/bin
154+export PATH="$PATH:/usr/local/go/bin:/usr/local/go/bin:$HOME/go/bin"
128155 latest_go_version="$(curl -fsSL "https://go.dev/VERSION?m=text" | head -1)"
129156 filename="$latest_go_version.$([[ "$(uname -m)" == "Linux" ]] && { printf "linux"; } || printf "darwin").$(uname -m).tar.gz"
130-[[ "$(go version 2>/dev/null | awk '{print $3}')" == "$latest_go_version" ]] ||
131- { login_sudo && echo "Installing golang" && animate "curl -fsSL https://go.dev/dl/go1.26.1.linux-amd64.tar.gz | $prefix tar xvzf - -C /usr/local" && printf "\e[1A\e[2K"; } ||
132- { cat /tmp/last_output && echor "Failed to install go" && exit 1; }
133-echog "โœ… golang installed"
157+animate "[[ \"$(go version 2>/dev/null | awk '{print $3}')\" == \"$latest_go_version\" ]]" \
158+ "curl -fsSL https://go.dev/dl/go1.26.1.linux-amd64.tar.gz | $prefix tar xvzf - -C /usr/local" \
159+ "Installing go" \
160+ "go installed" \
161+ "Failed to install go"
134162
135163 # Install lazygit with go
136-command -v lazygit &>/dev/null ||
137- { echo "Installing lazygit" && animate "go install github.com/jesseduffield/lazygit@latest" && printf "\e[1A\e[2K"; } ||
138- { cat /tmp/last_output && echor "Failed to install lazygit" && exit 1; }
139-echog "โœ… lazygit installed"
164+animate "command -v lazygit &>/dev/null" \
165+ "go install github.com/jesseduffield/lazygit@latest" \
166+ "Installing lazygit" \
167+ "lazygit installed" \
168+ "Failed to install lazygit"
140169
141170 # Install yazi
142171 filename="yazi-$(uname -m)-$(
143172 { [[ "$(uname -s)" == "Linux" ]] && printf "unknown-linux-%s" "$(ldd --version 2>&1 | grep -qi musl && echo musl || echo gnu)"; } || printf "apple-darwin"
144173 ).zip"
145-[ -d /usr/local/yazi ] ||
146- { login_sudo && echo "installing yazi" && animate "rm -rf /tmp/yazi* && $prefix rm -rf /usr/local/yazi &&
174+animate "[ -d /usr/local/yazi ]" \
175+ "rm -rf /tmp/yazi* && $prefix rm -rf /usr/local/yazi &&
147176 curl -fsSL https://github.com/sxyazi/yazi/releases/download/v26.1.22/yazi-x86_64-unknown-linux-gnu.zip -o /tmp/yazi.zip &&
148- unzip /tmp/yazi.zip -d /tmp && $prefix mv /tmp/yazi-x86_64-unknown-linux-gnu/ /usr/local/yazi/ && $prefix mv /usr/local/yazi/ya* /usr/local/bin" &&
149- printf "\e[1A\e[2K"; } ||
150- { cat /tmp/last_output && echor "Failed to install yazi" && exit 1; }
151-echog "โœ… yazi installed"
177+ unzip /tmp/yazi.zip -d /tmp && $prefix mv /tmp/yazi-x86_64-unknown-linux-gnu/ /usr/local/yazi/ && $prefix mv /usr/local/yazi/ya* /usr/local/bin" \
178+ "Installing yazi" \
179+ "yazi installed" \
180+ "Failed to install yazi"
152181
153182 # Create backups
154183 [ -d ~/.config ] || mkdir -p ~/.config
@@ -158,19 +187,18 @@ echog "โœ… yazi installed"
158187
159188 # Clone config repo
160189 echo 'export ZDOTDIR=$HOME/.config/zsh' >~/.zshenv
161-{ echo "Cloning dotzsh" && animate "git clone https://github.com/huncholane/dotzsh ~/.config/zsh" && printf "\e[1A\e[2K"; } ||
162- { cat /tmp/last_output && echor "Failed to clone dotzsh to ~/.config/zsh" && exit 1; }
163190 export ZDOTDIR=$HOME/.config/zsh
164-echog "โœ… dotzsh installed"
191+animate "" \
192+ "git clone https://github.com/huncholane/dotzsh ~/.config/zsh" \
193+ "Cloning dotzsh" \
194+ "dotzsh cloned. Just a few more things." \
195+ "Failed to clone dotzsh"
165196
166197 # Install antidote
167-[ -d ~/.config/zsh/.antidote ] ||
168- { echo "Installing antidote" && animate "git clone --depth=1 https://github.com/mattmc3/antidote.git ${ZDOTDIR:-~}/.antidote" && printf "\e[1A\e[2K"; } ||
169- { cat /tmp/last_output && echor "Failed to install antidote" && exit 1; }
170-
171-echot -e "\nRun 'exec zsh' to see the magic"
172-
173-# [ -f /etc/arch-release ] && pacman -Q base-devel &>/dev/null || sudo pacman -S base-devel || echo -e "\e[31mFailed to install base-devel" && exit 1
174-# [ -f /etc/debian_version ] && (apt update dpkg -s build-essential &>/dev/null || echo "missing build-essential")
198+animate "[ -d ~/.config/zsh/.antidote ]" \
199+ "git clone --depth=1 https://github.com/mattmc3/antidote.git ${ZDOTDIR:-~}/.antidote" \
200+ "Installing antidote" \
201+ "antidote installed" \
202+ "Failed to install antidote"
175203
176-# command -v zsh || sudo apt install zsh || sudo dnf install zsh || sudo pacman -S zsh
204+echot "\nRun 'exec zsh' to see the magic"
+8-0install_test.sh
@@ -0,0 +1,8 @@
1+#!/bin/bash
2+
3+case "$1" in
4+"ubuntu") docker run --rm -v "$(pwd):/installer" -w /installer -it ubuntu bash -c "
5+ apt update -y && apt install sudo -y && useradd test -p test && usermod -aG sudo test &&
6+ su - test" ;;
7+"arch") docker run --rm -v "$(pwd):/installer" -w /installer -it arch 'pacman -Syu --noconfirm && pacman -S && pacman -S --noconfirm && useradd test && bash' ;;
8+esac