Sha256: 26380b3808c7dd72f63202cfd2607397bb9cca354b1ae99f8017d2cd43ea5ee1

Contents?: true

Size: 1.63 KB

Versions: 1

Compression:

Stored size: 1.63 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
      rescue Psych::SyntaxError
        # Este error ocurre cuando se guardan caracteres raros en el archivo liri-credentials.yml
        # en este caso se borra el archivo y se pide de nuevo la contraseƱa
        delete_credentials
        return nil, nil
      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

      def delete_credentials
        if File.exist?(@file_path)
          File.delete(@file_path)
          File.exist?(@file_path) ? false : true
        else
          false
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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