Sha256: 56b41397608197e9cdba933b147900326f3388adec39ef3f05b8d53a0907cdc8

Contents?: true

Size: 1.64 KB

Versions: 28

Compression:

Stored size: 1.64 KB

Contents

module FaaStRuby
  module NewCredentials
    require 'fileutils'
    require 'yaml'
    class CredentialsFile
      def initialize
        @folder = File.expand_path("~/.faastruby")
        @file = "#{@folder}/credentials.yml"
        rename_if_old_file_exists
        create_credentials_folder
      end

      def rename_if_old_file_exists
        old_file = File.expand_path("~/.faastruby")
        return true unless File.file?(old_file)
        new_file = File.expand_path("~/.faastruby.tor1")
        FileUtils.mv(old_file, new_file)
        return true
      end

      def create_credentials_folder
        return true if File.file?(@file)
        FileUtils.mkdir_p(@folder)
        clear
      end

      def read
        YAML.load(File.read(@file)) rescue {}
      end

      def get
        creds = read['credentials'] || {}
        unless creds['email'] && creds['api_key'] && creds['api_secret']
          FaaStRuby::CLI.error("\nYou are not logged in. To login, run: faastruby login\n\nIf you don't have an account, run 'faastruby signup' to create one.\n", color: nil)
        end
        creds
      end

      def has_user_logged_in?
        creds = read['credentials'] || {}
        creds['email'] && creds['api_key'] && creds['api_secret']
      end

      def save(email:, api_key:, api_secret:)
        yaml = {
          'credentials' => {
            'email' => email,
            'api_key' => api_key,
            'api_secret' => api_secret
          }
        }.to_yaml
        File.write(@file, yaml)
      end

      def clear
        yaml = {
          'credentials' => nil
        }.to_yaml
        File.write(@file, yaml)
      end
    end
  end
end

Version data entries

28 entries across 28 versions & 1 rubygems

Version Path
faastruby-0.5.30 lib/faastruby/cli/new_credentials.rb
faastruby-0.5.29 lib/faastruby/cli/new_credentials.rb
faastruby-0.5.28 lib/faastruby/cli/new_credentials.rb
faastruby-0.5.27 lib/faastruby/cli/new_credentials.rb
faastruby-0.5.26 lib/faastruby/cli/new_credentials.rb
faastruby-0.5.25 lib/faastruby/cli/new_credentials.rb
faastruby-0.5.24 lib/faastruby/cli/new_credentials.rb
faastruby-0.5.23 lib/faastruby/cli/new_credentials.rb
faastruby-0.5.22 lib/faastruby/cli/new_credentials.rb
faastruby-0.5.21 lib/faastruby/cli/new_credentials.rb
faastruby-0.5.20 lib/faastruby/cli/new_credentials.rb
faastruby-0.5.19 lib/faastruby/cli/new_credentials.rb
faastruby-0.5.18 lib/faastruby/cli/new_credentials.rb
faastruby-0.5.17 lib/faastruby/cli/new_credentials.rb
faastruby-0.5.16 lib/faastruby/cli/new_credentials.rb
faastruby-0.5.15 lib/faastruby/cli/new_credentials.rb
faastruby-0.5.14 lib/faastruby/cli/new_credentials.rb
faastruby-0.5.13 lib/faastruby/cli/new_credentials.rb
faastruby-0.5.12 lib/faastruby/cli/new_credentials.rb
faastruby-0.5.11 lib/faastruby/cli/new_credentials.rb