1MooX::ConfigFromFile::RUosleer::CSoonrttrMeiodboBuXyt:Fe:idCloePnnefarimlgeF(Dr3oo)cmuFmielnet:a:tRioolne::SortedByFilename(3)
2
3
4
6 MooX::ConfigFromFile::Role::SortedByFilename - allows filename based
7 sort algorithm for multiple config files
8
10 package MyApp::Cmd::TPau;
11
12 use DBI;
13 use Moo;
14 use MooX::Cmd with_configfromfile => 1;
15
16 with "MooX::ConfigFromFile::Role::SortedByFilename";
17 with "MooX::ConfigFromFile::Role::HashMergeLoaded";
18
19 # ensure hashes are merged by top-most scalar wins
20 around _build_config_merge_behavior => sub { 'RIGHT_PRECEDENT' };
21
22 has csv => (is => "ro", required => 1);
23
24 sub execute
25 {
26 my $self = shift;
27 DBI->connect("DBI::csv:", undef, undef, $self->csv);
28 }
29
30 __END__
31 $ cat etc/myapp.json
32 {
33 "csv": {
34 "f_ext": ".csv/r",
35 "f_dir": "data"
36 "csv_sep_char": ";",
37 "csv_class": "Text::CSV_XS"
38 }
39 }
40 $cat etc/myapp-tpau.json
41 {
42 "csv": {
43 "f_dir": "data/tpau"
44 }
45 }
46
48 This is an additional role for MooX::ConfigFromFile to allow merging
49 loaded configurations in a more predictable order: filename > extension
50 > path. (Read: When the filename is identical, the extensions are
51 compared, when they are identical, the locations are compared).
52
53 While filename and file extension are compared on character basis, the
54 sort order of the file locations (path) is based on precedence in
55 MooX::File::ConfigDir or File::ConfigDir, respectively. This order is
56 defined internally in @File::ConfigDir::extensible_bases.
57
59 sorted_loaded_config
60 This role modifies the builder for sorted_loaded_config by sorting the
61 loaded files from raw_loaded_config by filename, extension and location
62 in the filesystem, respectively.
63
64 Let's assume the affected setup has a CLI interface named "oper" with
65 sub commands like "git" provides them, too. And the company using the
66 application does it well by defining staging environments like "DEV"
67 (Development), "TEST" (Testing), "INT" (Integration) and "PROD"
68 (Production). For the example, the sub commands shall be "deploy" and
69 "report".
70
71 This will give you possible "config_prefix_map"s of
72
73 # main command
74 ['oper']
75 ['oper', 'dev']
76 ['oper', 'test']
77 ['oper', 'int']
78 ['oper', 'prod']
79 # deploy sub-command
80 ['oper', 'deploy']
81 ['oper', 'deploy', 'dev']
82 ['oper', 'deploy', 'test']
83 ['oper', 'deploy', 'int']
84 ['oper', 'deploy', 'prod']
85 # report sub-command
86 ['oper', 'report']
87 ['oper', 'report', 'dev']
88 ['oper', 'report', 'test']
89 ['oper', 'report', 'int']
90 ['oper', 'report', 'prod']
91
92 This will result in (let's further assume, developers prefer "JSON",
93 operators prefer "YAML") following possible config filenames:
94
95 [
96 # main command
97 'oper.json',
98 'oper.yaml',
99 'oper-dev.json',
100 'oper-dev.yaml',
101 'oper-test.json',
102 'oper-test.yaml',
103 'oper-int.json',
104 'oper-int.yaml',
105 'oper-prod.json',
106 'oper-prod.yaml',
107 # deploy sub-command
108 'oper-deploy.json',
109 'oper-deploy.yaml',
110 'oper-deploy-dev.json',
111 'oper-deploy-dev.yaml',
112 'oper-deploy-test.json',
113 'oper-deploy-test.yaml',
114 'oper-deploy-int.json',
115 'oper-deploy-int.yaml',
116 'oper-deploy-prod.json',
117 'oper-deploy-prod.yaml',
118 # report sub-command
119 'oper-report.json',
120 'oper-report.yaml',
121 'oper-report-dev.json',
122 'oper-report-dev.yaml',
123 'oper-report-test.json',
124 'oper-report-test.yaml',
125 'oper-report-int.json',
126 'oper-report-int.yaml',
127 'oper-report-prod.json',
128 'oper-report-prod.yaml',
129 ]
130
131 For a particular invoking ("oper report" in "int" stage) following
132 files exists:
133
134 [
135 '/etc/oper.json', # global configuration by developers
136 '/etc/oper.yaml', # global configuration by operating policy
137 '/opt/ctrl/etc/oper.json', # vendor configuration by developers
138 '/opt/ctrl/etc/oper.yaml', # vendor configuration by operating policy
139 '/opt/ctrl/etc/oper-int.yaml', # vendor configuration by stage operating policy
140 '/opt/ctrl/etc/oper-report.yaml', # vendor configuration by report operating team
141 '/home/usr4711/oper-report.yaml', # usr4711 individual configuration (e.g. for template adoption)
142 ]
143
144 The default sort algorithm will deliver
145
146 [
147 "/etc/oper.json",
148 "/etc/oper.yaml",
149 "/home/usr4711/oper-report.yaml",
150 "/opt/ctrl/etc/oper-int.yaml",
151 "/opt/ctrl/etc/oper-report.yaml",
152 "/opt/ctrl/etc/oper.json",
153 "/opt/ctrl/etc/oper.yaml"
154 ]
155
156 This role will change the sort algorithm to deliver
157
158 [
159 "/etc/oper.json",
160 "/opt/ctrl/etc/oper.json",
161 "/etc/oper.yaml",
162 "/opt/ctrl/etc/oper.yaml",
163 "/opt/ctrl/etc/oper-int.yaml",
164 "/opt/ctrl/etc/oper-report.yaml",
165 "/home/usr4711/oper-report.yaml"
166 ]
167
168 Which will cause that all policy configuration will override the
169 developer defaults and user configuration override policy settings.
170
172 Jens Rehsack, "<rehsack at cpan.org>"
173
176 Copyright 2018 Jens Rehsack.
177
178 This program is free software; you can redistribute it and/or modify it
179 under the terms of either: the GNU General Public License as published
180 by the Free Software Foundation; or the Artistic License.
181
182 See <http://dev.perl.org/licenses/> for more information.
183
184
185
186perl v5.30.1 Moo2X0:2:0C-o0n1f-i3g0FromFile::Role::SortedByFilename(3)