Module: Commands

Defined in:
lib/rokku/commands/commands.rb

Class Method Summary collapse

Class Method Details

.runObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rokku/commands/commands.rb', line 5

def self.run
  options = {}
  optparse = OptionParser.new do |opts|
    opts.banner = "\nHanami authorization policy generator
  Usage: rokku -n myapp -p user
  Flags:
  \n"

    opts.on("-n", "--app_name APP", "Specify the application name for the policy") do |app_name|
      options[:app_name] = app_name
    end

    opts.on("-p", "--policy POLICY", "Specify the policy name") do |policy|
      options[:policy] = policy
    end

    opts.on("-h", "--help", "Displays help") do
      puts opts
      exit
    end
  end

  begin
    optparse.parse!
    puts "Add flag -h or --help to see usage instructions." if options.empty?
    mandatory = [:app_name, :policy]
    missing = mandatory.select{ |arg| options[arg].nil? }
    unless missing.empty?
      raise OptionParser::MissingArgument.new(missing.join(', '))
    end
  rescue OptionParser::InvalidOption, OptionParser::MissingArgument
    puts $!.to_s
    puts optparse
    exit
  end

  puts "Performing task with options: #{options.inspect}"
  generate_policy("#{options[:app_name]}", "#{options[:policy]}") if options[:policy]
end