Class: Trustworthy::CLI::Init

Inherits:
Object
  • Object
show all
Includes:
Command
Defined in:
lib/trustworthy/cli/init.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Command

#print_help, #say

Class Method Details

.descriptionObject



6
7
8
# File 'lib/trustworthy/cli/init.rb', line 6

def self.description
  'Generate a new master key and user keys'
end

Instance Method Details

#default_optionsObject



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

def default_options
  {keys: 2}.merge(super)
end

#parse_options(args) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/trustworthy/cli/init.rb', line 14

def parse_options(args)
  super('init', args) do |opts, options|
    opts.on('-k', '--keys N', OptionParser::DecimalInteger, 'Number of keys to generate (default: 2, minimum: 2)') do |k|
      options[:keys] = k
    end
  end
end

#run(args) ⇒ Object

rubocop:disable Metrics/AbcSize



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/trustworthy/cli/init.rb', line 23

def run(args)
  options = parse_options(args)

  if options[:keys] < 2
    print_help
    return
  end

  Trustworthy::Settings.open(options[:config_file]) do |settings|
    unless settings.empty?
      say("Config #{options[:config_file]} already exists")
      return # rubocop:disable Lint/NonLocalExitFromIterator
    end
  end

  say("Creating a new master key with #{options[:keys]} keys")

  master_key = Trustworthy::MasterKey.create
  prompt = Trustworthy::Prompt.new(options[:config_file], $terminal)
  options[:keys].times do
    key = master_key.create_key
    username = prompt.add_user_key(key)
    $terminal.say("Key #{username} added")
  end

  say("Created #{options[:config_file]}")
end