class Trustworthy::CLI::Init

Public Class Methods

description() click to toggle source
# File lib/trustworthy/cli/init.rb, line 6
def self.description
  'Generate a new master key and user keys'
end

Public Instance Methods

default_options() click to toggle source
# File lib/trustworthy/cli/init.rb, line 10
def default_options
  {keys: 2}.merge(super)
end
parse_options(args) click to toggle source
Calls superclass method Trustworthy::CLI::Command#parse_options
# 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) click to toggle source

rubocop:disable Metrics/AbcSize

# 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