lib/ayadn/switch.rb in ayadn-1.8.2 vs lib/ayadn/switch.rb in ayadn-2.0
- old
+ new
@@ -1,74 +1,78 @@
# encoding: utf-8
module Ayadn
class Switch
+ def initialize
+ @thor = Thor::Shell::Color.new # local statuses
+ @status = Status.new # global statuses + utils
+ @acc_db = Amalgalite::Database.new(Dir.home + "/ayadn/accounts.sqlite")
+ end
+
def list
- home_path = Dir.home + "/ayadn"
- if File.exist?("#{home_path}/accounts.db")
- accounts_db = Databases.init("#{home_path}/accounts.db")
- active = accounts_db['ACTIVE']
- begin
- puts "\nCurrently authorized accounts:\n".color(:cyan)
- accounts_db.each do |acc|
- next if acc[0] == 'ACTIVE'
- if acc[1][:username] == active
- puts "#{acc[1][:handle]}".color(:red)
- else
- puts "#{acc[1][:handle]}".color(:green)
- end
- end
- puts "\n"
- ensure
- close_db(accounts_db)
+ puts "\n"
+ accounts = Databases.all_accounts(@acc_db)
+ please if accounts.empty?
+ accounts.sort_by! { |acc| acc[0] }
+ table = Terminal::Table.new do |t|
+ t.style = { :width => 80 }
+ t.title = "Ayadn accounts"
+ t.headings = ['Username', 'ID', 'Path']
+ end
+ accounts.each do |acc|
+ username = acc[2]
+ id = acc[1].to_s
+ path = "~/ayadn/#{File.basename(acc[3])}"
+ if acc[4] == 1
+ username = username.color(:green)
+ id = id.color(:green)
+ path = path.color(:green)
end
- else
- please
+ table << [username, id, path]
end
+ puts table
+ puts "\n"
end
def switch(user)
if user.empty? || user.nil?
- puts "\n\nOops, something went wrong, I couldn't get your username. Please try again.\n\n".color(:red)
+ @status.no_username
exit
end
- #puts "\e[H\e[2J"
username = Workers.new.remove_arobase_if_present([user.first])[0]
- home_path = Dir.home + "/ayadn"
- if File.exist?("#{home_path}/accounts.db")
- accounts_db = Databases.init("#{home_path}/accounts.db")
- active = accounts_db['ACTIVE']
- if username == accounts_db[active][:username]
- puts "\nYou're already authorized with username '#{accounts_db[active][:handle]}'.\n".color(:red)
- cancel(accounts_db)
+ accounts = Databases.all_accounts(@acc_db)
+ please if accounts.empty?
+ active = accounts.select { |acc| acc[4] == 1 }[0]
+ active_user = active[0]
+ if username == active_user
+ @status.say do
+ @thor.say_status :done, "already authorized with username @#{username}", :green
end
- if accounts_db[username]
- puts "\nSwitching to account @#{username}...".color(:green)
- accounts_db['ACTIVE'] = username
- close_db(accounts_db)
- puts Status.done
- exit
- else
- puts "\nThis account isn't in the database. Please run 'ayadn authorize'.\n".color(:red)
- cancel(accounts_db)
+ exit
+ end
+ flag = accounts.select { |acc| acc[0] == username }.flatten
+ if flag.empty?
+ @status.say do
+ @thor.say_status :error, "@#{username} isn't in the database", :red
+ @thor.say_status :next, "please run `ayadn -auth` to authorize this account", :yellow
end
+ exit
else
- please
+ @status.say do
+ @thor.say_status :switching, "from @#{active_user} to @#{username}", :cyan
+ Databases.set_active_account(@acc_db, username)
+ @thor.say_status :done, "@#{username} is now the active account", :green
+ end
+ exit
end
end
private
- def cancel(accounts_db)
- accounts_db.close
- exit
- end
- def close_db(db)
- db.flush
- db.close
- end
def please
- puts "\nPlease run 'ayadn authorize' first.\n".color(:red)
+ @status.say do
+ @thor.say_status :error, "please run `ayadn -auth` to authorize an account", :red
+ end
exit
end
end
end