pdk 2.4.0 → 2.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +803 -1329
- data/LICENSE +201 -201
- data/README.md +165 -163
- data/exe/pdk +10 -10
- data/lib/pdk/analytics/client/google_analytics.rb +143 -143
- data/lib/pdk/analytics/client/noop.rb +25 -25
- data/lib/pdk/analytics/util.rb +19 -19
- data/lib/pdk/analytics.rb +30 -30
- data/lib/pdk/answer_file.rb +12 -12
- data/lib/pdk/bolt.rb +19 -19
- data/lib/pdk/cli/build.rb +82 -82
- data/lib/pdk/cli/bundle.rb +48 -48
- data/lib/pdk/cli/config/get.rb +26 -26
- data/lib/pdk/cli/config.rb +22 -22
- data/lib/pdk/cli/console.rb +148 -148
- data/lib/pdk/cli/convert.rb +52 -52
- data/lib/pdk/cli/env.rb +52 -52
- data/lib/pdk/cli/errors.rb +25 -25
- data/lib/pdk/cli/exec/command.rb +293 -293
- data/lib/pdk/cli/exec/interactive_command.rb +114 -114
- data/lib/pdk/cli/exec.rb +84 -84
- data/lib/pdk/cli/exec_group.rb +104 -104
- data/lib/pdk/cli/get/config.rb +24 -24
- data/lib/pdk/cli/get.rb +20 -20
- data/lib/pdk/cli/module/build.rb +12 -12
- data/lib/pdk/cli/module/generate.rb +47 -47
- data/lib/pdk/cli/module.rb +14 -14
- data/lib/pdk/cli/new/class.rb +32 -32
- data/lib/pdk/cli/new/defined_type.rb +32 -32
- data/lib/pdk/cli/new/fact.rb +29 -29
- data/lib/pdk/cli/new/function.rb +29 -29
- data/lib/pdk/cli/new/module.rb +53 -53
- data/lib/pdk/cli/new/provider.rb +29 -29
- data/lib/pdk/cli/new/task.rb +34 -34
- data/lib/pdk/cli/new/test.rb +52 -52
- data/lib/pdk/cli/new/transport.rb +27 -27
- data/lib/pdk/cli/new.rb +21 -21
- data/lib/pdk/cli/release/prep.rb +39 -39
- data/lib/pdk/cli/release/publish.rb +50 -50
- data/lib/pdk/cli/release.rb +194 -194
- data/lib/pdk/cli/remove/config.rb +80 -80
- data/lib/pdk/cli/remove.rb +20 -20
- data/lib/pdk/cli/set/config.rb +119 -119
- data/lib/pdk/cli/set.rb +20 -20
- data/lib/pdk/cli/test/unit.rb +90 -90
- data/lib/pdk/cli/test.rb +11 -11
- data/lib/pdk/cli/update.rb +64 -64
- data/lib/pdk/cli/util/command_redirector.rb +27 -27
- data/lib/pdk/cli/util/interview.rb +72 -72
- data/lib/pdk/cli/util/option_normalizer.rb +55 -55
- data/lib/pdk/cli/util/option_validator.rb +68 -68
- data/lib/pdk/cli/util/spinner.rb +13 -13
- data/lib/pdk/cli/util/update_manager_printer.rb +82 -82
- data/lib/pdk/cli/util.rb +305 -305
- data/lib/pdk/cli/validate.rb +116 -116
- data/lib/pdk/cli.rb +175 -175
- data/lib/pdk/config/analytics_schema.json +26 -26
- data/lib/pdk/config/errors.rb +5 -5
- data/lib/pdk/config/ini_file.rb +183 -183
- data/lib/pdk/config/ini_file_setting.rb +39 -39
- data/lib/pdk/config/json.rb +34 -34
- data/lib/pdk/config/json_schema_namespace.rb +142 -142
- data/lib/pdk/config/json_schema_setting.rb +53 -53
- data/lib/pdk/config/json_with_schema.rb +49 -49
- data/lib/pdk/config/namespace.rb +354 -354
- data/lib/pdk/config/setting.rb +135 -135
- data/lib/pdk/config/validator.rb +31 -31
- data/lib/pdk/config/yaml.rb +46 -46
- data/lib/pdk/config/yaml_with_schema.rb +59 -59
- data/lib/pdk/config.rb +390 -390
- data/lib/pdk/context/control_repo.rb +60 -60
- data/lib/pdk/context/module.rb +28 -28
- data/lib/pdk/context/none.rb +22 -22
- data/lib/pdk/context.rb +99 -99
- data/lib/pdk/control_repo.rb +90 -90
- data/lib/pdk/generate/defined_type.rb +43 -43
- data/lib/pdk/generate/fact.rb +25 -25
- data/lib/pdk/generate/function.rb +48 -48
- data/lib/pdk/generate/module.rb +352 -352
- data/lib/pdk/generate/provider.rb +28 -28
- data/lib/pdk/generate/puppet_class.rb +43 -43
- data/lib/pdk/generate/puppet_object.rb +232 -232
- data/lib/pdk/generate/task.rb +68 -68
- data/lib/pdk/generate/transport.rb +33 -33
- data/lib/pdk/generate.rb +24 -24
- data/lib/pdk/i18n.rb +4 -4
- data/lib/pdk/logger.rb +45 -45
- data/lib/pdk/module/build.rb +322 -322
- data/lib/pdk/module/convert.rb +296 -296
- data/lib/pdk/module/metadata.rb +202 -202
- data/lib/pdk/module/release.rb +260 -260
- data/lib/pdk/module/update.rb +131 -131
- data/lib/pdk/module/update_manager.rb +227 -227
- data/lib/pdk/module.rb +30 -30
- data/lib/pdk/report/event.rb +370 -370
- data/lib/pdk/report.rb +121 -121
- data/lib/pdk/template/fetcher/git.rb +85 -85
- data/lib/pdk/template/fetcher/local.rb +28 -28
- data/lib/pdk/template/fetcher.rb +98 -98
- data/lib/pdk/template/renderer/v1/legacy_template_dir.rb +116 -116
- data/lib/pdk/template/renderer/v1/renderer.rb +132 -132
- data/lib/pdk/template/renderer/v1/template_file.rb +102 -102
- data/lib/pdk/template/renderer/v1.rb +25 -25
- data/lib/pdk/template/renderer.rb +96 -96
- data/lib/pdk/template/template_dir.rb +67 -67
- data/lib/pdk/template.rb +59 -59
- data/lib/pdk/tests/unit.rb +252 -252
- data/lib/pdk/util/bundler.rb +259 -259
- data/lib/pdk/util/changelog_generator.rb +137 -137
- data/lib/pdk/util/env.rb +47 -47
- data/lib/pdk/util/filesystem.rb +138 -138
- data/lib/pdk/util/git.rb +179 -179
- data/lib/pdk/util/json_finder.rb +85 -85
- data/lib/pdk/util/puppet_strings.rb +125 -125
- data/lib/pdk/util/puppet_version.rb +266 -266
- data/lib/pdk/util/ruby_version.rb +179 -179
- data/lib/pdk/util/template_uri.rb +295 -295
- data/lib/pdk/util/vendored_file.rb +93 -93
- data/lib/pdk/util/version.rb +43 -43
- data/lib/pdk/util/windows/api_types.rb +82 -82
- data/lib/pdk/util/windows/file.rb +36 -36
- data/lib/pdk/util/windows/process.rb +79 -79
- data/lib/pdk/util/windows/string.rb +16 -16
- data/lib/pdk/util/windows.rb +15 -15
- data/lib/pdk/util.rb +278 -278
- data/lib/pdk/validate/control_repo/control_repo_validator_group.rb +23 -23
- data/lib/pdk/validate/control_repo/environment_conf_validator.rb +98 -98
- data/lib/pdk/validate/external_command_validator.rb +208 -208
- data/lib/pdk/validate/internal_ruby_validator.rb +100 -100
- data/lib/pdk/validate/invokable_validator.rb +228 -228
- data/lib/pdk/validate/metadata/metadata_json_lint_validator.rb +86 -86
- data/lib/pdk/validate/metadata/metadata_syntax_validator.rb +78 -78
- data/lib/pdk/validate/metadata/metadata_validator_group.rb +20 -20
- data/lib/pdk/validate/puppet/puppet_epp_validator.rb +133 -133
- data/lib/pdk/validate/puppet/puppet_lint_validator.rb +66 -66
- data/lib/pdk/validate/puppet/puppet_syntax_validator.rb +137 -137
- data/lib/pdk/validate/puppet/puppet_validator_group.rb +21 -21
- data/lib/pdk/validate/ruby/ruby_rubocop_validator.rb +80 -80
- data/lib/pdk/validate/ruby/ruby_validator_group.rb +19 -19
- data/lib/pdk/validate/tasks/tasks_metadata_lint_validator.rb +88 -88
- data/lib/pdk/validate/tasks/tasks_name_validator.rb +50 -50
- data/lib/pdk/validate/tasks/tasks_validator_group.rb +20 -20
- data/lib/pdk/validate/validator.rb +118 -118
- data/lib/pdk/validate/validator_group.rb +104 -104
- data/lib/pdk/validate/yaml/yaml_syntax_validator.rb +95 -95
- data/lib/pdk/validate/yaml/yaml_validator_group.rb +19 -19
- data/lib/pdk/validate.rb +94 -94
- data/lib/pdk/version.rb +4 -4
- data/lib/pdk.rb +76 -76
- data/locales/config.yaml +21 -21
- data/locales/pdk.pot +2111 -2094
- metadata +3 -3
@@ -1,142 +1,142 @@
|
|
1
|
-
require 'pdk'
|
2
|
-
|
3
|
-
# Due to https://github.com/ruby-json-schema/json-schema/issues/439
|
4
|
-
# Windows file paths "appear" as uri's with no host and a schema of drive letter
|
5
|
-
# Also it is not possible to craft a URI with a Windows path due to the URI object
|
6
|
-
# always prepending the path with forward slash (`/`) so Windows paths end up looking
|
7
|
-
# like '/C:\schema.json', which can not be read.
|
8
|
-
# Instead we need to monkey patch the Schema Reader reader to remove the errant forward slash
|
9
|
-
require 'json-schema/schema/reader'
|
10
|
-
module JSON
|
11
|
-
class Schema
|
12
|
-
class Reader
|
13
|
-
alias original_read_file read_file
|
14
|
-
|
15
|
-
def read_file(pathname)
|
16
|
-
new_pathname = JSON::Util::URI.unescaped_path(pathname.to_s)
|
17
|
-
# Munge the path if it looks like a Windows path e.g. /C:/Windows ...
|
18
|
-
# Note that UNC style paths do not have the same issue (\\host\path)
|
19
|
-
new_pathname.slice!(0) if new_pathname.start_with?('/') && new_pathname[2] == ':'
|
20
|
-
original_read_file(Pathname.new(new_pathname))
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
require 'json-schema'
|
27
|
-
|
28
|
-
module PDK
|
29
|
-
class Config
|
30
|
-
class JSONSchemaNamespace < Namespace
|
31
|
-
# Initialises the PDK::Config::JSONSchemaNamespace object.
|
32
|
-
#
|
33
|
-
# @see PDK::Config::Namespace.initialize
|
34
|
-
#
|
35
|
-
# @option params [String] :schema_file Path to the JSON Schema document
|
36
|
-
def initialize(name = nil, file: nil, parent: nil, persistent_defaults: false, schema_file: nil, &block)
|
37
|
-
super(name, file: file, parent: parent, persistent_defaults: persistent_defaults, &block)
|
38
|
-
@schema_file = schema_file
|
39
|
-
@unmanaged_settings = {}
|
40
|
-
end
|
41
|
-
|
42
|
-
# The JSON Schema for the namespace
|
43
|
-
#
|
44
|
-
# @return [Hash]
|
45
|
-
def schema
|
46
|
-
document_schema.schema
|
47
|
-
end
|
48
|
-
|
49
|
-
# Whether the schema is valid but empty.
|
50
|
-
#
|
51
|
-
# @return [Boolean]
|
52
|
-
def empty_schema?
|
53
|
-
document_schema.schema.empty?
|
54
|
-
end
|
55
|
-
|
56
|
-
# Name of all the top level properties for the schema
|
57
|
-
#
|
58
|
-
# @return [String[]]
|
59
|
-
def schema_property_names
|
60
|
-
return [] if schema['properties'].nil?
|
61
|
-
schema['properties'].keys
|
62
|
-
end
|
63
|
-
|
64
|
-
# Extends the to_h namespace method to include unmanaged settings
|
65
|
-
#
|
66
|
-
# @see PDK::Config::Namespace.to_h
|
67
|
-
def to_h
|
68
|
-
# This may seem counter-intuitive but we need to call super first as the settings
|
69
|
-
# may not have been loaded yet, which means @unmanaged_settings will be empty.
|
70
|
-
# We call super first to force any file loading and then merge the unmanaged settings
|
71
|
-
settings_hash = super
|
72
|
-
@unmanaged_settings = {} if @unmanaged_settings.nil?
|
73
|
-
@unmanaged_settings.merge(settings_hash)
|
74
|
-
end
|
75
|
-
|
76
|
-
# Validates a document (Hash table) against the schema
|
77
|
-
#
|
78
|
-
# @return [Boolean]
|
79
|
-
def validate_document!(document)
|
80
|
-
::JSON::Validator.validate!(schema, document)
|
81
|
-
end
|
82
|
-
|
83
|
-
protected
|
84
|
-
|
85
|
-
# @!attribute [w] unmanaged_settings
|
86
|
-
# Sets the list of unmanaged settings. For subclass use only
|
87
|
-
#
|
88
|
-
# @param unmanaged_settings [Hash<String, Object]] A hashtable of all unmanaged settings which will be persisted, but not visible
|
89
|
-
# @protected
|
90
|
-
attr_writer :unmanaged_settings
|
91
|
-
|
92
|
-
private
|
93
|
-
|
94
|
-
# Override the create_setting method to always fail. This is called
|
95
|
-
# to dyanmically add settings. However as we're using a schema, no
|
96
|
-
# new settings can be created
|
97
|
-
#
|
98
|
-
# @see PDK::Config::Namespace.create_missing_setting
|
99
|
-
#
|
100
|
-
# @private
|
101
|
-
def create_missing_setting(key, _initial_value = nil)
|
102
|
-
raise ArgumentError, _("Setting '#{key}' does not exist'")
|
103
|
-
end
|
104
|
-
|
105
|
-
# Create a valid, but empty schema
|
106
|
-
#
|
107
|
-
# @return [JSON::Schema]
|
108
|
-
def create_empty_schema
|
109
|
-
::JSON::Schema.new({}, 'http://json-schema.org/draft-06/schema#')
|
110
|
-
end
|
111
|
-
|
112
|
-
# Lazily retrieve the JSON schema from disk for this namespace
|
113
|
-
#
|
114
|
-
# @return [JSON::Schema]
|
115
|
-
def document_schema
|
116
|
-
return @document_schema unless @document_schema.nil?
|
117
|
-
|
118
|
-
# Create an empty schema by default.
|
119
|
-
@document_schema = create_empty_schema
|
120
|
-
|
121
|
-
return @document_schema if @schema_file.nil?
|
122
|
-
unless PDK::Util::Filesystem.file?(@schema_file)
|
123
|
-
raise PDK::Config::LoadError, _('Unable to open %{file} for reading. File does not exist') % {
|
124
|
-
file: @schema_file,
|
125
|
-
}
|
126
|
-
end
|
127
|
-
|
128
|
-
# The schema should not query external URI references, except for the meta-schema. Local files are allowed
|
129
|
-
schema_reader = ::JSON::Schema::Reader.new(
|
130
|
-
accept_file: true,
|
131
|
-
accept_uri: proc { |uri| uri.host.nil? || ['json-schema.org'].include?(uri.host) },
|
132
|
-
)
|
133
|
-
@document_schema = schema_reader.read(Addressable::URI.convert_path(@schema_file))
|
134
|
-
rescue ::JSON::Schema::JsonParseError => e
|
135
|
-
raise PDK::Config::LoadError, _('Unable to open %{file} for reading. JSON Error: %{msg}') % {
|
136
|
-
file: @schema_file,
|
137
|
-
msg: e.message,
|
138
|
-
}
|
139
|
-
end
|
140
|
-
end
|
141
|
-
end
|
142
|
-
end
|
1
|
+
require 'pdk'
|
2
|
+
|
3
|
+
# Due to https://github.com/ruby-json-schema/json-schema/issues/439
|
4
|
+
# Windows file paths "appear" as uri's with no host and a schema of drive letter
|
5
|
+
# Also it is not possible to craft a URI with a Windows path due to the URI object
|
6
|
+
# always prepending the path with forward slash (`/`) so Windows paths end up looking
|
7
|
+
# like '/C:\schema.json', which can not be read.
|
8
|
+
# Instead we need to monkey patch the Schema Reader reader to remove the errant forward slash
|
9
|
+
require 'json-schema/schema/reader'
|
10
|
+
module JSON
|
11
|
+
class Schema
|
12
|
+
class Reader
|
13
|
+
alias original_read_file read_file
|
14
|
+
|
15
|
+
def read_file(pathname)
|
16
|
+
new_pathname = JSON::Util::URI.unescaped_path(pathname.to_s)
|
17
|
+
# Munge the path if it looks like a Windows path e.g. /C:/Windows ...
|
18
|
+
# Note that UNC style paths do not have the same issue (\\host\path)
|
19
|
+
new_pathname.slice!(0) if new_pathname.start_with?('/') && new_pathname[2] == ':'
|
20
|
+
original_read_file(Pathname.new(new_pathname))
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
require 'json-schema'
|
27
|
+
|
28
|
+
module PDK
|
29
|
+
class Config
|
30
|
+
class JSONSchemaNamespace < Namespace
|
31
|
+
# Initialises the PDK::Config::JSONSchemaNamespace object.
|
32
|
+
#
|
33
|
+
# @see PDK::Config::Namespace.initialize
|
34
|
+
#
|
35
|
+
# @option params [String] :schema_file Path to the JSON Schema document
|
36
|
+
def initialize(name = nil, file: nil, parent: nil, persistent_defaults: false, schema_file: nil, &block)
|
37
|
+
super(name, file: file, parent: parent, persistent_defaults: persistent_defaults, &block)
|
38
|
+
@schema_file = schema_file
|
39
|
+
@unmanaged_settings = {}
|
40
|
+
end
|
41
|
+
|
42
|
+
# The JSON Schema for the namespace
|
43
|
+
#
|
44
|
+
# @return [Hash]
|
45
|
+
def schema
|
46
|
+
document_schema.schema
|
47
|
+
end
|
48
|
+
|
49
|
+
# Whether the schema is valid but empty.
|
50
|
+
#
|
51
|
+
# @return [Boolean]
|
52
|
+
def empty_schema?
|
53
|
+
document_schema.schema.empty?
|
54
|
+
end
|
55
|
+
|
56
|
+
# Name of all the top level properties for the schema
|
57
|
+
#
|
58
|
+
# @return [String[]]
|
59
|
+
def schema_property_names
|
60
|
+
return [] if schema['properties'].nil?
|
61
|
+
schema['properties'].keys
|
62
|
+
end
|
63
|
+
|
64
|
+
# Extends the to_h namespace method to include unmanaged settings
|
65
|
+
#
|
66
|
+
# @see PDK::Config::Namespace.to_h
|
67
|
+
def to_h
|
68
|
+
# This may seem counter-intuitive but we need to call super first as the settings
|
69
|
+
# may not have been loaded yet, which means @unmanaged_settings will be empty.
|
70
|
+
# We call super first to force any file loading and then merge the unmanaged settings
|
71
|
+
settings_hash = super
|
72
|
+
@unmanaged_settings = {} if @unmanaged_settings.nil?
|
73
|
+
@unmanaged_settings.merge(settings_hash)
|
74
|
+
end
|
75
|
+
|
76
|
+
# Validates a document (Hash table) against the schema
|
77
|
+
#
|
78
|
+
# @return [Boolean]
|
79
|
+
def validate_document!(document)
|
80
|
+
::JSON::Validator.validate!(schema, document)
|
81
|
+
end
|
82
|
+
|
83
|
+
protected
|
84
|
+
|
85
|
+
# @!attribute [w] unmanaged_settings
|
86
|
+
# Sets the list of unmanaged settings. For subclass use only
|
87
|
+
#
|
88
|
+
# @param unmanaged_settings [Hash<String, Object]] A hashtable of all unmanaged settings which will be persisted, but not visible
|
89
|
+
# @protected
|
90
|
+
attr_writer :unmanaged_settings
|
91
|
+
|
92
|
+
private
|
93
|
+
|
94
|
+
# Override the create_setting method to always fail. This is called
|
95
|
+
# to dyanmically add settings. However as we're using a schema, no
|
96
|
+
# new settings can be created
|
97
|
+
#
|
98
|
+
# @see PDK::Config::Namespace.create_missing_setting
|
99
|
+
#
|
100
|
+
# @private
|
101
|
+
def create_missing_setting(key, _initial_value = nil)
|
102
|
+
raise ArgumentError, _("Setting '#{key}' does not exist'")
|
103
|
+
end
|
104
|
+
|
105
|
+
# Create a valid, but empty schema
|
106
|
+
#
|
107
|
+
# @return [JSON::Schema]
|
108
|
+
def create_empty_schema
|
109
|
+
::JSON::Schema.new({}, 'http://json-schema.org/draft-06/schema#')
|
110
|
+
end
|
111
|
+
|
112
|
+
# Lazily retrieve the JSON schema from disk for this namespace
|
113
|
+
#
|
114
|
+
# @return [JSON::Schema]
|
115
|
+
def document_schema
|
116
|
+
return @document_schema unless @document_schema.nil?
|
117
|
+
|
118
|
+
# Create an empty schema by default.
|
119
|
+
@document_schema = create_empty_schema
|
120
|
+
|
121
|
+
return @document_schema if @schema_file.nil?
|
122
|
+
unless PDK::Util::Filesystem.file?(@schema_file)
|
123
|
+
raise PDK::Config::LoadError, _('Unable to open %{file} for reading. File does not exist') % {
|
124
|
+
file: @schema_file,
|
125
|
+
}
|
126
|
+
end
|
127
|
+
|
128
|
+
# The schema should not query external URI references, except for the meta-schema. Local files are allowed
|
129
|
+
schema_reader = ::JSON::Schema::Reader.new(
|
130
|
+
accept_file: true,
|
131
|
+
accept_uri: proc { |uri| uri.host.nil? || ['json-schema.org'].include?(uri.host) },
|
132
|
+
)
|
133
|
+
@document_schema = schema_reader.read(Addressable::URI.convert_path(@schema_file))
|
134
|
+
rescue ::JSON::Schema::JsonParseError => e
|
135
|
+
raise PDK::Config::LoadError, _('Unable to open %{file} for reading. JSON Error: %{msg}') % {
|
136
|
+
file: @schema_file,
|
137
|
+
msg: e.message,
|
138
|
+
}
|
139
|
+
end
|
140
|
+
end
|
141
|
+
end
|
142
|
+
end
|
@@ -1,53 +1,53 @@
|
|
1
|
-
require 'pdk'
|
2
|
-
|
3
|
-
module PDK
|
4
|
-
class Config
|
5
|
-
class JSONSchemaSetting < PDK::Config::Setting
|
6
|
-
# Initialises the PDK::Config::JSONSchemaSetting object.
|
7
|
-
#
|
8
|
-
# @see PDK::Config::Setting.initialize
|
9
|
-
def initialize(_name, namespace, _initial_value)
|
10
|
-
raise 'The JSONSchemaSetting object can only be created within the JSONSchemaNamespace' unless namespace.is_a?(PDK::Config::JSONSchemaNamespace)
|
11
|
-
super
|
12
|
-
end
|
13
|
-
|
14
|
-
# Verifies that the new setting value is valid by calling the JSON schema validator on
|
15
|
-
# a hash which includes the new setting
|
16
|
-
#
|
17
|
-
# @see PDK::Config::Setting.validate!
|
18
|
-
def validate!(value)
|
19
|
-
# Get the existing namespace data
|
20
|
-
new_document = namespace.to_h
|
21
|
-
# ... set the new value
|
22
|
-
new_document[@name] = value
|
23
|
-
begin
|
24
|
-
# ... add validate it
|
25
|
-
namespace.validate_document!(new_document)
|
26
|
-
rescue ::JSON::Schema::ValidationError => e
|
27
|
-
raise ArgumentError, _('%{key} %{message}') % {
|
28
|
-
key: qualified_name,
|
29
|
-
message: e.message,
|
30
|
-
}
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
# Evaluate the default setting, firstly from the JSON schema and then
|
35
|
-
# from any other default evaluators in the settings chain.
|
36
|
-
#
|
37
|
-
# @see PDK::Config::Setting.default
|
38
|
-
#
|
39
|
-
# @return [Object, nil] the result of evaluating the block given to
|
40
|
-
# {#default_to}, or `nil` if the setting has no default.
|
41
|
-
def default
|
42
|
-
# Return the default from the schema document if it exists
|
43
|
-
if namespace.schema_property_names.include?(@name)
|
44
|
-
prop_schema = namespace.schema['properties'][@name]
|
45
|
-
return prop_schema['default'] unless prop_schema['default'].nil?
|
46
|
-
end
|
47
|
-
# ... otherwise call the settings chain default
|
48
|
-
# and if that doesn't exist, just return nil
|
49
|
-
@previous_setting.nil? ? nil : @previous_setting.default
|
50
|
-
end
|
51
|
-
end
|
52
|
-
end
|
53
|
-
end
|
1
|
+
require 'pdk'
|
2
|
+
|
3
|
+
module PDK
|
4
|
+
class Config
|
5
|
+
class JSONSchemaSetting < PDK::Config::Setting
|
6
|
+
# Initialises the PDK::Config::JSONSchemaSetting object.
|
7
|
+
#
|
8
|
+
# @see PDK::Config::Setting.initialize
|
9
|
+
def initialize(_name, namespace, _initial_value)
|
10
|
+
raise 'The JSONSchemaSetting object can only be created within the JSONSchemaNamespace' unless namespace.is_a?(PDK::Config::JSONSchemaNamespace)
|
11
|
+
super
|
12
|
+
end
|
13
|
+
|
14
|
+
# Verifies that the new setting value is valid by calling the JSON schema validator on
|
15
|
+
# a hash which includes the new setting
|
16
|
+
#
|
17
|
+
# @see PDK::Config::Setting.validate!
|
18
|
+
def validate!(value)
|
19
|
+
# Get the existing namespace data
|
20
|
+
new_document = namespace.to_h
|
21
|
+
# ... set the new value
|
22
|
+
new_document[@name] = value
|
23
|
+
begin
|
24
|
+
# ... add validate it
|
25
|
+
namespace.validate_document!(new_document)
|
26
|
+
rescue ::JSON::Schema::ValidationError => e
|
27
|
+
raise ArgumentError, _('%{key} %{message}') % {
|
28
|
+
key: qualified_name,
|
29
|
+
message: e.message,
|
30
|
+
}
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
# Evaluate the default setting, firstly from the JSON schema and then
|
35
|
+
# from any other default evaluators in the settings chain.
|
36
|
+
#
|
37
|
+
# @see PDK::Config::Setting.default
|
38
|
+
#
|
39
|
+
# @return [Object, nil] the result of evaluating the block given to
|
40
|
+
# {#default_to}, or `nil` if the setting has no default.
|
41
|
+
def default
|
42
|
+
# Return the default from the schema document if it exists
|
43
|
+
if namespace.schema_property_names.include?(@name)
|
44
|
+
prop_schema = namespace.schema['properties'][@name]
|
45
|
+
return prop_schema['default'] unless prop_schema['default'].nil?
|
46
|
+
end
|
47
|
+
# ... otherwise call the settings chain default
|
48
|
+
# and if that doesn't exist, just return nil
|
49
|
+
@previous_setting.nil? ? nil : @previous_setting.default
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -1,49 +1,49 @@
|
|
1
|
-
require 'pdk'
|
2
|
-
|
3
|
-
module PDK
|
4
|
-
class Config
|
5
|
-
class JSONWithSchema < JSONSchemaNamespace
|
6
|
-
# Parses a JSON document with a schema.
|
7
|
-
#
|
8
|
-
# @see PDK::Config::Namespace.parse_file
|
9
|
-
def parse_file(filename)
|
10
|
-
raise unless block_given?
|
11
|
-
data = load_data(filename)
|
12
|
-
data = '{}' if data.nil? || data.empty?
|
13
|
-
require 'json'
|
14
|
-
|
15
|
-
@raw_data = ::JSON.parse(data)
|
16
|
-
@raw_data = {} if @raw_data.nil?
|
17
|
-
|
18
|
-
begin
|
19
|
-
# Ensure the parsed document is actually valid
|
20
|
-
validate_document!(@raw_data)
|
21
|
-
rescue ::JSON::Schema::ValidationError => e
|
22
|
-
raise PDK::Config::LoadError, _('The configuration file %{filename} is not valid: %{message}') % {
|
23
|
-
filename: filename,
|
24
|
-
message: e.message,
|
25
|
-
}
|
26
|
-
end
|
27
|
-
|
28
|
-
schema_property_names.each do |key|
|
29
|
-
yield key, PDK::Config::JSONSchemaSetting.new(key, self, @raw_data[key])
|
30
|
-
end
|
31
|
-
|
32
|
-
# Remove all of the "known" settings from the schema and
|
33
|
-
# we're left with the settings that we don't manage.
|
34
|
-
self.unmanaged_settings = @raw_data.reject { |k, _| schema_property_names.include?(k) }
|
35
|
-
rescue ::JSON::ParserError => e
|
36
|
-
raise PDK::Config::LoadError, e.message
|
37
|
-
end
|
38
|
-
|
39
|
-
# Serializes object data into a JSON string.
|
40
|
-
#
|
41
|
-
# @see PDK::Config::Namespace.serialize_data
|
42
|
-
def serialize_data(data)
|
43
|
-
require 'json'
|
44
|
-
|
45
|
-
::JSON.pretty_generate(data)
|
46
|
-
end
|
47
|
-
end
|
48
|
-
end
|
49
|
-
end
|
1
|
+
require 'pdk'
|
2
|
+
|
3
|
+
module PDK
|
4
|
+
class Config
|
5
|
+
class JSONWithSchema < JSONSchemaNamespace
|
6
|
+
# Parses a JSON document with a schema.
|
7
|
+
#
|
8
|
+
# @see PDK::Config::Namespace.parse_file
|
9
|
+
def parse_file(filename)
|
10
|
+
raise unless block_given?
|
11
|
+
data = load_data(filename)
|
12
|
+
data = '{}' if data.nil? || data.empty?
|
13
|
+
require 'json'
|
14
|
+
|
15
|
+
@raw_data = ::JSON.parse(data)
|
16
|
+
@raw_data = {} if @raw_data.nil?
|
17
|
+
|
18
|
+
begin
|
19
|
+
# Ensure the parsed document is actually valid
|
20
|
+
validate_document!(@raw_data)
|
21
|
+
rescue ::JSON::Schema::ValidationError => e
|
22
|
+
raise PDK::Config::LoadError, _('The configuration file %{filename} is not valid: %{message}') % {
|
23
|
+
filename: filename,
|
24
|
+
message: e.message,
|
25
|
+
}
|
26
|
+
end
|
27
|
+
|
28
|
+
schema_property_names.each do |key|
|
29
|
+
yield key, PDK::Config::JSONSchemaSetting.new(key, self, @raw_data[key])
|
30
|
+
end
|
31
|
+
|
32
|
+
# Remove all of the "known" settings from the schema and
|
33
|
+
# we're left with the settings that we don't manage.
|
34
|
+
self.unmanaged_settings = @raw_data.reject { |k, _| schema_property_names.include?(k) }
|
35
|
+
rescue ::JSON::ParserError => e
|
36
|
+
raise PDK::Config::LoadError, e.message
|
37
|
+
end
|
38
|
+
|
39
|
+
# Serializes object data into a JSON string.
|
40
|
+
#
|
41
|
+
# @see PDK::Config::Namespace.serialize_data
|
42
|
+
def serialize_data(data)
|
43
|
+
require 'json'
|
44
|
+
|
45
|
+
::JSON.pretty_generate(data)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|