Class: Rubyists::Opr::Commands::Gen

Inherits:
Rubyists::Opr::Command show all
Defined in:
lib/rubyists::opr/commands/gen.rb

Overview

Generate passwords

Instance Attribute Summary collapse

Attributes inherited from Rubyists::Opr::Command

#options

Instance Method Summary collapse

Methods inherited from Rubyists::Opr::Command

#command, #cursor, #editor, #exec_exist?, #generator, #pager, #platform, #prompt, #screen, #which

Constructor Details

#initialize(path, options) ⇒ Gen

Returns a new instance of Gen.



14
15
16
17
# File 'lib/rubyists::opr/commands/gen.rb', line 14

def initialize(path, options)
  @path_name = path
  @options = options
end

Instance Attribute Details

#path_nameObject (readonly)

Returns the value of attribute path_name.



13
14
15
# File 'lib/rubyists::opr/commands/gen.rb', line 13

def path_name
  @path_name
end

Instance Method Details

#execute(input: $stdin, output: $stdout) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/rubyists::opr/commands/gen.rb', line 49

def execute(input: $stdin, output: $stdout)
  chars = begin
            str = input.read_nonblock(1) << input.read.chomp
            str.chars
          rescue IO::EAGAINWaitReadable
            Opr::Utils::SAMPLES
          end
  chars = nil if options[:chars] == false
  if options[:size]
    min = max = options[:size]
  else
    min, max = options.values_at :min, :max
  end
  output_or_save Utils.passgen(min, max, chars), output: output
end

#output_or_save(pass, output: $stdout) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/rubyists::opr/commands/gen.rb', line 37

def output_or_save(pass, output: $stdout)
  return output.puts(pass) if path_name.nil?

  parsed = parse_path
  Opr. do
    vault = Vault.find_by_name(parsed.vault)
    vault.insert(title: parsed.name, type: :login, password: pass,
                 username: options[:username], notes: options[:notes])
  end
  output.puts "#{parsed.name} saved in #{parsed.vault}"
end

#parse_pathObject

Here we determine if the @path contains a vault or is just a name.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rubyists::opr/commands/gen.rb', line 21

def parse_path
  # If options[:vault] is supplied, consider the whole @path the item name
  return OpenStruct.new(vault: options[:vault], name: path_name) if options[:vault]

  # Otherwise see if the path starts with a vault
  parts = path_name.split('/', 2)
  # If we got 2 parts, the first is the vault, the rest is the item name
  if parts.size == 2
    vault, name = parts
  else
    name = path_name
    vault = options[:vault] || 'Private'
  end
  OpenStruct.new vault: vault, name: name
end