Sha256: c66fac6fada4ab17b4060781ed50dfa0c734d2096bdea35bfcf543a21b105335

Contents?: true

Size: 911 Bytes

Versions: 4

Compression:

Stored size: 911 Bytes

Contents

class Gratan::Identifier::Auto
  def initialize(output, options = {})
    @output = output
    @options = options

    unless @options[:dry_run]
      if output == '-'
        @output = $stdout
      else
        @output = output
      end
    end

    @cache = {}
  end

  def identify(user, host)
    if @cache[user]
      password = @cache[user]
    else
      password = mkpasswd
      @cache[user] = password
    end

    puts_password(user, host, password)
    password
  end

  private

  def mkpasswd(len = 8)
    [*1..9, *'A'..'Z', *'a'..'z'].shuffle.slice(0, len).join
  end

  def puts_password(user, host, password)
    open_output do |f|
      f.puts("#{user}@#{host},#{password}")
    end
  end

  def open_output
    return if @options[:dry_run]

    if @output == '-'
      yield($stdout)
      $stdout.flush
    else
      open(@output, 'a') do |f|
        yield(f)
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
gratan-0.2.5 lib/gratan/identifier/auto.rb
gratan-0.2.4 lib/gratan/identifier/auto.rb
gratan-0.2.3 lib/gratan/identifier/auto.rb
gratan-0.2.2 lib/gratan/identifier/auto.rb