lib/ayadn/switch.rb in ayadn-2.1 vs lib/ayadn/switch.rb in ayadn-3.0
- old
+ new
@@ -1,17 +1,24 @@
# 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")
+ @status = Status.new
+ begin
+ @acc_db = Amalgalite::Database.new(Dir.home + "/ayadn/accounts.sqlite")
+ rescue Amalgalite::SQLite3::Error => e
+ @status.not_authorized
+ exit
+ rescue => e
+ raise e
+ end
end
def list
puts "\n"
+ please if @acc_db.blank?
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 }
@@ -37,42 +44,42 @@
if user.empty? || user.nil?
@status.no_username
exit
end
username = Workers.new.remove_arobase_if_present([user.first])[0]
+ please if @acc_db.blank?
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
+ @status.say_green :done, "already authorized with username @#{username}"
end
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
+ @status.say_error "@#{username} isn't in the database"
+ @status.say_yellow :next, "please run `ayadn -auth` to authorize this account"
end
exit
else
@status.say do
- @thor.say_status :switching, "from @#{active_user} to @#{username}", :cyan
+ @status.say_cyan :switching, "from @#{active_user} to @#{username}"
Databases.set_active_account(@acc_db, username)
- @thor.say_status :done, "@#{username} is now the active account", :green
+ @status.say_green :done, "@#{username} is now the active account"
end
exit
end
end
private
def please
- @status.say do
- @thor.say_status :error, "please run `ayadn -auth` to authorize an account", :red
- end
+ @status.say_info "please run `ayadn -auth` to authorize an account"
+ puts
exit
end
end
end