Class: RPass::Shell

Inherits:
Object
  • Object
show all
Extended by:
CLI::Task
Defined in:
lib/rpass/shell.rb

Instance Method Summary collapse

Constructor Details

#initialize(accounts, display: Display.new) ⇒ Shell

Returns a new instance of Shell.



15
16
17
18
19
# File 'lib/rpass/shell.rb', line 15

def initialize(accounts, display: Display.new)
  @accounts = accounts
  @matches = nil
  @display = display
end

Instance Method Details

#cp(args) ⇒ Object



32
33
34
# File 'lib/rpass/shell.rb', line 32

def cp(args)
  pbcopy < get_match(args[0]).password
end

#find_matches(string) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# File 'lib/rpass/shell.rb', line 55

def find_matches(string)
  accounts.select do ||
    matched = false
    %i{id name username password url group}.each do |method|
      matched ||= .send(method).to_s =~ /#{string}/
      break if matched
    end
    matched
  end
end

#get_match(index) ⇒ Object



40
41
42
43
44
45
# File 'lib/rpass/shell.rb', line 40

def get_match(index)
  index = index.to_i
  fail "No matched accounts" unless matches
  fail "No such index: #{index}" if index < 0 || index > matches.size - 1
  matches[index]
end

#ls(args) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/rpass/shell.rb', line 21

def ls(args)
  string = args.size == 0 ? "." : args.first
  reset_matches
  i = 0
  matched_accounts(string).each do ||
    puts display.(, i)
    puts
    i += 1
  end
end

#matched_accounts(string) ⇒ Object



51
52
53
# File 'lib/rpass/shell.rb', line 51

def matched_accounts(string)
  @matches ||= find_matches(string)
end


36
37
38
# File 'lib/rpass/shell.rb', line 36

def print(args)
  puts get_match(args[0]).password
end

#promptObject



66
67
68
# File 'lib/rpass/shell.rb', line 66

def prompt
  "rpass"
end

#reset_matchesObject



47
48
49
# File 'lib/rpass/shell.rb', line 47

def reset_matches
  @matches = nil
end