Skip to content

Commit

Permalink
Added Ronin::Recon::CLI::ConfigFileOption (issue #178).
Browse files Browse the repository at this point in the history
  • Loading branch information
postmodern committed Sep 3, 2024
1 parent 0aedf2b commit 53329e6
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 17 deletions.
20 changes: 3 additions & 17 deletions lib/ronin/recon/cli/commands/run.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#

require_relative '../command'
require_relative '../config_file_option'
require_relative '../debug_option'
require_relative '../printing'
require_relative '../../value/parser'
Expand Down Expand Up @@ -65,19 +66,13 @@ module Commands
class Run < Command

include DebugOption
include ConfigFileOption
include Printing
include Core::CLI::Logging
include DB::CLI::DatabaseOptions

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

option :config_file, short: '-C',
value: {
type: String,
usage: 'FILE'
},
desc: 'Loads the configuration file'

option :worker, short: '-w',
value: {
type: String,
Expand Down Expand Up @@ -203,11 +198,6 @@ class Run < Command
# @return [Set<String>]
attr_reader :worker_files

# The loaded configuration for the {Engine}.
#
# @return [Config]
attr_reader :config

# The loaded workers for the {Engine}.
#
# @return [Workers]
Expand Down Expand Up @@ -339,11 +329,7 @@ def parse_value(value)
# the `--config-file` option or `~/.config/ronin-recon/config.yml`.
#
def load_config
@config = if (path = options[:config_file])
Config.load(path)
else
Config.default
end
super()

unless @only_workers.empty?
@config.workers = @only_workers
Expand Down
67 changes: 67 additions & 0 deletions lib/ronin/recon/cli/config_file_option.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# frozen_string_literal: true
#
# ronin-recon - A micro-framework and tool for performing reconnaissance.
#
# Copyright (c) 2023-2024 Hal Brodigan (postmodern.mod3@gmail.com)
#
# ronin-recon is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# ronin-recon is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with ronin-recon. If not, see <https://www.gnu.org/licenses/>.
#

require_relative '../config'

module Ronin
module Recon
class CLI
#
# Defines the `-C, --config-file` option.
#
# @since 0.2.0
#
module ConfigFileOption
#
# Defines the `-C, --config-file` option on the command class that
# included {ConfigFileOption}.
#
# @param [Class] command
# The command class that included {ConfigFileOption}.
#
def self.included(command)
command.option :config_file, short: '-C',
value: {
type: String,
usage: 'FILE'
},
desc: 'Loads the configuration file'
end

# The loaded configuration for the {Engine}.
#
# @return [Config]
attr_reader :config

#
# Loads the recon configuration file from either
# the `--config-file` option or `~/.config/ronin-recon/config.yml`.
#
def load_config
@config = if (path = options[:config_file])
Config.load(path)
else
Config.default
end
end
end
end
end
end
56 changes: 56 additions & 0 deletions spec/cli/config_file_option_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
require 'spec_helper'
require 'ronin/recon/cli/config_file_option'
require 'ronin/recon/cli/command'

describe Ronin::Recon::CLI::ConfigFileOption do
module TestConfigFileOption
class TestCommand < Ronin::Recon::CLI::Command

include Ronin::Recon::CLI::ConfigFileOption

end
end

let(:test_command) { TestConfigFileOption::TestCommand }
subject { test_command.new }

describe ".included" do
subject { test_command }

it "must define a '-C, --config-file' option" do
expect(subject.options[:config_file]).to_not be(nil)
expect(subject.options[:config_file].short).to eq('-C')
expect(subject.options[:config_file].value).to_not be(nil)
expect(subject.options[:config_file].value.type).to be(String)
expect(subject.options[:config_file].desc).to eq('Loads the configuration file')
end
end

describe "#load_config" do
let(:fixtures) { File.join(__dir__,'..','fixtures') }

context "when the '-C, --config-file' option is not given" do
before { subject.load_config }

it "must set @config to `Config.default`" do
expect(subject.config).to eq(Ronin::Recon::Config.default)
end
end

context "when the '-C, --config-file' option is given" do
let(:config_file) { File.join(fixtures,'config.yml') }

before do
subject.option_parser.parse(['--config-file', config_file])

subject.load_config
end

it "must load the config file and set @config" do
expect(subject.config).to eq(
Ronin::Recon::Config.load(config_file)
)
end
end
end
end

0 comments on commit 53329e6

Please sign in to comment.