Module: LastpassCLI

Defined in:
lib/lastpass-cli.rb,
lib/lastpass-cli/item.rb,
lib/lastpass-cli/agent.rb,
lib/lastpass-cli/command.rb,
lib/lastpass-cli/version.rb,
lib/lastpass-cli/configuration.rb

Defined Under Namespace

Classes: Agent, Command, Configuration, Item

Constant Summary collapse

VERSION =
"0.1.1"

Class Method Summary collapse

Class Method Details

.add_note(name, note_type: nil, notes: nil, data: {}) ⇒ Object



58
59
60
61
62
63
64
65
# File 'lib/lastpass-cli.rb', line 58

def self.add_note(name, note_type: nil, notes: nil, data: {})
  stdin_data = ""
  data.each do |field, value|
    stdin_data << "#{field.capitalize}:#{value}\n"
  end
  stdin_data << "Notes:\n#{notes}\n"
  Command.run(Command.new.add(name: name, note_type: note_type), stdin_data: stdin_data)
end

.add_password(name, username:, password:, notes: nil, url: nil) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/lastpass-cli.rb', line 50

def self.add_password(name, username:, password:, notes: nil, url: nil)
  stdin_data = "Username:#{username}\nPassword:#{password}\n"
  stdin_data << "URL:#{url}\n" if url
  stdin_data << "Notes:\n#{notes}\n" if notes
  Command.run(Command.new.add(name: name), stdin_data: stdin_data)
  show(name)
end

.configurationObject



8
9
10
# File 'lib/lastpass-cli.rb', line 8

def self.configuration
  @configuration ||= Configuration.new
end

.configure {|configuration| ... } ⇒ Object

Yields:



16
17
18
# File 'lib/lastpass-cli.rb', line 16

def self.configure
  yield(configuration)
end

.itemsObject



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/lastpass-cli.rb', line 20

def self.items
  items = []
  out = Command.run(Command.new.ls)
  if !out.nil? && out != ""
    out.each_line do |line|
      match_data = line.match(/(?<modified_at>\d{4}-\d{2}-\d{2} \d{2}:\d{2}) (?<folder>.+)\/(?<name>.+) \[id: (?<id>.*)\] \[username:\s?(?<username>.+)\]/)
      attributes = Hash[match_data.names.zip(match_data.captures)]
      items << Item.new(attributes)
    end
  end
  items
end

.reset_configuration!Object



12
13
14
# File 'lib/lastpass-cli.rb', line 12

def self.reset_configuration!
  @configuration = Configuration.new
end

.show(name) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/lastpass-cli.rb', line 33

def self.show(name)
  items = []
  out = Command.run(Command.new.show(name: name))
  if !out.nil? && out != "" && !out.start_with?("Error: ")
    out.each_line do |line|
      id_match = line.match(/^((?<folder>.*)\/)?(?<name>.*) \[id: (?<id>.*)\]/)
      if id_match
        items << Item.new(id: id_match[:id], folder: id_match[:folder], name: id_match[:name])
      else
        match_data = line.match(/^(?<key>.*): (?<value>.*)$/)
        items.last.set(match_data[:key].downcase, match_data[:value])
      end
    end
  end
  items
end