lib/faastruby/cli/credentials.rb in faastruby-0.3.4 vs lib/faastruby/cli/credentials.rb in faastruby-0.3.5
- old
+ new
@@ -1,19 +1,20 @@
require 'json'
module FaaStRuby
class Credentials # TODO: change it to YAML?
def self.load_credentials_file(credentials_file = FaaStRuby.credentials_file)
- if File.file?(credentials_file)
- creds = Oj.load(File.read(credentials_file))
- return creds if creds.is_a?(Hash)
- return {}
- else
- {}
+ return {} unless File.file?(credentials_file)
+ creds = Oj.load(File.read(credentials_file))
+ if creds.is_a?(Hash)
+ creds.delete_if{|workspace, credentials| credentials.nil?}
+ return creds
end
+ return {}
end
def self.add(workspace_name, new_credentials, credentials_file)
+ FaaStRuby::CLI.error("Error trying to save null credentials. You probably found a bug in the gem. Please report it at https://github.com/FaaStRuby/faastruby-cli/issues/new") unless new_credentials
credentials = load_credentials_file(credentials_file)
credentials.merge!({workspace_name => new_credentials})
save_file(credentials, credentials_file)
end
@@ -29,9 +30,10 @@
symbol = '~'
else
color = :green
symbol = '+'
end
+ credentials.delete_if{|workspace, creds| creds.nil?}
File.open(credentials_file, 'w') {|f| f.write JSON.pretty_generate(credentials)}
puts "#{symbol} f #{credentials_file}".colorize(color)
end
def self.load_for(workspace_name, cred_file = FaaStRuby.credentials_file, exit_on_error: true)