Skip to content

Commit 53329e6

Browse files
committed
Added Ronin::Recon::CLI::ConfigFileOption (issue #178).
1 parent 0aedf2b commit 53329e6

File tree

3 files changed

+126
-17
lines changed

3 files changed

+126
-17
lines changed

lib/ronin/recon/cli/commands/run.rb

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#
2020

2121
require_relative '../command'
22+
require_relative '../config_file_option'
2223
require_relative '../debug_option'
2324
require_relative '../printing'
2425
require_relative '../../value/parser'
@@ -65,19 +66,13 @@ module Commands
6566
class Run < Command
6667

6768
include DebugOption
69+
include ConfigFileOption
6870
include Printing
6971
include Core::CLI::Logging
7072
include DB::CLI::DatabaseOptions
7173

7274
usage '[options] {IP | IP-range | DOMAIN | HOST | WILDCARD | WEBSITE} ...'
7375

74-
option :config_file, short: '-C',
75-
value: {
76-
type: String,
77-
usage: 'FILE'
78-
},
79-
desc: 'Loads the configuration file'
80-
8176
option :worker, short: '-w',
8277
value: {
8378
type: String,
@@ -203,11 +198,6 @@ class Run < Command
203198
# @return [Set<String>]
204199
attr_reader :worker_files
205200

206-
# The loaded configuration for the {Engine}.
207-
#
208-
# @return [Config]
209-
attr_reader :config
210-
211201
# The loaded workers for the {Engine}.
212202
#
213203
# @return [Workers]
@@ -339,11 +329,7 @@ def parse_value(value)
339329
# the `--config-file` option or `~/.config/ronin-recon/config.yml`.
340330
#
341331
def load_config
342-
@config = if (path = options[:config_file])
343-
Config.load(path)
344-
else
345-
Config.default
346-
end
332+
super()
347333

348334
unless @only_workers.empty?
349335
@config.workers = @only_workers
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# frozen_string_literal: true
2+
#
3+
# ronin-recon - A micro-framework and tool for performing reconnaissance.
4+
#
5+
# Copyright (c) 2023-2024 Hal Brodigan ([email protected])
6+
#
7+
# ronin-recon is free software: you can redistribute it and/or modify
8+
# it under the terms of the GNU Lesser General Public License as published
9+
# by the Free Software Foundation, either version 3 of the License, or
10+
# (at your option) any later version.
11+
#
12+
# ronin-recon is distributed in the hope that it will be useful,
13+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
# GNU Lesser General Public License for more details.
16+
#
17+
# You should have received a copy of the GNU Lesser General Public License
18+
# along with ronin-recon. If not, see <https://www.gnu.org/licenses/>.
19+
#
20+
21+
require_relative '../config'
22+
23+
module Ronin
24+
module Recon
25+
class CLI
26+
#
27+
# Defines the `-C, --config-file` option.
28+
#
29+
# @since 0.2.0
30+
#
31+
module ConfigFileOption
32+
#
33+
# Defines the `-C, --config-file` option on the command class that
34+
# included {ConfigFileOption}.
35+
#
36+
# @param [Class] command
37+
# The command class that included {ConfigFileOption}.
38+
#
39+
def self.included(command)
40+
command.option :config_file, short: '-C',
41+
value: {
42+
type: String,
43+
usage: 'FILE'
44+
},
45+
desc: 'Loads the configuration file'
46+
end
47+
48+
# The loaded configuration for the {Engine}.
49+
#
50+
# @return [Config]
51+
attr_reader :config
52+
53+
#
54+
# Loads the recon configuration file from either
55+
# the `--config-file` option or `~/.config/ronin-recon/config.yml`.
56+
#
57+
def load_config
58+
@config = if (path = options[:config_file])
59+
Config.load(path)
60+
else
61+
Config.default
62+
end
63+
end
64+
end
65+
end
66+
end
67+
end
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
require 'spec_helper'
2+
require 'ronin/recon/cli/config_file_option'
3+
require 'ronin/recon/cli/command'
4+
5+
describe Ronin::Recon::CLI::ConfigFileOption do
6+
module TestConfigFileOption
7+
class TestCommand < Ronin::Recon::CLI::Command
8+
9+
include Ronin::Recon::CLI::ConfigFileOption
10+
11+
end
12+
end
13+
14+
let(:test_command) { TestConfigFileOption::TestCommand }
15+
subject { test_command.new }
16+
17+
describe ".included" do
18+
subject { test_command }
19+
20+
it "must define a '-C, --config-file' option" do
21+
expect(subject.options[:config_file]).to_not be(nil)
22+
expect(subject.options[:config_file].short).to eq('-C')
23+
expect(subject.options[:config_file].value).to_not be(nil)
24+
expect(subject.options[:config_file].value.type).to be(String)
25+
expect(subject.options[:config_file].desc).to eq('Loads the configuration file')
26+
end
27+
end
28+
29+
describe "#load_config" do
30+
let(:fixtures) { File.join(__dir__,'..','fixtures') }
31+
32+
context "when the '-C, --config-file' option is not given" do
33+
before { subject.load_config }
34+
35+
it "must set @config to `Config.default`" do
36+
expect(subject.config).to eq(Ronin::Recon::Config.default)
37+
end
38+
end
39+
40+
context "when the '-C, --config-file' option is given" do
41+
let(:config_file) { File.join(fixtures,'config.yml') }
42+
43+
before do
44+
subject.option_parser.parse(['--config-file', config_file])
45+
46+
subject.load_config
47+
end
48+
49+
it "must load the config file and set @config" do
50+
expect(subject.config).to eq(
51+
Ronin::Recon::Config.load(config_file)
52+
)
53+
end
54+
end
55+
end
56+
end

0 commit comments

Comments
 (0)