Skip to content
This repository has been archived by the owner on Oct 22, 2020. It is now read-only.

Commit

Permalink
Reduce code duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
rastating committed Jun 23, 2018
1 parent e516b39 commit ca938d9
Show file tree
Hide file tree
Showing 5 changed files with 362 additions and 23 deletions.
1 change: 0 additions & 1 deletion lib/cli/console.rb
Expand Up @@ -41,7 +41,6 @@ def permitted_commands
def initialize
super

@global_opts = {}
@indent_level = 1
@indent = ' '

Expand Down
15 changes: 5 additions & 10 deletions lib/cli/modules.rb
Expand Up @@ -20,7 +20,7 @@ def back
end

def reload
unless context
unless module_loaded?
print_bad 'No module loaded yet'
return
end
Expand All @@ -32,12 +32,10 @@ def reload
print_good "Reloaded module: #{mod}"
rescue StandardError => e
print_bad "Failed to reload module: #{e}"
return
end

# Set any globally set options.
@global_opts.each do |k, v|
set_option_value(k, v, true)
end
apply_global_options(mod)
end

def use(module_path)
Expand All @@ -51,13 +49,10 @@ def use(module_path)
context_stack.push(context)
rescue StandardError => e
print_bad "Failed to load module: #{e}"
return
end

# Set any globally set options.
@global_opts.each do |k, v|
set_option_value(k, v, true)
end

apply_global_options(mod)
refresh_autocomplete_options
end

Expand Down
33 changes: 21 additions & 12 deletions lib/cli/options.rb
Expand Up @@ -3,6 +3,11 @@
module Cli
# Methods for handling commands that interact with module options.
module Options
def initialize
super
self.global_opts = {}
end

def unset(name)
unless context
print_bad 'No module loaded yet'
Expand All @@ -18,8 +23,15 @@ def unset(name)
print_good "Unset #{name}"
end

def apply_global_options(target)
return if target.nil?
global_opts.each do |k, v|
target.set_option_value(k, v)
end
end

def load_payload(name)
if context.module.to_s.split('::')[-2].eql? 'Auxiliary'
if context.module.aux_module?
print_warning 'Auxiliary modules do not use payloads'
return
end
Expand All @@ -30,15 +42,10 @@ def load_payload(name)
rescue StandardError => e
print_bad "Failed to load payload: #{e}"
print_bad e.backtrace.join("\n\t")
return
end

# Set any globally set options.
if payload
@global_opts.each do |k, v|
payload.set_option_value(k, v)
end
end

apply_global_options(payload)
refresh_autocomplete_options
end

Expand All @@ -63,22 +70,22 @@ def gset(name, *args)
end

value = args.join(' ')
@global_opts[name] = value
global_opts[name] = value
set_option_value(name, value, true) if context

print_good "Globally set the value of #{name} to #{value}"
end

def gunset(name)
@global_opts.delete(name)
context.module.unset_option(name) if context
global_opts.delete(name)
context.module.unset_option(name) if module_loaded?
print_good "Removed the global setting for #{name}"
end

def set(name, *args)
value = args.join(' ')

unless context
unless module_loaded?
print_warning 'No module loaded yet'
return
end
Expand All @@ -94,5 +101,7 @@ def set(name, *args)
print_bad e.backtrace.join("\n\t")
end
end

attr_accessor :global_opts
end
end
1 change: 1 addition & 0 deletions spec/lib/cli/modules_spec.rb
Expand Up @@ -10,6 +10,7 @@
allow(subject).to receive(:print_good)
allow(subject).to receive(:print_info)
allow(subject).to receive(:print_bad)
allow(subject).to receive(:print_warning)
allow(subject).to receive(:puts)
allow(subject).to receive(:indent_cursor).and_call_original
allow(subject).to receive(:print_table)
Expand Down

0 comments on commit ca938d9

Please sign in to comment.