forked from deanwilson/puppet-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpuppet-explode-resource.rb
More file actions
38 lines (24 loc) · 839 Bytes
/
puppet-explode-resource.rb
File metadata and controls
38 lines (24 loc) · 839 Bytes
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
#!/usr/bin/ruby
step = 2
resource = STDIN.read
prefix_length = resource.match(/^(\s*)/)[0].length
header_regex = Regexp.new(/^\s*(\S+)\s+\{\s+(.+["']):\s+/)
# clean up whitespace
resource.strip!
matched = resource.match( header_regex )
# remove what we know about so we can split the attributes
resource.sub!( header_regex, '')
attribs = Hash.new
resource.scan(/(.+?)=>\s*(.+?),\s*/).each { |k,v| attribs[k.strip] = v.strip }
longest_key = attribs.keys.max_by{|a| a.length}
indent = prefix_length + step
####################### print the resource back out # template this
printf("%s%s { %s:\n", ( ' ' * prefix_length), matched[1], matched[2])
attribs.sort.each do |k,v|
printf("%s", ( ' ' * indent ))
print k.ljust(longest_key.length)
printf " => "
print v
printf(",\n")
end
printf("%s}\n", ( ' ' * prefix_length))