Class: Confiner::Cli

Inherits:
Object
  • Object
show all
Includes:
Logger
Defined in:
lib/confiner/cli.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logger

#log, log_to, log_to=

Constructor Details

#initialize(*options) ⇒ Cli

Returns a new instance of Cli.



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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/confiner/cli.rb', line 13

def initialize(*options)
  @plugin_arguments = [] # arguments to be passed to the plugin(s)
  @rules_files = [] # which files were loaded
  @rules = []
  @plugin_options = { debug: false, dry_run: false }

  # default logging to standard out
  Logger.log_to = $stdout

  if options.include?('--')
    @plugin_arguments = options[options.index('--')..]
    options = options[0..options.index('--')]
  end

  @parser = OptionParser.new do |opts|
    opts.banner = <<~USAGE
      Usage: #{$PROGRAM_NAME} [options] [-- plugin_options]

      Examples:
        Run all rules within .confiner directory outputting to a log file:
          #{$PROGRAM_NAME} -o confiner.log
        Run a specific rule file overriding specific arguments for plugins:
          #{$PROGRAM_NAME} -r rules.yml -- --arg1=foo --arg2=bar
        Run all all_rules within a specific directory:
          #{$PROGRAM_NAME} -r ./all_rules

      Options:
    USAGE

    opts.on('-h', '--help', 'Show the help') do
      puts opts
      exit 0
    end

    opts.on('-r RULES', '--rules RULES', 'Path to rule yaml file or directory of rules') do |rule|
      rule.strip! # strip any trailing/leading spacess
      rules = File.expand_path(rule)

      raise "Rule file or directory `#{rules}` does not exist" unless File.exist?(rules)

      @rules = if File.directory?(rules)
                 Dir[File.join(rules, '**', '*.yml')].each_with_object([]) do |definitions, all_rules|
                   all_rules << load_yaml(definitions)
                 end
               else
                 [load_yaml(rules)]
               end
    end

    opts.on('-v', '--version', 'Show the version') do
      $stdout.puts "#{$PROGRAM_NAME} version #{VERSION}"
      exit(0)
    end

    opts.on('--dry-run', 'Dry run') do
      @plugin_options[:dry_run] = true
    end

    opts.on('--debug', 'Toggle debug mode') do
      @plugin_options[:debug] = true
    end

    opts.on('-o OUTPUT', '--output-to OUTPUT', 'File to output the log to') do |output_to|
      Logger.log_to = output_to
    end
  end

  @parser.parse!(options)

  log :confiner, 'Program Start'

  if @rules.empty?
    # load any and all rules within .confiner
    raise 'No rules to run. Are you missing a .confiner directory or -r argument?' unless Dir.exist?('.confiner')

    @rules = Dir[File.join('.confiner', '**', '*.yml')].each_with_object([]) do |definitions, rules|
      rules << load_yaml(definitions)
    end
  end

  log :rules, 'Using rule files:'
  @rules_files.each do |file|
    log :loaded, file, 2
  end
end

Instance Attribute Details

#actionObject

Returns the value of attribute action.



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

def action
  @action
end

#parserObject (readonly)

Returns the value of attribute parser.



11
12
13
# File 'lib/confiner/cli.rb', line 11

def parser
  @parser
end

#plugin_argumentsObject (readonly)

Returns the value of attribute plugin_arguments.



11
12
13
# File 'lib/confiner/cli.rb', line 11

def plugin_arguments
  @plugin_arguments
end

#pluginsObject

Returns the value of attribute plugins.



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

def plugins
  @plugins
end

Class Method Details

.run(*argv) ⇒ Object



110
111
112
# File 'lib/confiner/cli.rb', line 110

def self.run(*argv)
  new(*argv).run
end

Instance Method Details

#runObject

Run the confiner



100
101
102
103
104
105
106
107
108
# File 'lib/confiner/cli.rb', line 100

def run
  @rules.each do |rules|
    rules.each do |rule|
      process_rule(rule)
    end
  end

  log :confiner, 'Done'
end