-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added
Ronin::Recon::CLI::ConfigFileOption
(issue #178).
- Loading branch information
1 parent
0aedf2b
commit 53329e6
Showing
3 changed files
with
126 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |