Sha256: 467319f2774e0941aa9234a947945895f943948de233cca5b87c9d9975bfa375

Contents?: true

Size: 1.18 KB

Versions: 1

Compression:

Stored size: 1.18 KB

Contents

require 'highline/import'

module Liri
  class Manager
    class Credential
      FILE_NAME = 'liri-credentials.yml'
      def initialize(folder_path)
        @folder_path = folder_path
        @file_path = File.join(@folder_path, '/', FILE_NAME)
      end

      # Obtiene ususario y contraseƱa del sistema en el que se ejecuta el programa
      def get
        user, password = get_credentials
        unless user || password
          user, password = ask_credentials
          save_credentials(user, password)
        end
        return user, password
      end

      private
      def get_local_user
        %x[whoami].delete!("\n")
      end

      def get_credentials
        if File.exist?(@file_path)
          data = YAML.load(File.read(@file_path))
          return data['user'], data['password']
        else
          return nil, nil
        end
      end

      def ask_credentials
        local_user = get_local_user
        password = ask("Enter password of user #{local_user}: ") { |q| q.echo = "*" }
        return local_user, password
      end

      def save_credentials(user, password)
        File.write(@file_path, "user: #{user}\npassword: #{password}")
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
liri-0.3.1 lib/manager/credential.rb