CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/lib/metasploit/framework/command/base.rb
Views: 1904
1
#
2
# Gems
3
#
4
5
require 'active_support/core_ext/module/introspection'
6
7
#
8
# Project
9
#
10
11
require 'metasploit/framework/command'
12
require 'metasploit/framework/parsed_options'
13
require 'metasploit/framework/require'
14
15
# Based on pattern used for lib/rails/commands in the railties gem.
16
class Metasploit::Framework::Command::Base
17
#
18
# Attributes
19
#
20
21
# @!attribute [r] application
22
# The Rails application for metasploit-framework.
23
#
24
# @return [Metasploit::Framework::Application]
25
attr_reader :application
26
27
# @!attribute [r] parsed_options
28
# The parsed options from the command line.
29
#
30
# @return (see parsed_options)
31
attr_reader :parsed_options
32
33
#
34
# Class Methods
35
#
36
37
# @note {require_environment!} should be called to load
38
# `config/application.rb` to so that the RAILS_ENV can be set from the
39
# command line options in `ARGV` prior to `Rails.env` being set.
40
# @note After returning, `Rails.application` will be defined and configured.
41
#
42
# Parses `ARGV` for command line arguments to configure the
43
# `Rails.application`.
44
#
45
# @return (see parsed_options)
46
def self.require_environment!
47
parsed_options = self.parsed_options
48
# RAILS_ENV must be set before requiring 'config/application.rb'
49
parsed_options.environment!
50
ARGV.replace(parsed_options.positional)
51
52
# allow other Rails::Applications to use this command
53
if !defined?(Rails) || Rails.application.nil?
54
# @see https://github.com/rails/rails/blob/v3.2.17/railties/lib/rails/commands.rb#L39-L40
55
require Pathname.new(__FILE__).parent.parent.parent.parent.parent.join('config', 'application')
56
end
57
58
# have to configure before requiring environment because
59
# config/environment.rb calls initialize! and the initializers will use
60
# the configuration from the parsed options.
61
parsed_options.configure(Rails.application)
62
63
Rails.application.require_environment!
64
65
parsed_options
66
end
67
68
def self.parsed_options
69
parsed_options_class.new
70
end
71
72
def self.parsed_options_class
73
@parsed_options_class ||= parsed_options_class_name.constantize
74
end
75
76
def self.parsed_options_class_name
77
@parsed_options_class_name ||= "#{module_parent.module_parent}::ParsedOptions::#{name.demodulize}"
78
end
79
80
def self.start
81
parsed_options = require_environment!
82
new(application: Rails.application, parsed_options: parsed_options).start
83
end
84
85
#
86
# Instance Methods
87
#
88
89
# @param attributes [Hash{Symbol => ActiveSupport::OrderedOptions,Rails::Application}]
90
# @option attributes [Rails::Application] :application
91
# @option attributes [ActiveSupport::OrderedOptions] :parsed_options
92
# @raise [KeyError] if :application is not given
93
# @raise [KeyError] if :parsed_options is not given
94
def initialize(attributes={})
95
@application = attributes.fetch(:application)
96
@parsed_options = attributes.fetch(:parsed_options)
97
end
98
99
# @abstract Use {#application} to start this command.
100
#
101
# Starts this command.
102
#
103
# @return [void]
104
# @raise [NotImplementedError]
105
def start
106
raise NotImplementedError
107
end
108
end
109
110