Sha256: cafefa94da7615db5b42917142c0ea6ca4338cf9bf38992997fe73347a7466e3

Contents?: true

Size: 1.8 KB

Versions: 11

Compression:

Stored size: 1.8 KB

Contents

require 'highline/import'
module HammerCLIForeman
  class BasicCredentials < ApipieBindings::AbstractCredentials

    # Can keep username and passwords credentials and prompt for them when necessary
    # @param [Hash] params
    # @option params [String] :username when nil, user is prompted when the attribute is accessed
    # @option params [String] :password when nil, user is prompted when the attribute is accessed
    # @example use container with prompt
    #   c = HammerCLIForeman::BasicCredentials.new()
    #   c.username
    #   > [Foreman] Username: admin
    #   => "admin"
    # @example use container with preset value
    #   c = HammerCLIForeman::BasicCredentials.new(:username => 'admin')
    #   c.username
    #   => "admin"
    def initialize(params={})
      @username = params[:username]
      @password = params[:password]
    end

    # Get username. Prompt for it when not set
    # @return [String]
    def username
      @username ||= ask_user(_("[Foreman] Username: ")) if HammerCLI.interactive?
      @username
    end

    # Get password. Prompt for it when not set. Password characters are replaced with asterisks on the screen.
    # @return [String]
    def password
      @password ||= ask_user(_("[Foreman] Password for %s: ") % username, true) if HammerCLI.interactive?
      @password
    end

    def empty?
      !@username && !@password
    end

    def clear
      super
      @username = nil
      @password = nil
    end

    # Convert credentials to hash usable for merging to RestClient configuration.
    # @return [Hash]
    def to_params
      {
        :user => username,
        :password => password
      }
    end

    private

    def ask_user(prompt, silent=false)
      if silent
        ask(prompt) {|q| q.echo = false}
      else
        ask(prompt)
      end
    end

  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
hammer_cli_foreman-0.8.0 lib/hammer_cli_foreman/credentials.rb
hammer_cli_foreman-0.7.0 lib/hammer_cli_foreman/credentials.rb
hammer_cli_foreman-0.6.2 lib/hammer_cli_foreman/credentials.rb
hammer_cli_foreman-0.6.1 lib/hammer_cli_foreman/credentials.rb
hammer_cli_foreman-0.6.0 lib/hammer_cli_foreman/credentials.rb
hammer_cli_foreman-0.5.1 lib/hammer_cli_foreman/credentials.rb
hammer_cli_foreman-0.5.0 lib/hammer_cli_foreman/credentials.rb
hammer_cli_foreman-0.4.0 lib/hammer_cli_foreman/credentials.rb
hammer_cli_foreman-0.3.0 lib/hammer_cli_foreman/credentials.rb
hammer_cli_foreman-0.2.0 lib/hammer_cli_foreman/credentials.rb
hammer_cli_foreman-0.1.4 lib/hammer_cli_foreman/credentials.rb