Skip to content

Commit 8af237d

Browse files
authored
Update main.cr
1 parent a2430d9 commit 8af237d

1 file changed

Lines changed: 31 additions & 2 deletions

File tree

HackerOS-Updater/main.cr

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ require "option_parser"
22
require "process"
33
require "file_utils"
44
require "colorize"
5+
56
# ANSI color codes
67
RED = "\e[31m"
78
GREEN = "\e[32m"
@@ -10,25 +11,30 @@ YELLOW = "\e[33m"
1011
CYAN = "\e[36m"
1112
MAGENTA = "\e[35m"
1213
RESET = "\e[0m"
14+
1315
# Paths
1416
HACKEROS_UPDATE_SCRIPT = "/usr/share/HackerOS/Scripts/Bin/update-hackeros.sh"
1517
WALLPAPERS_UPDATE_SCRIPT = "/usr/share/HackerOS/Scripts/Bin/update-wallpapers.sh"
1618
BIN_PATH = Process.executable_path.not_nil!
1719
AUTO_SCRIPT_PATH = "#{ENV["HOME"]}/.hackeros/auto-update.sh" # Script to wait for internet
20+
1821
def display_header(title : String)
1922
puts "<--------[ #{title} ]-------->".colorize(:yellow)
2023
end
24+
2125
def run_command(cmd : String) : {Bool, String}
2226
status = Process.run(cmd, shell: true, input: Process::Redirect::Inherit, output: Process::Redirect::Inherit, error: Process::Redirect::Inherit)
2327
{status.success?, ""}
2428
end
29+
2530
def get_status(success : Bool) : String
2631
if success
2732
"#{BLUE}COMPLETE#{RESET}"
2833
else
2934
"#{RED}FAILED#{RESET}"
3035
end
3136
end
37+
3238
def perform_updates : {String, String, String, String, String, String, String, String, String}
3339
# APT Update
3440
display_header("System Update")
@@ -38,44 +44,54 @@ def perform_updates : {String, String, String, String, String, String, String, S
3844
apt_success &&= success
3945
end
4046
apt_status = get_status(apt_success)
47+
4148
# Flatpak Update
4249
display_header("Flatpak Update")
4350
flatpak_success, _ = run_command("flatpak update -y")
4451
flatpak_status = get_status(flatpak_success)
52+
4553
# Snap Update
4654
display_header("Snap Update")
4755
snap_success, _ = run_command("sudo snap refresh")
4856
snap_status = get_status(snap_success)
49-
# Brew Update
57+
58+
# Brew Update (Expanded with cleanup)
5059
display_header("Brew Update")
5160
brew_success = true
52-
["brew update", "brew upgrade"].each do |cmd|
61+
["brew update", "brew upgrade", "brew cleanup"].each do |cmd|
5362
success, _ = run_command(cmd)
5463
brew_success &&= success
5564
end
5665
brew_status = get_status(brew_success)
66+
5767
# Firmware Update
5868
display_header("Firmware Update")
5969
fw_success, _ = run_command("sudo fwupdmgr update")
6070
fw_status = get_status(fw_success)
71+
6172
# Oh My Zsh Update
6273
display_header("Oh My Zsh Update")
6374
omz_success, _ = run_command("omz update")
6475
omz_status = get_status(omz_success)
76+
6577
# Distrobox Update
6678
display_header("Distrobox Update")
6779
distrobox_success, _ = run_command("distrobox-upgrade --all")
6880
distrobox_status = get_status(distrobox_success)
81+
6982
# HackerOS Update
7083
display_header("HackerOS Update")
7184
hacker_success, _ = run_command(HACKEROS_UPDATE_SCRIPT)
7285
hacker_status = get_status(hacker_success)
86+
7387
# Wallpapers Update
7488
display_header("Wallpaper Updates")
7589
wall_success, _ = run_command(WALLPAPERS_UPDATE_SCRIPT)
7690
wall_status = get_status(wall_success)
91+
7792
{apt_status, flatpak_status, snap_status, brew_status, fw_status, omz_status, distrobox_status, hacker_status, wall_status}
7893
end
94+
7995
def show_summary(apt_status, flatpak_status, snap_status, brew_status, fw_status, omz_status, distrobox_status, hacker_status, wall_status)
8096
puts "\nSystem Updates - #{apt_status}"
8197
puts "Flatpak Updates - #{flatpak_status}"
@@ -87,6 +103,7 @@ def show_summary(apt_status, flatpak_status, snap_status, brew_status, fw_status
87103
puts "HackerOS Updates - #{hacker_status}"
88104
puts "Wallpaper Updates - #{wall_status}"
89105
end
106+
90107
def enable_automatic_updates
91108
# Create a script that waits for internet and runs the updater
92109
auto_script = <<-SCRIPT
@@ -98,6 +115,7 @@ def enable_automatic_updates
98115
SCRIPT
99116
File.write(AUTO_SCRIPT_PATH, auto_script)
100117
File.chmod(AUTO_SCRIPT_PATH, 0o755)
118+
101119
# Add to crontab
102120
current_crontab = `crontab -l`
103121
unless current_crontab.includes?("@reboot #{AUTO_SCRIPT_PATH}")
@@ -108,17 +126,20 @@ def enable_automatic_updates
108126
end
109127
puts "#{GREEN}Automatic updates enabled.#{RESET}"
110128
end
129+
111130
def disable_automatic_updates
112131
# Remove from crontab
113132
current_crontab = `crontab -l`
114133
new_crontab = current_crontab.lines.reject { |line| line.includes?("@reboot #{AUTO_SCRIPT_PATH}") }.join("\n")
115134
File.write("/tmp/crontab.txt", new_crontab)
116135
run_command("crontab /tmp/crontab.txt")
117136
File.delete("/tmp/crontab.txt")
137+
118138
# Remove script if exists
119139
File.delete(AUTO_SCRIPT_PATH) if File.exists?(AUTO_SCRIPT_PATH)
120140
puts "#{GREEN}Automatic updates disabled.#{RESET}"
121141
end
142+
122143
def show_gui_menu
123144
loop do
124145
puts "\n#{YELLOW}=== HackerOS Updater Menu ===#{RESET}"
@@ -128,6 +149,7 @@ def show_gui_menu
128149
puts "#{GREEN}[L]og out#{RESET} #{CYAN}- Log out from current session#{RESET}"
129150
puts "#{GREEN}[T]erminal#{RESET} #{CYAN}- Open a new Alacritty terminal#{RESET}"
130151
puts "#{GREEN}[A]utomatic Updates#{RESET} #{CYAN}- Enable automatic updates on boot#{RESET}"
152+
puts "#{GREEN}[D]isable Automatic Updates#{RESET} #{CYAN}- Disable automatic updates on boot#{RESET}" # Added disable option
131153
print "#{MAGENTA}Enter your choice: #{RESET}"
132154
choice = ""
133155
STDIN.raw do |io|
@@ -151,11 +173,14 @@ def show_gui_menu
151173
Process.new("alacritty", input: Process::Redirect::Close, output: Process::Redirect::Close, error: Process::Redirect::Close)
152174
when "A"
153175
enable_automatic_updates
176+
when "D"
177+
disable_automatic_updates
154178
else
155179
puts "#{RED}Invalid choice. Try again.#{RESET}"
156180
end
157181
end
158182
end
183+
159184
def main
160185
with_gui = false
161186
gui_mode = false
@@ -164,15 +189,19 @@ def main
164189
parser.on("--with-gui", "Run in GUI mode with Alacritty") { with_gui = true }
165190
parser.on("--gui-mode", "Internal GUI mode") { gui_mode = true }
166191
end
192+
167193
if with_gui
168194
# Launch in Alacritty with gui-mode
169195
Process.new("alacritty", args: ["-e", BIN_PATH, "--gui-mode"], input: Process::Redirect::Close, output: Process::Redirect::Close, error: Process::Redirect::Close)
170196
return
171197
end
198+
172199
apt_status, flatpak_status, snap_status, brew_status, fw_status, omz_status, distrobox_status, hacker_status, wall_status = perform_updates
173200
show_summary(apt_status, flatpak_status, snap_status, brew_status, fw_status, omz_status, distrobox_status, hacker_status, wall_status)
201+
174202
if gui_mode
175203
show_gui_menu
176204
end
177205
end
206+
178207
main

0 commit comments

Comments
 (0)