Puppet Function: systemd::str2unitname
- Defined in:
- lib/puppet/functions/systemd/str2unitname.rb
- Function type:
- Ruby 4.x API
Overview
Convert a string for use as a systemd unit name See systemd.unit(5) for the escaping algorithm
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/puppet/functions/systemd/str2unitname.rb', line 5 Puppet::Functions.create_function(:'systemd::str2unitname') do dispatch :unitname do required_param 'String', :s optional_param 'Boolean', :pathmode end def unitname(s, pathmode=true) t = s # In path mode, drop leading and trailing slashes, then collapse duplicates t = t.gsub(/\/\/+/,'/').gsub(/\/$/,'').gsub(/^\//,'') if (pathmode) # In path mode, if the result is now empty, we'll make # it represent the root directory return '-' if pathmode && t.empty? # \x<nn> escape all chars except alpha-numerics, underbars, slashes and dots t = t.gsub(/([^A-Za-z0-9_\/.])/) { |c| "\\x#{c.ord.to_s(16)}" } # Replace all remaining slashes with hyphens # and escape any remaining leading dot return t.gsub(/\//,'-').gsub(/^\./, '\\x2e') end end |