Sha256: be603eab8a499182ed9b0fb5ffcd4fa9a050bf57d88ef33bc8145cad200bcd61

Contents?: true

Size: 1.95 KB

Versions: 4

Compression:

Stored size: 1.95 KB

Contents

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
        {}
      end
    end

    def self.add(workspace_name, new_credentials, credentials_file)
      credentials = load_credentials_file(credentials_file)
      credentials.merge!({workspace_name => new_credentials})
      save_file(credentials, credentials_file)
    end

    def self.remove(workspace_name, credentials_file)
      credentials = load_credentials_file
      credentials.delete_if{|k,v| k == workspace_name}
      save_file(credentials, credentials_file)
    end

    def self.save_file(credentials, credentials_file)
      if File.file?(credentials_file)
        color = :yellow
        symbol = '~'
      else
        color = :green
        symbol = '+'
      end
      File.open(credentials_file, 'w') {|f| f.write Oj.dump(credentials)}
      puts "#{symbol} f #{credentials_file}".colorize(color)
    end

    def self.load_for(workspace_name, cred_file = FaaStRuby.credentials_file)
      credentials = load_from_env(workspace_name) || load_credentials_file(cred_file)
      FaaStRuby::CLI.error("Could not find credentials for '#{workspace_name}' in '#{cred_file}'") unless credentials.has_key?(workspace_name)
      FaaStRuby.configure do |config|
        config.api_key = credentials[workspace_name]['api_key']
        config.api_secret = credentials[workspace_name]['api_secret']
      end
    end

    def self.load_from_env(workspace_name)
      return nil unless ENV['FAASTRUBY_API_KEY'] && ENV['FAASTRUBY_API_SECRET']
      puts "#{"WARNING:".red} Using credentials from env vars FAASTRUBY_API_KEY and FAASTRUBY_API_SECRET"
      {workspace_name => {'api_key' => ENV['FAASTRUBY_API_KEY'], 'api_secret' => ENV['FAASTRUBY_API_SECRET']}}
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
faastruby-0.3.2 lib/faastruby/cli/credentials.rb
faastruby-0.3.1 lib/faastruby/cli/credentials.rb
faastruby-0.2.6 lib/faastruby/cli/credentials.rb
faastruby-0.2.5 lib/faastruby/cli/credentials.rb