Skip to content

Commit

Permalink
(choria-io#83) support external applications
Browse files Browse the repository at this point in the history
Signed-off-by: R.I.Pienaar <[email protected]>
  • Loading branch information
ripienaar committed Dec 26, 2020
1 parent d618c3c commit 66a8b63
Showing 1 changed file with 38 additions and 1 deletion.
39 changes: 38 additions & 1 deletion lib/mcollective/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,24 @@ def description(descr)
self[:description] = descr
end

# Executes an external program instead of implement the logic in ruby
#
# @param [Hash] command the command to run
# @option command [String] :command the command to run
# @option command [Array] :args arguments to pass to the command
def external(command)
self[:external] = command
end

# Executes an external program to show help instead of supplying options
#
# @param [Hash] command the command to run
# @option command [String] :command the command to run
# @option command [Array] :args arguments to pass to the command
def external_help(command)
self[:external_help] = command
end

# Supplies usage information, calling multiple times will
# create multiple usage lines in --help output
def usage(usage)
Expand Down Expand Up @@ -82,7 +100,9 @@ def intialize_application_options
@application_options = {:description => nil,
:usage => [],
:cli_arguments => [],
:exclude_arg_sections => []}
:exclude_arg_sections => [],
:external => nil,
:external_help => nil}
end
end

Expand Down Expand Up @@ -267,14 +287,23 @@ def application_failure(err, err_dest=$stderr)
exit 1
end

def external_help
ext = application_options[:external_help]
exec(ext[:command], ext[:args])
end

def help
return external_help if application_options[:external_help]

application_parse_options(true)
end

# The main logic loop, builds up the options, validate configuration and calls
# the main as supplied by the user. Disconnects when done and pass any exception
# onto the application_failure helper
def run
return external_main if application_options[:external]

application_parse_options

validate_configuration(configuration) if respond_to?(:validate_configuration)
Expand All @@ -293,6 +322,14 @@ def disconnect
rescue # rubocop:disable Lint/SuppressedException
end

def external_main
ext = application_options[:external]
args = ext[:args] || []
args.concat(ARGV)

exec(ext[:command], *args)
end

# Fake abstract class that logs if the user tries to use an application without
# supplying a main override method.
def main
Expand Down

0 comments on commit 66a8b63

Please sign in to comment.