Skip to content

Commit 08e0b7d

Browse files
author
Kevin Paulisse
committed
Add support for arbitrary commands
1 parent c45e58f commit 08e0b7d

1 file changed

Lines changed: 42 additions & 0 deletions

File tree

lib/octocatalog-diff/catalog-util/command.rb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,51 @@ def puppet_command
9595
cmdline << "--ssldir=#{Shellwords.escape(File.join(@compilation_dir, 'var', 'ssl'))}"
9696
cmdline << "--confdir=#{Shellwords.escape(@compilation_dir)}"
9797

98+
# Other parameters provided by the user
99+
override_and_append_commandline_with_user_supplied_arguments(cmdline)
100+
98101
# Return full command
99102
cmdline.join(' ')
100103
end
104+
105+
private
106+
107+
# Private: Mutate the command line with arguments that were passed directly from the
108+
# user. This appends new arguments and overwrites existing arguments.
109+
# @param cmdline [Array] Existing command line - mutated by this method
110+
def override_and_append_commandline_with_user_supplied_arguments(cmdline)
111+
return unless @options[:command_line].is_a?(Array)
112+
113+
@options[:command_line].each do |opt|
114+
# Validate format: Accept '--key=value' or '--key' only.
115+
unless opt =~ /\A--([^=\s]+)(=.+)?\z/
116+
raise ArgumentError, "Command line option '#{opt}' does not match format '--SOME_OPTION=SOME_VALUE'"
117+
end
118+
key = Shellwords.escape(Regexp.last_match(1))
119+
val = Regexp.last_match(2)
120+
val.sub!(/\A=/, '') if val.is_a?(String)
121+
122+
# Determine if command line already contains this setting. If yes, the setting provided
123+
# here should override. If no, then append to the commandline.
124+
new_setting = val.nil? ? "--#{key}" : "--#{key}=#{Shellwords.escape(val)}"
125+
ind = key_position(cmdline, key)
126+
if ind.nil?
127+
cmdline << new_setting
128+
else
129+
cmdline[ind] = new_setting
130+
end
131+
end
132+
end
133+
134+
# Private: Determine if the key (given by --key) is already defined in the
135+
# command line. Returns nil if it is not already defined, otherwise returns
136+
# the index.
137+
# @param cmdline [Array] Existing command line
138+
# @param key [String] Key to look up
139+
# @return [Fixnum] Index of where key is defined (nil if undefined)
140+
def key_position(cmdline, key)
141+
cmdline.index { |x| x == "--#{key}" || x =~ /\A--#{key}=/ }
142+
end
101143
end
102144
end
103145
end

0 commit comments

Comments
 (0)