Class: Omnivault::CLI

Inherits:
Thor
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/omnivault/cli.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.default_option_descObject



18
19
20
# File 'lib/omnivault/cli.rb', line 18

def self.default_option_desc
  '[-v VAULT]'
end

.default_optionsObject



14
15
16
# File 'lib/omnivault/cli.rb', line 14

def self.default_options
  option :vault, aliases: '-v', default: 'default'
end

.exit_on_failure?Boolean

Forward return codes on failures.

Returns:

  • (Boolean)


10
11
12
# File 'lib/omnivault/cli.rb', line 10

def self.exit_on_failure?
  true
end

Instance Method Details

#envObject



51
52
53
54
55
# File 'lib/omnivault/cli.rb', line 51

def env
  vault_by_name(options[:vault]).entries.each do |key, value|
    say "#{key}=#{Shellwords.escape(value)}"
  end
end

#exec(*args) ⇒ Object



60
61
62
63
64
65
# File 'lib/omnivault/cli.rb', line 60

def exec(*args)
  vault_by_name(options[:vault]).entries.each do |key, value|
    ENV[key] = value
  end
  system(*args)
end

#lsObject



70
71
72
73
74
# File 'lib/omnivault/cli.rb', line 70

def ls
  vault_by_name(options[:vault]).entries.keys.each do |key|
    say key
  end
end

#set(*args) ⇒ Object

Raises:

  • (ArgumentError)


25
26
27
28
29
30
31
32
# File 'lib/omnivault/cli.rb', line 25

def set(*args)
  raise ArgumentError, 'Wrong number of arguments' if args.empty?

  hash = Hash[args.map { |arg| arg.split('=', 2) }]
  hash.each do |key, value|
    vault_by_name(options[:vault]).store(key, value)
  end
end

#unset(*args) ⇒ Object

Raises:

  • (ArgumentError)


37
38
39
40
41
42
43
44
45
46
# File 'lib/omnivault/cli.rb', line 37

def unset(*args)
  raise ArgumentError, 'Wrong number of arguments' if args.empty?

  vault = vault_by_name(options[:vault])
  missing = args - vault.entries.keys
  raise Thor::Error, "Keys not found: #{missing.join(', ')}" if missing.any?
  args.each do |key|
    vault.remove(key)
  end
end