Compare commits

...

2 Commits

Author SHA1 Message Date
7b61dd700a Added installation backup functionality 2024-10-19 10:16:53 -05:00
01a838c7f2 Pre change 2024-10-18 22:48:44 -05:00

View File

@ -3,12 +3,14 @@
# BEGIN: Includes & Requires
require 'erb'
require 'time'
require 'json'
require 'net/http'
require 'English'
require 'rubygems/package'
require 'fileutils'
require 'optparse'
require 'zlib'
# END: Includes & Requires
@ -46,6 +48,7 @@ DOWNLOAD_URL='https://discord.com/api/download/stable?platform=linux&format=tar.
$CONFIG = Hash.new
$CONFIG[:icon_dir] = Dir.new(Dir.home) / '.local' / 'share' / 'icons'
File.mkdir($CONFIG[:icon_dir]) if not File.exist?($CONFIG[:icon_dir])
$CONFIG[:discord_config_path] = Dir.new(Dir.home) / '.config' / 'discord'
$CONFIG[:local_application_install_dir] = Dir.new(Dir.home) / '.local' / 'share' / 'applications'
File.mkdir($CONFIG[:local_application_install_dir]) if not File.exist?($CONFIG[:local_application_install_dir])
$CONFIG[:desktop_path] = Dir.new(Dir.home) / '.local' / 'share' / 'applications' / 'discord.desktop'
@ -88,6 +91,15 @@ OptionParser.new do |parser|
$CONFIG[:action] = 'install'
end
parser.on('-u', '--uninstall', "Uninstall Discord (not-implemented)") do
$CONFIG[:action] = 'uninstall'
end
parser.on('-b', '--backup', "Backup Discord Installation (not-implemented)") do
$CONFIG[:action] = 'backup'
$CONFIG[:backup_filename] = 'discord-backup-' + Time.now.strftime('%Y-%m-%d') + '.tar.gz'
end
parser.separator ""
parser.separator " General Flags"
parser.separator "------------------------------------------------------------------------"
@ -127,6 +139,7 @@ def get_local_version
end
end
def download_installer(version)
uri = URI(URL_DOWNLOAD_BASE.gsub('{VERSION}', version.to_s))
puts "download_installer: Downloading The discord #{version.to_s} tar.gz"
@ -148,12 +161,13 @@ def do_install(version)
File.open(installer_file, 'rb') do |file|
Gem::Package.new("").extract_tar_gz(file, $CONFIG[:install_dir])
end
if not $CONFIG[:keep_installer]
puts "do_install: Removing new installer (#{installer_file})"
File.unlink(installer_file)
end
end
if not $CONFIG[:keep_installer]
puts "do_install: Removing new installer (#{installer_file})"
File.unlink(installer_file)
end
end
def update_desktop_file
@ -180,6 +194,60 @@ def update_desktop_file
end
end
def make_backup
files = {
files: Array.new,
dirs: Array.new,
symlinks: Array.new
}
Dir.glob("#{$CONFIG[:discord_config_path].path}/**/**/**/**").each{ |item|
if File.file?(item)
files[:files] << item
elsif File.symlink?(item)
files[:symlinks] << item
elsif File.directory?(item)
files[:dirs] << item
end
}
Dir.glob("#{$CONFIG[:discord_path].path}/**/**/**/**").each {|item|
if File.file?(item)
files[:files] << item
elsif File.symlink?(item)
files[:symlinks] << item
elsif File.directory?(item)
files[:dirs] << item
end
}
Dir.chdir(Dir.home) do
File.open($CONFIG[:backup_filename], "wb") do |backup_file|
Zlib::GzipWriter.wrap(backup_file) do |gzip|
Gem::Package::TarWriter.new(gzip) do |tar|
files[:files].each do |file|
File.open(file, 'rb') { |file_to_archive|
contents = file_to_archive.read()
tar.add_file_simple(file_to_archive.path.sub("#{Dir.home}/",''), file_to_archive.stat.mode, contents.length) do |io|
io.write(contents)
end
}
end
files[:dirs].each do |dir|
tar.mkdir(dir.sub("#{Dir.home}/",""), File.stat(dir).mode)
end
files[:symlinks].each {|symlink|
tar.add_symlink(symlink.sub("#{Dir.home}/",""), File.readlink(symlink), File.lstat(symlink).mode)
}
end
end
end
end
end
# END: Helper Functions
@ -207,6 +275,8 @@ case $CONFIG[:action]
puts " Remote Version: #{remote_version.to_s}"
puts " Local Version: #{local_version.to_s}"
puts " Needs Updating: #{(remote_version > local_version) ? "Yes" : "No"}"
when 'backup'
make_backup
end
# END: Work