-
Notifications
You must be signed in to change notification settings - Fork 98
Expand file tree
/
Copy pathhash2csv.rb
More file actions
67 lines (56 loc) · 1.68 KB
/
Copy pathhash2csv.rb
File metadata and controls
67 lines (56 loc) · 1.68 KB
1
2
3
4
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# This is an autogenerated function, ported from the original legacy version.
# It /should work/ as is, but will not have all the benefits of the modern
# function API. You should see the function docs to learn how to add function
# signatures for type safety and to document this function using puppet-strings.
#
# https://puppet.com/docs/puppet/latest/custom_functions_ruby.html
#
# ---- original file header ----
#
# Converts the hash to a csv string
#
# $hash = {
# HOME => '/home/user',
# ENV1 => 'env1',
# SECRET => 'secret'
# }
#
# becomes:
#
# $string = "HOME='/home/user',ENV1='env1',SECRET='secret'"
#
# ---- original file header ----
#
# @summary
# Returns a csv formatted string from an hash in the form
# KEY=VALUE,KEY2=VALUE2,KEY3=VALUE3 ordered by key
#
#
Puppet::Functions.create_function(:'supervisord::hash2csv') do
# @param args
# The original array of arguments. Port this to individually managed params
# to get the full benefit of the modern function API.
#
# @return [Data type]
# Describe what the function returns here
#
dispatch :default_impl do
# Call the method named 'default_impl' when this is matched
# Port this to match individual params for better type safety
repeated_param 'Any', :args
end
def default_impl(*args)
raise(Puppet::ParseError, "hash2csv(): Wrong number of arguments " +
"given (#{args.size} of 1)") if args.size < 1
hash = args[0]
unless hash.is_a?(Hash)
raise(Puppet::ParseError, 'hash2csv(): Requires an Hash')
end
sorted_hash = hash.sort
result = ''
sorted_hash.each {|key, value|
result += "#{key}='#{value}',"
}
return result.chop!
end
end