|
| 1 | +# NAME |
| 2 | + |
| 3 | +Config::Processor - Cascading configuration files processor with additional |
| 4 | +features |
| 5 | + |
| 6 | +# SYNOPSIS |
| 7 | + |
| 8 | + use Config::Processor; |
| 9 | + |
| 10 | + my $config_processor = Config::Processor->new( |
| 11 | + dirs => [qw( /etc/myapp /home/username/etc/myapp )] |
| 12 | + ); |
| 13 | + |
| 14 | + my $config = $config_processor->load(qw( dirs.yml db.json metrics/* )); |
| 15 | + |
| 16 | + $config = $config_processor->load( |
| 17 | + qw( dirs.yml db.json redis.yml mongodb.json metrics/* ), |
| 18 | + |
| 19 | + { myapp => { |
| 20 | + db => { |
| 21 | + connectors => { |
| 22 | + stat_master => { |
| 23 | + host => 'localhost', |
| 24 | + port => '4321', |
| 25 | + }, |
| 26 | + }, |
| 27 | + }, |
| 28 | + }, |
| 29 | + }, |
| 30 | + ); |
| 31 | + |
| 32 | +# DESCRIPTION |
| 33 | + |
| 34 | +Config::Processor is the cascading configuration files processor, which |
| 35 | +supports file inclusions, variables interpolation and other manipulations with |
| 36 | +configuration tree. Works with YAML and JSON file formats. File format is |
| 37 | +determined by the extension. Supports following file extensions: `.yml`, |
| 38 | +`.yaml`, `.jsn`, `.json`. |
| 39 | + |
| 40 | +# CONSTRUCTOR |
| 41 | + |
| 42 | +## new( %params ) |
| 43 | + |
| 44 | + my $config_processor = Config::Processor->new( |
| 45 | + dirs => [qw( /etc/myapp /home/username/etc/myapp )], |
| 46 | + export_env => 1, |
| 47 | + ); |
| 48 | + |
| 49 | + $config_processor = Config::Processor->new; |
| 50 | + |
| 51 | + $config_processor = Config::Processor->new( |
| 52 | + dirs => [qw( /etc/myapp /home/username/etc/myapp )], |
| 53 | + interpolate_variables => 0, |
| 54 | + process_directives => 0, |
| 55 | + ); |
| 56 | + |
| 57 | +- dirs => \\@dirs |
| 58 | + |
| 59 | + List of directories, in which configuration processor will search files. If |
| 60 | + the parameter not specified, current directory will be used. |
| 61 | + |
| 62 | +- interpolate\_variables => $boolean |
| 63 | + |
| 64 | + Enables or disables variable interpolation in configurations files. |
| 65 | + Enabled by default. |
| 66 | + |
| 67 | +- process\_directives => $boolean |
| 68 | + |
| 69 | + Enables or disables directive processing in configurations files. |
| 70 | + Enabled by default. |
| 71 | + |
| 72 | +- export\_env => $boolean |
| 73 | + |
| 74 | + Enables or disables environment variables exporting to configuration tree. |
| 75 | + If enabled, environment variables can be accessed by the key `ENV` from the |
| 76 | + configuration tree and can be interpolated into other configuration parameters. |
| 77 | + |
| 78 | + Disabled by default. |
| 79 | + |
| 80 | +# METHODS |
| 81 | + |
| 82 | +## load( @config\_sections ) |
| 83 | + |
| 84 | +Attempts to load all configuration sections and returns reference to resulting |
| 85 | +configuration tree. |
| 86 | + |
| 87 | +Configuration section can be a relative filename, a filename with wildcard |
| 88 | +characters or a hash reference. Filenames with wildcard characters is processed |
| 89 | +by `CORE::glob` function and supports the same syntax. |
| 90 | + |
| 91 | + my $config = $config_processor->load( qw( myapp.yml extras/* ), \%hard_config ); |
| 92 | + |
| 93 | +## interpolate\_variables( \[ $boolean \] ) |
| 94 | + |
| 95 | +Enables or disables variable interpolation in configurations files. |
| 96 | + |
| 97 | +## process\_directives( \[ $boolean \] ) |
| 98 | + |
| 99 | +Enables or disables directive processing in configuration files. |
| 100 | + |
| 101 | +## export\_env( \[ $boolean \] ) |
| 102 | + |
| 103 | +Enables or disables environment variables exporting to configuration tree. |
| 104 | + |
| 105 | +# MERGING RULES |
| 106 | + |
| 107 | +Config::Processor merges all configuration sections in one resulting configuration tree by following rules: |
| 108 | + |
| 109 | + Left value Right value Result value |
| 110 | + |
| 111 | + SCALAR $a SCALAR $b SCALAR $b |
| 112 | + SCALAR $a ARRAY \@b ARRAY \@b |
| 113 | + SCALAR $a HASH \%b HASH \%b |
| 114 | + |
| 115 | + ARRAY \@a SCALAR $b SCALAR $b |
| 116 | + ARRAY \@a ARRAY \@b ARRAY \@b |
| 117 | + ARRAY \@a HASH \%b HASH \%b |
| 118 | + |
| 119 | + HASH \%a SCALAR $b SCALAR $b |
| 120 | + HASH \%a ARRAY \@b ARRAY \@b |
| 121 | + HASH \%a HASH \%b HASH recursive_merge( \%a, \%b ) |
| 122 | + |
| 123 | +For example, we have two configuration files. `db.yml` at the left side: |
| 124 | + |
| 125 | + db: |
| 126 | + connectors: |
| 127 | + stat_writer: |
| 128 | + host: "stat.mydb.com" |
| 129 | + port: "1234" |
| 130 | + dbname: "stat" |
| 131 | + username: "stat_writer" |
| 132 | + password: "stat_writer_pass" |
| 133 | + |
| 134 | +And `db_test.yml` at the right side: |
| 135 | + |
| 136 | + db: |
| 137 | + connectors: |
| 138 | + stat_writer: |
| 139 | + host: "localhost" |
| 140 | + username: "test" |
| 141 | + password: "test_pass" |
| 142 | + |
| 143 | +After merging of two files we will get: |
| 144 | + |
| 145 | + db => { |
| 146 | + connectors => { |
| 147 | + stat_writer => { |
| 148 | + host => "localhost", |
| 149 | + port: => "1234", |
| 150 | + dbname: => "stat", |
| 151 | + username: => "test", |
| 152 | + password: => "test_pass", |
| 153 | + }, |
| 154 | + }, |
| 155 | + }, |
| 156 | + |
| 157 | +# INTERPOLATION |
| 158 | + |
| 159 | +Config::Processor can interpolate variables in string values (if you need alias |
| 160 | +for complex structures see `var` directive). Variable names can be absolute or |
| 161 | +relative. Relative variable names begins with "." (dot). The number of dots |
| 162 | +depends on the nesting level of the current configuration parameter relative to |
| 163 | +referenced configuration parameter. |
| 164 | + |
| 165 | + myapp: |
| 166 | + media_formats: [ "images", "audio", "video" ] |
| 167 | + |
| 168 | + dirs: |
| 169 | + root_dir: "/myapp" |
| 170 | + templates_dir: "${myapp.dirs.root_dir}/templates" |
| 171 | + sessions_dir: "${.root_dir}/sessions" |
| 172 | + media_dirs: |
| 173 | + - "${..root_dir}/media/${myapp.media_formats.0}" |
| 174 | + - "${..root_dir}/media/${myapp.media_formats.1}" |
| 175 | + - "${..root_dir}/media/${myapp.media_formats.2}" |
| 176 | + |
| 177 | +After processing of the file we will get: |
| 178 | + |
| 179 | + myapp => { |
| 180 | + media_formats => [ "images", "audio", "video" ], |
| 181 | + |
| 182 | + dirs => { |
| 183 | + root_dir => "/myapp", |
| 184 | + templates_dir => "/myapp/templates", |
| 185 | + sessions_dir => "/myapp/sessions", |
| 186 | + media_dirs => [ |
| 187 | + "/myapp/media/images", |
| 188 | + "/myapp/media/audio", |
| 189 | + "/myapp/media/video", |
| 190 | + ], |
| 191 | + }, |
| 192 | + }, |
| 193 | + |
| 194 | +To escape variable interpolation add one more "$" symbol before variable. |
| 195 | + |
| 196 | + templates_dir: "$${myapp.dirs.root_dir}/templates" |
| 197 | + |
| 198 | +After processing we will get: |
| 199 | + |
| 200 | + templates_dir => ${myapp.dirs.root_dir}/templates, |
| 201 | + |
| 202 | +# DIRECTIVES |
| 203 | + |
| 204 | +- var: varname |
| 205 | + |
| 206 | + Assigns configuration parameter value to another configuration parameter. |
| 207 | + Variable names in the directive can be absolute or relative. Relative variable |
| 208 | + names begins with "." (dot). The number of dots depends on the nesting level of |
| 209 | + the current configuration parameter relative to referenced configuration |
| 210 | + parameter. |
| 211 | + |
| 212 | + myapp: |
| 213 | + db: |
| 214 | + default_options: |
| 215 | + PrintWarn: 0 |
| 216 | + PrintError: 0 |
| 217 | + RaiseError: 1 |
| 218 | + |
| 219 | + connectors: |
| 220 | + stat_master: |
| 221 | + host: "stat-master.mydb.com" |
| 222 | + port: "1234" |
| 223 | + dbname: "stat" |
| 224 | + username: "stat_writer" |
| 225 | + password: "stat_writer_pass" |
| 226 | + options: { var: myapp.db.default_options } |
| 227 | + |
| 228 | + stat_slave: |
| 229 | + host: "stat-slave.mydb.com" |
| 230 | + port: "1234" |
| 231 | + dbname: "stat" |
| 232 | + username: "stat_reader" |
| 233 | + password: "stat_reader_pass" |
| 234 | + options: { var: ...default_options } |
| 235 | + |
| 236 | +- include: filename |
| 237 | + |
| 238 | + Loads configuration parameters from file or multiple files and assigns it to |
| 239 | + specified configuration parameter. Argument of `include` directive can be |
| 240 | + relative filename or a filename with wildcard characters. If loading multiple |
| 241 | + files, configuration parameters from them will be merged before assignment. |
| 242 | + |
| 243 | + myapp: |
| 244 | + db: |
| 245 | + generic_options: |
| 246 | + PrintWarn: 0 |
| 247 | + PrintError: 0 |
| 248 | + RaiseError: 1 |
| 249 | + |
| 250 | + connectors: { include: db_connectors.yml } |
| 251 | + |
| 252 | + metrics: { include: metrics/* } |
| 253 | + |
| 254 | +- underlay |
| 255 | + |
| 256 | + Merges specified configuration parameters with parameters located at the same |
| 257 | + context. Configuration parameters from the context overrides parameters from |
| 258 | + the directive. `underlay` directive most usefull in combination with `var` |
| 259 | + and `include` directives. |
| 260 | + |
| 261 | + For example, you can use this directive to set default values of parameters. |
| 262 | + |
| 263 | + myapp: |
| 264 | + db: |
| 265 | + connectors: |
| 266 | + default: |
| 267 | + port: "1234" |
| 268 | + dbname: "stat" |
| 269 | + options: |
| 270 | + PrintWarn: 0 |
| 271 | + PrintError: 0 |
| 272 | + RaiseError: 1 |
| 273 | + |
| 274 | + stat_master: |
| 275 | + underlay: { var: .default } |
| 276 | + host: "stat-master.mydb.com" |
| 277 | + username: "stat_writer" |
| 278 | + password: "stat_writer_pass" |
| 279 | + |
| 280 | + stat_slave: |
| 281 | + underlay: { var: .default } |
| 282 | + host: "stat-slave.mydb.com" |
| 283 | + username: "stat_reader" |
| 284 | + password: "stat_reader_pass" |
| 285 | + |
| 286 | + You can move default parameters in separate files. |
| 287 | + |
| 288 | + myapp: |
| 289 | + db: |
| 290 | + connectors: |
| 291 | + underlay: |
| 292 | + - { include: db_connectors/default.yml } |
| 293 | + - { include: db_connectors/default_test.yml } |
| 294 | + |
| 295 | + stat_master: |
| 296 | + underlay: { var: .default } |
| 297 | + host: "stat-master.mydb.com" |
| 298 | + username: "stat_writer" |
| 299 | + password: "stat_writer_pass" |
| 300 | + |
| 301 | + stat_slave: |
| 302 | + underlay: { var: .default } |
| 303 | + host: "stat-slave.mydb.com" |
| 304 | + username: "stat_reader" |
| 305 | + password: "stat_reader_pass" |
| 306 | + |
| 307 | + test: |
| 308 | + underlay: { var: .default_test } |
| 309 | + username: "test" |
| 310 | + password: "test_pass" |
| 311 | + |
| 312 | +- overlay |
| 313 | + |
| 314 | + Merges specified configuration parameters with parameters located at the same |
| 315 | + context. Configuration parameters from the directive overrides parameters from |
| 316 | + the context. `overlay` directive most usefull in combination with `var` and |
| 317 | + `include` directives. |
| 318 | + |
| 319 | + For example, you can use `overlay` directive to temporaly overriding regular |
| 320 | + configuration parameters. |
| 321 | + |
| 322 | + myapp: |
| 323 | + db: |
| 324 | + connectors: |
| 325 | + default: |
| 326 | + port: "1234" |
| 327 | + dbname: "stat" |
| 328 | + options: |
| 329 | + PrintWarn: 0 |
| 330 | + PrintError: 0 |
| 331 | + RaiseError: 1 |
| 332 | + |
| 333 | + test: |
| 334 | + host: "localhost" |
| 335 | + port: "4321" |
| 336 | + |
| 337 | + stat_master: |
| 338 | + underlay: { var: .default } |
| 339 | + host: "stat-master.mydb.com" |
| 340 | + username: "stat_writer" |
| 341 | + password: "stat_writer_pass" |
| 342 | + overlay: { var: .test } |
| 343 | + |
| 344 | + stat_slave: |
| 345 | + underlay: { var: .default } |
| 346 | + host: "stat-slave.mydb.com" |
| 347 | + username: "stat_reader" |
| 348 | + password: "stat_reader_pass" |
| 349 | + overlay: { var: .test } |
| 350 | + |
| 351 | + To disable overriding just assign to `test` connector empty hash. |
| 352 | + |
| 353 | + test: {} |
| 354 | + |
| 355 | +# AUTHOR |
| 356 | + |
| 357 | +Eugene Ponizovsky, <[email protected]> |
| 358 | + |
| 359 | +# COPYRIGHT AND LICENSE |
| 360 | + |
| 361 | +Copyright (c) 2016-2017, Eugene Ponizovsky, <[email protected]>. |
| 362 | +All rights reserved. |
| 363 | + |
| 364 | +This module is free software; you can redistribute it and/or modify it under |
| 365 | +the same terms as Perl itself. |
0 commit comments