diff options
author | Gabriel Nagy <gabriel.nagy@puppet.com> | 2021-09-30 19:31:15 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-09-30 19:31:15 +0300 |
commit | 8f8c5a5b8f2addd157d353740202539218f2be9c (patch) | |
tree | 5ff1f5bbd08e7d556439df0f18756f10aeb34bf3 /lib | |
parent | 0d92e9f24280df1082ec8dfa2baf8778b853e0ca (diff) | |
parent | 855f3bba8bb1c29ea1ec2dae471c90bec7786f01 (diff) | |
download | puppet-augeas_core-8f8c5a5b8f2addd157d353740202539218f2be9c.tar.gz puppet-augeas_core-8f8c5a5b8f2addd157d353740202539218f2be9c.tar.bz2 |
Merge pull request #41 from BobosilaVictor/MODULES-11197
(MODULES-11197) Update puppetlabs-augeas_core to latest pdk template
Diffstat (limited to 'lib')
-rw-r--r-- | lib/puppet/provider/augeas/augeas.rb | 88 | ||||
-rw-r--r-- | lib/puppet/type/augeas.rb | 5 | ||||
-rw-r--r-- | lib/puppet_x/augeas/util/parser.rb | 62 |
3 files changed, 79 insertions, 76 deletions
diff --git a/lib/puppet/provider/augeas/augeas.rb b/lib/puppet/provider/augeas/augeas.rb index b0a3e4f..8a306db 100644 --- a/lib/puppet/provider/augeas/augeas.rb +++ b/lib/puppet/provider/augeas/augeas.rb @@ -86,7 +86,7 @@ Puppet::Type.type(:augeas).provide(:augeas) do sc = StringScanner.new(line) cmd = sc.scan(%r{\w+|==|!=}) formals = COMMANDS[cmd] - fail(_('Unknown command %{cmd}') % { cmd: cmd }) unless formals + raise(_('Unknown command %{cmd}') % { cmd: cmd }) unless formals argline << cmd narg = 0 formals.each do |f| @@ -95,22 +95,22 @@ Puppet::Type.type(:augeas).provide(:augeas) do if f == :path start = sc.pos nbracket = 0 - inSingleTick = false - inDoubleTick = false + in_single_tick = false + in_double_tick = false loop do sc.skip(%r{([^\]\[\s\\'"]|\\.)+}) ch = sc.getch nbracket += 1 if ch == '[' nbracket -= 1 if ch == ']' - inSingleTick = !inSingleTick if ch == "'" - inDoubleTick = !inDoubleTick if ch == '"' - fail(_('unmatched [')) if nbracket < 0 - break if (nbracket == 0 && !inSingleTick && !inDoubleTick && (ch =~ %r{\s})) || sc.eos? + in_single_tick = !in_single_tick if ch == "'" + in_double_tick = !in_double_tick if ch == '"' + raise(_('unmatched [')) if nbracket < 0 + break if (nbracket == 0 && !in_single_tick && !in_double_tick && (ch =~ %r{\s})) || sc.eos? end len = sc.pos - start len -= 1 unless sc.eos? p = sc.string[start, len] - fail(_('missing path argument %{narg} for %{cmd}') % { narg: narg, cmd: cmd }) if p.nil? + raise(_('missing path argument %{narg} for %{cmd}') % { narg: narg, cmd: cmd }) if p.nil? # Rip off any ticks if they are there. p = p[1, (p.size - 2)] if p[0, 1] == "'" || p[0, 1] == '"' @@ -122,7 +122,7 @@ Puppet::Type.type(:augeas).provide(:augeas) do end elsif f == :string delim = sc.peek(1) - if delim == "'" || delim == '"' + if ["'", '"'].include?(delim) sc.getch argline << sc.scan(%r{([^\\#{delim}]|(\\.))*}) # Unescape the delimiter so it's actually possible to have a @@ -136,12 +136,12 @@ Puppet::Type.type(:augeas).provide(:augeas) do else argline << sc.scan(%r{[^\s]+}) end - fail(_('missing string argument %{narg} for %{cmd}') % { narg: narg, cmd: cmd }) unless argline[-1] + raise(_('missing string argument %{narg} for %{cmd}') % { narg: narg, cmd: cmd }) unless argline[-1] elsif f == :comparator argline << sc.scan(%r{(==|!=|=~|<=|>=|<|>)}) unless argline[-1] puts sc.rest - fail(_('invalid comparator for command %{cmd}') % { cmd: cmd }) + raise(_('invalid comparator for command %{cmd}') % { cmd: cmd }) end elsif f == :int argline << sc.scan(%r{\d+}).to_i @@ -209,7 +209,7 @@ Puppet::Type.type(:augeas).provide(:augeas) do @aug = nil end - def is_numeric?(s) + def numeric?(s) case s when Integer true @@ -227,7 +227,7 @@ Puppet::Type.type(:augeas).provide(:augeas) do return_value = false # validate and tear apart the command - fail(_('Invalid command: %{cmd}') % { cmd: cmd_array.join(' ') }) if cmd_array.length < 4 + raise(_('Invalid command: %{cmd}') % { cmd: cmd_array.join(' ') }) if cmd_array.length < 4 _ = cmd_array.shift path = cmd_array.shift comparator = cmd_array.shift @@ -236,8 +236,8 @@ Puppet::Type.type(:augeas).provide(:augeas) do # check the value in augeas result = @aug.get(path) || '' - if ['<', '<=', '>=', '>'].include?(comparator) && is_numeric?(result) && - is_numeric?(arg) + if ['<', '<=', '>=', '>'].include?(comparator) && numeric?(result) && + numeric?(arg) resultf = result.to_f argf = arg.to_f return_value = resultf.send(comparator, argf) @@ -258,7 +258,7 @@ Puppet::Type.type(:augeas).provide(:augeas) do return_value = false # validate and tear apart the command - fail(_('Invalid command: %{cmd}') % { cmd: cmd_array.join(' ') }) if cmd_array.length < 3 + raise(_('Invalid command: %{cmd}') % { cmd: cmd_array.join(' ') }) if cmd_array.length < 3 _ = cmd_array.shift path = cmd_array.shift @@ -268,7 +268,7 @@ Puppet::Type.type(:augeas).provide(:augeas) do # Get the match paths from augeas result = @aug.match(path) || [] - fail(_("Error trying to get path '%{path}'") % { path: path }) if result == -1 + raise(_("Error trying to get path '%{path}'") % { path: path }) if result == -1 # Get the values of the match paths from augeas values = result.map { |r| @aug.get(r) } @@ -286,7 +286,7 @@ Puppet::Type.type(:augeas).provide(:augeas) do new_array = parse_to_array(arg) return_value = (values == new_array) rescue - fail(_('Invalid array in command: %{cmd}') % { cmd: cmd_array.join(' ') }) + raise(_('Invalid array in command: %{cmd}') % { cmd: cmd_array.join(' ') }) end when '!=' begin @@ -294,7 +294,7 @@ Puppet::Type.type(:augeas).provide(:augeas) do new_array = parse_to_array(arg) return_value = (values != new_array) rescue - fail(_('Invalid array in command: %{cmd}') % { cmd: cmd_array.join(' ') }) + raise(_('Invalid array in command: %{cmd}') % { cmd: cmd_array.join(' ') }) end end !!return_value @@ -306,7 +306,7 @@ Puppet::Type.type(:augeas).provide(:augeas) do return_value = false # validate and tear apart the command - fail(_('Invalid command: %{cmd}') % { cmd: cmd_array.join(' ') }) if cmd_array.length < 3 + raise(_('Invalid command: %{cmd}') % { cmd: cmd_array.join(' ') }) if cmd_array.length < 3 _ = cmd_array.shift path = cmd_array.shift @@ -316,12 +316,12 @@ Puppet::Type.type(:augeas).provide(:augeas) do # Get the values from augeas result = @aug.match(path) || [] - fail(_("Error trying to match path '%{path}'") % { path: path }) if result == -1 + raise(_("Error trying to match path '%{path}'") % { path: path }) if result == -1 # Now do the work case verb when 'size' - fail(_('Invalid command: %{cmd}') % { cmd: cmd_array.join(' ') }) if clause_array.length != 2 + raise(_('Invalid command: %{cmd}') % { cmd: cmd_array.join(' ') }) if clause_array.length != 2 comparator = clause_array.shift arg = clause_array.shift return_value = case comparator @@ -342,7 +342,7 @@ Puppet::Type.type(:augeas).provide(:augeas) do new_array = parse_to_array(arg) return_value = (result == new_array) rescue - fail(_('Invalid array in command: %{cmd}') % { cmd: cmd_array.join(' ') }) + raise(_('Invalid array in command: %{cmd}') % { cmd: cmd_array.join(' ') }) end when '!=' begin @@ -350,7 +350,7 @@ Puppet::Type.type(:augeas).provide(:augeas) do new_array = parse_to_array(arg) return_value = (result != new_array) rescue - fail(_('Invalid array in command: %{cmd}') % { cmd: cmd_array.join(' ') }) + raise(_('Invalid array in command: %{cmd}') % { cmd: cmd_array.join(' ') }) end end !!return_value @@ -438,7 +438,7 @@ Puppet::Type.type(:augeas).provide(:augeas) do when 'match' then return_value = process_match(cmd_array) end rescue StandardError => e - fail(_("Error sending command '%{command}' with params %{param}/%{message}") % { command: command, param: cmd_array[1..-1].inspect, message: e.message }) + raise(_("Error sending command '%{command}' with params %{param}/%{message}") % { command: command, param: cmd_array[1..-1].inspect, message: e.message }) end end @@ -453,7 +453,7 @@ Puppet::Type.type(:augeas).provide(:augeas) do save_result = @aug.save unless save_result print_put_errors - fail(_('Save failed, see debug output for details')) + raise(Puppet::Error, _('Save failed, see debug output for details')) end saved_files = @aug.match('/augeas/events/saved') @@ -495,7 +495,7 @@ Puppet::Type.type(:augeas).provide(:augeas) do do_execute_changes unless @aug.save print_put_errors - fail(_('Save failed, see debug output for details')) + raise(Puppet::Error, _('Save failed, see debug output for details')) end :executed @@ -508,7 +508,7 @@ Puppet::Type.type(:augeas).provide(:augeas) do def do_execute_changes commands = parse_commands(resource[:changes]) commands.each do |cmd_array| - fail(_('invalid command %{cmd}') % { value0: cmd_array.join[' '] }) if cmd_array.length < 2 + raise(_('invalid command %{cmd}') % { value0: cmd_array.join[' '] }) if cmd_array.length < 2 command = cmd_array[0] cmd_array.shift begin @@ -516,37 +516,37 @@ Puppet::Type.type(:augeas).provide(:augeas) do when 'set' debug("sending command '#{command}' with params #{cmd_array.inspect}") rv = aug.set(cmd_array[0], cmd_array[1]) - fail(_("Error sending command '%{command}' with params %{params}") % { command: command, params: cmd_array.inspect }) unless rv + raise(_("Error sending command '%{command}' with params %{params}") % { command: command, params: cmd_array.inspect }) unless rv when 'setm' if aug.respond_to?(command) debug("sending command '#{command}' with params #{cmd_array.inspect}") rv = aug.setm(cmd_array[0], cmd_array[1], cmd_array[2]) - fail(_("Error sending command '%{command}' with params %{params}") % { command: command, params: cmd_array.inspect }) if rv == -1 + raise(_("Error sending command '%{command}' with params %{params}") % { command: command, params: cmd_array.inspect }) if rv == -1 else - fail(_("command '%{command}' not supported in installed version of ruby-augeas") % { command: command }) + raise(_("command '%{command}' not supported in installed version of ruby-augeas") % { command: command }) end when 'rm', 'remove' debug("sending command '#{command}' with params #{cmd_array.inspect}") rv = aug.rm(cmd_array[0]) - fail(_("Error sending command '%{command}' with params %{params}") % { command: command, params: cmd_array.inspect }) if rv == -1 + raise(_("Error sending command '%{command}' with params %{params}") % { command: command, params: cmd_array.inspect }) if rv == -1 when 'clear' debug("sending command '#{command}' with params #{cmd_array.inspect}") rv = aug.clear(cmd_array[0]) - fail(_("Error sending command '%{command}' with params %{params}") % { command: command, params: cmd_array.inspect }) unless rv + raise(_("Error sending command '%{command}' with params %{params}") % { command: command, params: cmd_array.inspect }) unless rv when 'clearm' # Check command exists ... doesn't currently in ruby-augeas 0.4.1 if aug.respond_to?(command) debug("sending command '#{command}' with params #{cmd_array.inspect}") rv = aug.clearm(cmd_array[0], cmd_array[1]) - fail(_("Error sending command '%{command}' with params %{params}") % { command: command, params: cmd_array.inspect }) unless rv + raise(_("Error sending command '%{command}' with params %{params}") % { command: command, params: cmd_array.inspect }) unless rv else - fail(_("command '%{command}' not supported in installed version of ruby-augeas") % { command: command }) + raise(_("command '%{command}' not supported in installed version of ruby-augeas") % { command: command }) end when 'touch' debug("sending command '#{command}' (match, set) with params #{cmd_array.inspect}") if aug.match(cmd_array[0]).empty? rv = aug.clear(cmd_array[0]) - fail(_("Error sending command '%{command}' with params %{params}") % { command: command, params: cmd_array.inspect }) unless rv + raise(_("Error sending command '%{command}' with params %{params}") % { command: command, params: cmd_array.inspect }) unless rv end when 'insert', 'ins' label = cmd_array[0] @@ -555,31 +555,31 @@ Puppet::Type.type(:augeas).provide(:augeas) do case where when 'before' then before = true when 'after' then before = false - else fail(_("Invalid value '%{where}' for where param") % { where: where }) + else raise(_("Invalid value '%{where}' for where param") % { where: where }) end debug("sending command '#{command}' with params #{[label, where, path].inspect}") rv = aug.insert(path, label, before) - fail(_("Error sending command '%{command}' with params %{params}") % { command: command, params: cmd_array.inspect }) if rv == -1 + raise(_("Error sending command '%{command}' with params %{params}") % { command: command, params: cmd_array.inspect }) if rv == -1 when 'defvar' debug("sending command '#{command}' with params #{cmd_array.inspect}") rv = aug.defvar(cmd_array[0], cmd_array[1]) - fail(_("Error sending command '%{command}' with params %{params}") % { command: command, params: cmd_array.inspect }) unless rv + raise(_("Error sending command '%{command}' with params %{params}") % { command: command, params: cmd_array.inspect }) unless rv when 'defnode' debug("sending command '#{command}' with params #{cmd_array.inspect}") rv = aug.defnode(cmd_array[0], cmd_array[1], cmd_array[2]) - fail(_("Error sending command '%{command}' with params %{params}") % { command: command, params: cmd_array.inspect }) unless rv + raise(_("Error sending command '%{command}' with params %{params}") % { command: command, params: cmd_array.inspect }) unless rv when 'mv', 'move' debug("sending command '#{command}' with params #{cmd_array.inspect}") rv = aug.mv(cmd_array[0], cmd_array[1]) - fail(_("Error sending command '%{command}' with params %{params}") % { command: command, params: cmd_array.inspect }) if rv == -1 + raise(_("Error sending command '%{command}' with params %{params}") % { command: command, params: cmd_array.inspect }) if rv == -1 when 'rename' debug("sending command '#{command}' with params #{cmd_array.inspect}") rv = aug.rename(cmd_array[0], cmd_array[1]) - fail(_("Error sending command '%{command}' with params %{params}") % { command: command, params: cmd_array.inspect }) if rv == -1 - else fail(_("Command '%{command}' is not supported") % { command: command }) + raise(_("Error sending command '%{command}' with params %{params}") % { command: command, params: cmd_array.inspect }) if rv == -1 + else raise(_("Command '%{command}' is not supported") % { command: command }) end rescue StandardError => e - fail(_("Error sending command '%{command}' with params %{params}/%{message}") % { command: command, params: cmd_array.inspect, message: e.message }) + raise(_("Error sending command '%{command}' with params %{params}/%{message}") % { command: command, params: cmd_array.inspect, message: e.message }) end end end diff --git a/lib/puppet/type/augeas.rb b/lib/puppet/type/augeas.rb index 20366f6..2303fdc 100644 --- a/lib/puppet/type/augeas.rb +++ b/lib/puppet/type/augeas.rb @@ -129,7 +129,8 @@ Puppet::Type.newtype(:augeas) do end newparam(:load_path) do - desc "Optional colon-separated list or array of directories; these directories are searched for schema definitions. The agent's `$libdir/augeas/lenses` path will always be added to support pluginsync." + desc "Optional colon-separated list or array of directories; these directories are searched for schema definitions. + The agent's `$libdir/augeas/lenses` path will always be added to support pluginsync." defaultto '' end @@ -162,7 +163,7 @@ Puppet::Type.newtype(:augeas) do validate do has_lens = !self[:lens].nil? has_incl = !self[:incl].nil? - fail(_('You must specify both the lens and incl parameters, or neither.')) if has_lens != has_incl + raise(Puppet::Error, _('You must specify both the lens and incl parameters, or neither.')) if has_lens != has_incl end newparam(:show_diff, boolean: true, parent: Puppet::Parameter::Boolean) do diff --git a/lib/puppet_x/augeas/util/parser.rb b/lib/puppet_x/augeas/util/parser.rb index 1abf42f..271e11c 100644 --- a/lib/puppet_x/augeas/util/parser.rb +++ b/lib/puppet_x/augeas/util/parser.rb @@ -1,27 +1,29 @@ # rubocop:disable Style/Documentation module PuppetX; end + module PuppetX::Augeas; end + module PuppetX::Augeas::Util; end # rubocop:enable Style/Documentation # Container for helpers to parse user provided data contained in manifests. module PuppetX::Augeas::Util::Parser - TOKEN_ARRAY_CLOSE = %r{\s*\]\s*} - TOKEN_ARRAY_OPEN = %r{\s*\[\s*} - TOKEN_ARRAY_SEPARATOR = %r{\s*,\s*} - TOKEN_CLOSE_CURLY = %r|}| - TOKEN_DOUBLE_QUOTE = %r{"} - TOKEN_DOUBLE_QUOTE_ESCAPED_CHAR = %r{\\(["\\abtnvfres0-7xu])} - TOKEN_DOUBLE_QUOTE_UNESCAPED_CHAR = %r{[^"\\]} - TOKEN_HEX_CHAR = %r{[0-9a-fA-F]{1,2}} - TOKEN_OCTAL_CHAR = %r{[0-7]{1,3}} - TOKEN_OPEN_CURLY = %r|{| - TOKEN_SINGLE_QUOTE = %r{'} - TOKEN_SINGLE_QUOTE_ESCAPED_CHAR = %r{\\(['\\])} - TOKEN_SINGLE_QUOTE_UNESCAPED_CHAR = %r{[^'\\]} - TOKEN_SPACE = %r{\s} - TOKEN_UNICODE_LONG_HEX_CHAR = %r{[0-9a-fA-F]{1,6}} - TOKEN_UNICODE_SHORT_HEX_CHAR = %r{[0-9a-fA-F]{4}} + TOKEN_ARRAY_CLOSE = %r{\s*\]\s*}.freeze + TOKEN_ARRAY_OPEN = %r{\s*\[\s*}.freeze + TOKEN_ARRAY_SEPARATOR = %r{\s*,\s*}.freeze + TOKEN_CLOSE_CURLY = %r|}|.freeze + TOKEN_DOUBLE_QUOTE = %r{"}.freeze + TOKEN_DOUBLE_QUOTE_ESCAPED_CHAR = %r{\\(["\\abtnvfres0-7xu])}.freeze + TOKEN_DOUBLE_QUOTE_UNESCAPED_CHAR = %r{[^"\\]}.freeze + TOKEN_HEX_CHAR = %r{[0-9a-fA-F]{1,2}}.freeze + TOKEN_OCTAL_CHAR = %r{[0-7]{1,3}}.freeze + TOKEN_OPEN_CURLY = %r|{|.freeze + TOKEN_SINGLE_QUOTE = %r{'}.freeze + TOKEN_SINGLE_QUOTE_ESCAPED_CHAR = %r{\\(['\\])}.freeze + TOKEN_SINGLE_QUOTE_UNESCAPED_CHAR = %r{[^'\\]}.freeze + TOKEN_SPACE = %r{\s}.freeze + TOKEN_UNICODE_LONG_HEX_CHAR = %r{[0-9a-fA-F]{1,6}}.freeze + TOKEN_UNICODE_SHORT_HEX_CHAR = %r{[0-9a-fA-F]{4}}.freeze # Parse a string into the (nearly) equivalent Ruby array. This only handles # arrays with string members (double-, or single-quoted), and does not @@ -88,34 +90,34 @@ module PuppetX::Augeas::Util::Parser return nil if match.nil? case scanner[1] - when '\\' then return '\\' - when '"' then return '"' - when 'a' then return "\a" - when 'b' then return "\b" - when 't' then return "\t" - when 'n' then return "\n" - when 'v' then return "\v" - when 'f' then return "\f" - when 'r' then return "\r" - when 'e' then return "\e" - when 's' then return "\s" + when '\\' then '\\' + when '"' then '"' + when 'a' then "\a" + when 'b' then "\b" + when 't' then "\t" + when 'n' then "\n" + when 'v' then "\v" + when 'f' then "\f" + when 'r' then "\r" + when 'e' then "\e" + when 's' then "\s" when %r{[0-7]} # Back the scanner up by one byte so we can grab all of the potential # octal digits at the same time. scanner.pos = scanner.pos - 1 octal_character = scanner.scan(TOKEN_OCTAL_CHAR) - return octal_character.to_i(8).chr + octal_character.to_i(8).chr when 'x' hex_character = scanner.scan(TOKEN_HEX_CHAR) return nil if hex_character.nil? hex_character.to_i(16).chr when 'u' - return unicode_short_hex_character(scanner) || unicode_long_hex_characters(scanner) + unicode_short_hex_character(scanner) || unicode_long_hex_characters(scanner) else # Not a valid escape sequence as far as we're concerned. - return nil + nil end end private :double_quote_escaped_char |