#!/usr/bin/env ruby require 'bundler/setup' require 'mkalias' require 'usage' VALID_OPTIONS = %w(new list show remove add_signal remove_signal).freeze def usage puts Usage.header puts Usage.option_new puts Usage.option_list puts Usage.option_show puts Usage.option_remove puts Usage.option_signals puts Usage.attention abort end def new usage if ARGV.count < 3 alias_name = ARGV[1] commands = ARGV[2..-1] result = Mkalias.new_alias(alias_name, commands) if result puts " - Created Alias: #{alias_name}" check_signal else puts " ERROR: O Alias [#{alias_name}] já existe" end end def list alias_names = Mkalias.list_alias puts 'Registered Alias:' alias_names.each do |alias_name| puts " - #{alias_name}" end end def check_alias_exists(alias_list, used_alias_names) alias_not_founded = alias_list - used_alias_names unless alias_not_founded.empty? alias_not_founded = alias_not_founded.join(', ') puts "Alias not founded: #{alias_not_founded}" puts '' end end def show alias_names = ARGV.count < 2 ? Mkalias.list_alias : ARGV[1..-1] commands = Mkalias.show_alias(alias_names) check_alias_exists(alias_names, commands.keys) unless commands.empty? commands.each do |alias_name, alias_commands| puts "-> #{alias_name}" alias_commands.each { |alias_command| puts " $ #{alias_command}" } puts '' end end end def remove usage if ARGV.count < 2 alias_names = ARGV[1..-1] removed_alias = Mkalias.remove_alias(alias_names) check_alias_exists(alias_names, removed_alias) unless removed_alias.empty? puts 'Removed Alias:' removed_alias.each { |alias_name| puts "- #{alias_name}" } check_signal end end def add_signal result = Mkalias.add_signal if result puts "Add signal to call 'source ~/.bashrc'" puts " - Run '$ source ~/.bashrc' to update your bash" else puts 'The signal has already been added' end end def remove_signal result = Mkalias.remove_signal if result puts 'The signal was removed' puts " - Run '$ source ~/.bashrc' to update your bash" else puts 'The signal does not exist to be removed' end end def check_signal if Mkalias.signal? `kill -USR1 #{Process.ppid}` else puts " - Run '$ source ~/.bashrc' to use your alias" end end def main usage if ARGV.count == 0 || !VALID_OPTIONS.include?(ARGV[0]) option = ARGV[0] send(option.to_s) end main