-
Notifications
You must be signed in to change notification settings - Fork 181
Expand file tree
/
Copy pathruby.rb
More file actions
154 lines (135 loc) · 4.14 KB
/
ruby.rb
File metadata and controls
154 lines (135 loc) · 4.14 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# frozen_string_literal: true
require File.expand_path('../../util/ini_file', __dir__)
Puppet::Type.type(:ini_setting).provide(:ruby) do
def self.instances
desc '
Creates new ini_setting file, a specific config file with a provider that uses
this as its parent and implements the method
self.file_path, and that will provide the value for the path to the
ini file.'
raise(Puppet::Error, 'Ini_settings only support collecting instances when a file path is hard coded') unless respond_to?(:file_path)
# figure out what to do about the seperator
ini_file = Puppet::Util::IniFile.new(file_path, '=')
resources = []
ini_file.section_names.each do |section_name|
ini_file.get_settings(section_name).each do |setting, value|
resources.push(
new(
name: namevar(section_name, setting),
value:,
ensure: :present,
),
)
end
end
resources
end
def self.namevar(section_name, setting)
setting.nil? ? section_name : "#{section_name}/#{setting}"
end
def exists?
if setting.nil?
ini_file.section_names.include?(section)
elsif ini_file.section?(section)
!ini_file.get_value(section, setting).nil?
elsif resource.parameters.key?(:force_new_section_creation) && !resource[:force_new_section_creation]
# for backwards compatibility, if a user is using their own ini_setting
# types but does not have this parameter, we need to fall back to the
# previous functionality which was to create the section. Anyone
# wishing to leverage this setting must define it in their provider
# type. See comments on
# https://github.com/puppetlabs/puppetlabs-inifile/pull/286
resource[:ensure] = :absent
resource[:force_new_section_creation]
elsif resource.parameters.key?(:force_new_section_creation) && resource[:force_new_section_creation]
!resource[:force_new_section_creation]
else
false
end
end
def create
if setting.nil? && resource[:value].nil?
ini_file.set_value(section)
else
ini_file.set_value(section, setting, separator, resource[:value])
end
ini_file.save
@ini_file = nil
end
def destroy
ini_file.remove_setting(section, setting)
ini_file.save
@ini_file = nil
end
def value
ini_file.get_value(section, setting)
end
def value=(_value)
if setting.nil? && resource[:value].nil?
ini_file.set_value(section)
else
ini_file.set_value(section, setting, separator, resource[:value])
end
ini_file.save
end
def section
# this method is here so that it can be overridden by a child provider
resource[:section]
end
def setting
# this method is here so that it can be overridden by a child provider
resource[:setting]
end
def file_path
# this method is here to support purging and sub-classing.
# if a user creates a type and subclasses our provider and provides a
# 'file_path' method, then they don't have to specify the
# path as a parameter for every ini_setting declaration.
# This implementation allows us to support that while still
# falling back to the parameter value when necessary.
if self.class.respond_to?(:file_path)
self.class.file_path
else
resource[:path]
end
end
def separator
if resource.class.validattr?(:key_val_separator)
resource[:key_val_separator] || '='
else
'='
end
end
def section_prefix
if resource.class.validattr?(:section_prefix)
resource[:section_prefix] || '['
else
'['
end
end
def section_suffix
if resource.class.validattr?(:section_suffix)
resource[:section_suffix] || ']'
else
']'
end
end
def indent_char
if resource.class.validattr?(:indent_char)
resource[:indent_char] || ' '
else
' '
end
end
def indent_width
if resource.class.validattr?(:indent_width)
resource[:indent_width] || nil
else
nil
end
end
private
def ini_file
@ini_file ||= Puppet::Util::IniFile.new(file_path, separator, section_prefix, section_suffix, indent_char, indent_width)
end
end