Class: Rubyists::Opr::Commands::Gen
- Inherits:
-
Rubyists::Opr::Command
- Object
- Rubyists::Opr::Command
- Rubyists::Opr::Commands::Gen
- Defined in:
- lib/rubyists::opr/commands/gen.rb
Overview
Generate passwords
Instance Attribute Summary collapse
-
#path_name ⇒ Object
readonly
Returns the value of attribute path_name.
Attributes inherited from Rubyists::Opr::Command
Instance Method Summary collapse
- #execute(input: $stdin, output: $stdout) ⇒ Object
-
#initialize(path, options) ⇒ Gen
constructor
A new instance of Gen.
- #output_or_save(pass, output: $stdout) ⇒ Object
-
#parse_path ⇒ Object
Here we determine if the @path contains a vault or is just a name.
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, ) @path_name = path @options = end |
Instance Attribute Details
#path_name ⇒ Object (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 [:chars] == false if [:size] min = max = [:size] else min, max = .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.with_login do vault = Vault.find_by_name(parsed.vault) vault.insert(title: parsed.name, type: :login, password: pass, username: [:username], notes: [:notes]) end output.puts "#{parsed.name} saved in #{parsed.vault}" end |
#parse_path ⇒ Object
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: [:vault], name: path_name) if [: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 = [:vault] || 'Private' end OpenStruct.new vault: vault, name: name end |