Class: Threatinator::CLI::Parser

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/threatinator/cli/parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#add_cli_args, #clean_opts, #fix_opts, #nest_opts

Constructor Details

#initializeParser

Returns a new instance of Parser.



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

def initialize
  @builder = nil
  @plugin_loader = Threatinator::PluginLoader.new
  @plugin_loader.load_all_plugins
  @run_action_config_class = Threatinator::Actions::Run::Config.generate(@plugin_loader)
  @mod = _init_mod
end

Instance Attribute Details

#builderObject

Returns the value of attribute builder.



55
56
57
# File 'lib/threatinator/cli/parser.rb', line 55

def builder
  @builder
end

#config_hashObject (readonly)

Returns the value of attribute config_hash.



56
57
58
# File 'lib/threatinator/cli/parser.rb', line 56

def config_hash
  @config_hash
end

#extra_argsObject (readonly)

Returns the value of attribute extra_args.



56
57
58
# File 'lib/threatinator/cli/parser.rb', line 56

def extra_args
  @extra_args
end

#run_action_config_classObject (readonly)

Returns the value of attribute run_action_config_class.



57
58
59
# File 'lib/threatinator/cli/parser.rb', line 57

def run_action_config_class
  @run_action_config_class
end

Instance Method Details

#_init_modObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/threatinator/cli/parser.rb', line 76

def _init_mod
  parser = self
  Module.new do
    extend Helpers
    extend GLI::App

    program_desc 'Threatinator!'

    add_cli_args(self, Threatinator::Config::Logger.properties('logger'))
    add_cli_args(self, Threatinator::Config::FeedSearch.properties('feed_search'))

    desc "fetch and parse a feed"
    command :run do |c|
      c.flag 'run.coverage_output', type: String, desc: "Write coverage analysis to the specified file (CSV format)"

      add_cli_args(c, parser.run_action_config_class.properties('run'))
      c.action do |global_options, options, args|
        if options["run.feed_provider"].nil?
          options["run.feed_provider"] = args.shift
        end

        if options["run.feed_name"].nil?
          options["run.feed_name"] = args.shift
        end

        parser.set_opts(global_options, options, args)

        builder = Threatinator::CLI::RunActionBuilder.new(parser.config_hash, parser.extra_args, parser.run_action_config_class)
        parser.builder = builder
      end
    end

    desc 'list out all the feeds'
    command :list do |c|
      add_cli_args(c, Threatinator::Actions::List::Config.properties('list'))
      c.action do |global_options, options, args|
        parser.set_opts(global_options, options, args)
        parser.builder = Threatinator::CLI::ListActionBuilder.new(parser.config_hash, parser.extra_args)
      end
    end
  end
end

#parse(args) ⇒ Object



67
68
69
# File 'lib/threatinator/cli/parser.rb', line 67

def parse(args)
  @mod.run(args)
end

#set_opts(global_options, options, args) ⇒ Object



71
72
73
74
# File 'lib/threatinator/cli/parser.rb', line 71

def set_opts(global_options, options, args)
  @config_hash = fix_opts(global_options.merge(options))
  @extra_args = args
end