| 2 |
rodolico |
1 |
# perlConfigFileUtility
|
|
|
2 |
|
|
|
3 |
A Perl utility for managing configuration files with template merging, interactive editing, and format conversion capabilities.
|
|
|
4 |
|
|
|
5 |
## Overview
|
|
|
6 |
|
|
|
7 |
`perlConfigFileUtility` is designed to simplify configuration file management by:
|
|
|
8 |
- Merging template structures with existing configuration files
|
|
|
9 |
- Providing an interactive hierarchical editor for modifying configurations
|
|
|
10 |
- Converting between YAML and JSON formats
|
|
|
11 |
- Automatically backing up config files when saving changes
|
|
|
12 |
|
|
|
13 |
## Background
|
|
|
14 |
|
|
|
15 |
This utility solves two primary problems:
|
|
|
16 |
|
|
|
17 |
1. **User-Friendly Editing**: Enables users unfamiliar with JSON/YAML syntax to safely edit configuration files through an interactive interface.
|
|
|
18 |
|
|
|
19 |
2. **Version Compatibility**: Handles configuration file evolution across software versions. When new key/value pairs are added to the application, the utility merges them with existing config files, ensuring older configurations remain compatible with newer software.
|
|
|
20 |
|
|
|
21 |
### Implementation Approach
|
|
|
22 |
|
|
|
23 |
- Default configuration is stored as a Perl data structure in a separate file (loaded via `do` statement in project script)
|
|
|
24 |
- This template can be shared across multiple applications
|
|
|
25 |
- The utility loads existing YAML/JSON configs, merges missing values from the template, and presents an interactive editor
|
|
|
26 |
- Format conversion (YAML ↔ JSON) was added as a natural extension of the merge functionality
|
|
|
27 |
|
|
|
28 |
## Features
|
|
|
29 |
|
|
|
30 |
- **Template Merging**: Combine Perl data structure templates with existing YAML/JSON config files
|
|
|
31 |
- **Interactive Editor**: Navigate and edit nested hashes and arrays with a menu-driven interface
|
|
|
32 |
- **Dynamic Modifications**: Add new keys to hashes and elements to arrays on the fly
|
|
|
33 |
- **Delete Operations**: Remove keys from hashes and elements from arrays, including all nested children
|
|
|
34 |
- **Format Conversion**: Seamlessly convert between YAML and JSON configuration formats
|
|
|
35 |
- **Safe Updates**: Automatic backup creation before saving changes
|
|
|
36 |
|
|
|
37 |
## Usage
|
|
|
38 |
|
|
|
39 |
```bash
|
|
|
40 |
# Basic usage
|
|
|
41 |
perlConfigFileUtility [-t <template_file>] [-c <config_file>] [-o <output_file>] [-e]
|
|
|
42 |
|
|
|
43 |
# Positional arguments (backward compatibility)
|
|
|
44 |
perlConfigFileUtility <template_file> [config_file]
|
|
|
45 |
```
|
|
|
46 |
|
| 3 |
rodolico |
47 |
## Example of template file
|
|
|
48 |
|
|
|
49 |
The template file is a Perl hash reference that defines the complete configuration structure with default values and inline documentation. Below is a simplified excerpt from the `sneakernet` project's datastructure file (see `../zfs_utils/sneakernet/sneakernet.datastructure` for the full example).
|
|
|
50 |
|
|
|
51 |
```perl
|
|
|
52 |
# Default configuration structure for sneakernet
|
|
|
53 |
# This file is loaded by the sneakernet script to provide default configuration values
|
|
|
54 |
# Variables from $programDefinition hashref (scriptDirectory, scriptFullPath) will be interpolated at runtime
|
|
|
55 |
|
|
|
56 |
{
|
|
|
57 |
'dryrun' => 0, # if set to 1, will not perform any changes, just log what would be done
|
|
|
58 |
'verbosity' => 1, # verbosity level for logging
|
|
|
59 |
'debug' => 0, # set to number greater than 0 for debugging program
|
|
|
60 |
'status_file' => "<scriptDirectory>/sneakernet_target.status", # file created on source server to track last copied dataset
|
|
|
61 |
'log_file' => "<scriptFullPath>.log",
|
|
|
62 |
'displayLogsOnTTY' => '', # if set to path of a tty, log messages will be sent there also. Example: /dev/ttyv1
|
|
|
63 |
'displayLogsOnConsole' => 1, # If set to non-zero, will send log messages to screen in addition to log file
|
|
|
64 |
'source' => { # information about source server
|
|
|
65 |
'hostname' => '', # used to see if we are on source
|
|
|
66 |
'poolname' => 'pool', # name of the ZFS default parent pool to export
|
|
|
67 |
'cleanUpScriptsDir' => '<scriptDirectory>/cleanupScripts', # location on disk where scripts to be sent to target are located
|
|
|
68 |
'report' => { # if set, will generate a report via email or by storing on a drive
|
|
|
69 |
'email' => '', # if set, will email the report to this address
|
|
|
70 |
'subject' => '', # subject of the report email (will be auto-generated if empty)
|
|
|
71 |
'targetDrive' => { # if set, will store the report on this drive
|
|
|
72 |
'label' => '', # the GPT or msdos label of the report drive, REQUIRED
|
|
|
73 |
'fstype' => '', # filesystem type of the report drive, default is msdos
|
|
|
74 |
'check_interval' => 15, # how often to check for the disk (seconds), message displayed every interval
|
|
|
75 |
'wait_timeout' => 300, # time to wait for the disk to appear in seconds
|
|
|
76 |
'mount_point' => '', # where to mount the report drive, default is /mnt/label
|
|
|
77 |
}
|
|
|
78 |
}
|
|
|
79 |
},
|
|
|
80 |
# ... additional sections: 'target', 'transport', 'datasets' (see full file)
|
|
|
81 |
}
|
|
|
82 |
```
|
|
|
83 |
|
|
|
84 |
### How it works
|
|
|
85 |
|
|
|
86 |
The template file serves dual purposes:
|
|
|
87 |
|
|
|
88 |
1. **Default Values**: The Perl script loads it at runtime using `$config = do 'scriptname.datastructure'`, which returns the hash reference with all defaults.
|
|
|
89 |
|
|
|
90 |
2. **Configuration Schema**: The inline comments document each setting's purpose, forming a self-documenting configuration schema that can be exported to YAML format (e.g., `scriptname.conf.yaml`).
|
|
|
91 |
|
|
|
92 |
When the datastructure changes, this tool can non-destructively merge the updated template with existing configuration files, preserving user customizations while adding new keys and documentation. The interactive edit mode (`-e`) provides safe, menu-driven configuration management.
|
|
|
93 |
|
| 2 |
rodolico |
94 |
## Options
|
|
|
95 |
|
|
|
96 |
| Option | Description |
|
|
|
97 |
|--------|-------------|
|
|
|
98 |
| `-t, --template <file>` | Template file (Perl hashref) |
|
|
|
99 |
| `-c, --config <file>` | Config file (YAML or JSON) |
|
|
|
100 |
| `-o, --output <file>` | Output file (default: STDOUT) |
|
|
|
101 |
| `-e, --edit` | Interactive edit mode |
|
|
|
102 |
| `-v, --version` | Show version information |
|
|
|
103 |
| `-h, --help` | Show help message |
|
|
|
104 |
|
| 4 |
rodolico |
105 |
## Examples
|
|
|
106 |
|
|
|
107 |
### Merge Template with Existing Config
|
|
|
108 |
```bash
|
|
|
109 |
perlConfigFileUtility -t template.pl -c config.yaml -o output.yaml
|
|
|
110 |
```
|
|
|
111 |
|
|
|
112 |
### Interactive Editing
|
|
|
113 |
```bash
|
|
|
114 |
perlConfigFileUtility -c config.yaml -e
|
|
|
115 |
```
|
|
|
116 |
Navigate through nested structures, modify values, add new keys/elements, or delete existing ones.
|
|
|
117 |
|
|
|
118 |
### Format Conversion
|
|
|
119 |
```bash
|
|
|
120 |
# YAML to JSON
|
|
|
121 |
perlConfigFileUtility -c config.yaml -o config.json
|
|
|
122 |
|
|
|
123 |
# JSON to YAML
|
|
|
124 |
perlConfigFileUtility -c config.json -o config.yaml
|
|
|
125 |
```
|
|
|
126 |
|
|
|
127 |
### Update Config with New Template Keys
|
|
|
128 |
```bash
|
|
|
129 |
# Merge new template keys into existing config, preserving user values
|
|
|
130 |
perlConfigFileUtility -t myapp.datastructure -c myapp.conf.yaml -o myapp.conf.yaml
|
|
|
131 |
```
|
|
|
132 |
|
|
|
133 |
### Start Fresh from Template
|
|
|
134 |
```bash
|
|
|
135 |
# Export template as YAML config
|
|
|
136 |
perlConfigFileUtility -t myapp.datastructure -o myapp.conf.yaml
|
|
|
137 |
```
|
|
|
138 |
|
|
|
139 |
## Interactive Editor Commands
|
|
|
140 |
|
|
|
141 |
When in edit mode (`-e`), the following commands are available:
|
|
|
142 |
|
|
|
143 |
- **Number (1-N)**: Select and edit the numbered item
|
|
|
144 |
- **0 or Enter**: Go back to previous level
|
|
|
145 |
- **a**: Add new key (in hash) or element (in array)
|
|
|
146 |
- **d**: Delete a key (from hash) or element (from array)
|
|
|
147 |
- **q**: Quit and optionally save changes
|
|
|
148 |
|
|
|
149 |
When editing:
|
|
|
150 |
- **Scalars**: Enter new value or press Enter to keep current
|
|
|
151 |
- **Hashes**: Navigate into nested structure
|
|
|
152 |
- **Arrays**: Navigate into nested structure or edit elements
|
|
|
153 |
|
| 2 |
rodolico |
154 |
## Requirements
|
|
|
155 |
|
| 4 |
rodolico |
156 |
### Perl Modules
|
| 2 |
rodolico |
157 |
|
| 4 |
rodolico |
158 |
**Required:**
|
|
|
159 |
- Perl 5.x or higher
|
|
|
160 |
- `File::Slurp` - File reading utilities
|
|
|
161 |
- `Data::Dumper` - Data structure serialization
|
|
|
162 |
- `Getopt::Long` - Command-line option parsing
|
|
|
163 |
|
|
|
164 |
**At least one YAML library:**
|
|
|
165 |
- `YAML::XS` (recommended, fastest)
|
|
|
166 |
- `YAML::Tiny`
|
|
|
167 |
- `YAML`
|
|
|
168 |
|
|
|
169 |
**At least one JSON library:**
|
|
|
170 |
- `JSON::XS` (recommended, fastest)
|
|
|
171 |
- `JSON::PP`
|
|
|
172 |
- `JSON`
|
|
|
173 |
|
|
|
174 |
### Installation
|
|
|
175 |
|
|
|
176 |
**Using CPAN:**
|
|
|
177 |
```bash
|
|
|
178 |
cpan File::Slurp YAML::XS JSON::XS
|
|
|
179 |
```
|
|
|
180 |
|
|
|
181 |
**Using cpanm:**
|
|
|
182 |
```bash
|
|
|
183 |
cpanm File::Slurp YAML::XS JSON::XS
|
|
|
184 |
```
|
|
|
185 |
|
|
|
186 |
**Debian/Ubuntu:**
|
|
|
187 |
```bash
|
|
|
188 |
sudo apt-get install libfile-slurp-perl libyaml-perl libjson-xs-perl
|
|
|
189 |
```
|
|
|
190 |
|
|
|
191 |
**RedHat/CentOS:**
|
|
|
192 |
```bash
|
|
|
193 |
sudo yum install perl-File-Slurp perl-YAML perl-JSON-XS
|
|
|
194 |
```
|
|
|
195 |
|
|
|
196 |
**FreeBSD:**
|
|
|
197 |
```bash
|
|
|
198 |
sudo pkg install p5-File-Slurp p5-YAML p5-JSON-XS
|
|
|
199 |
```
|
|
|
200 |
|
|
|
201 |
### Source Code
|
|
|
202 |
|
|
|
203 |
This script is part of the Subversion repository and may be downloaded, checked out, or exported from:
|
|
|
204 |
|
|
|
205 |
```bash
|
|
|
206 |
# Checkout the entire repository
|
|
|
207 |
svn checkout http://svn.dailydata.net/svn/perlutils/trunk perlutils
|
|
|
208 |
|
|
|
209 |
# Export without version control metadata
|
|
|
210 |
svn export http://svn.dailydata.net/svn/perlutils/trunk perlutils
|
|
|
211 |
|
|
|
212 |
# Download just this script
|
|
|
213 |
svn export http://svn.dailydata.net/svn/perlutils/trunk/perlConfigFileUtility
|
|
|
214 |
```
|
|
|
215 |
|
|
|
216 |
## Use Cases
|
|
|
217 |
|
|
|
218 |
### 1. User-Friendly Configuration Editing
|
|
|
219 |
|
|
|
220 |
Enable non-technical users to modify complex configuration files without risking syntax errors:
|
|
|
221 |
```bash
|
|
|
222 |
perlConfigFileUtility -c /etc/myapp/config.yaml -e
|
|
|
223 |
```
|
|
|
224 |
|
|
|
225 |
### 2. Configuration Version Management
|
|
|
226 |
|
|
|
227 |
When software is updated with new configuration options, merge them into existing configs:
|
|
|
228 |
```bash
|
|
|
229 |
# v2.0 adds new keys to template
|
|
|
230 |
perlConfigFileUtility -t myapp-v2.datastructure -c myapp-v1.conf.yaml -o myapp-v2.conf.yaml
|
|
|
231 |
```
|
|
|
232 |
User customizations are preserved while new defaults are added.
|
|
|
233 |
|
|
|
234 |
### 3. Configuration Format Migration
|
|
|
235 |
|
|
|
236 |
Convert existing configs between formats:
|
|
|
237 |
```bash
|
|
|
238 |
# Organization switches from JSON to YAML
|
|
|
239 |
for file in configs/*.json; do
|
|
|
240 |
perlConfigFileUtility -c "$file" -o "${file%.json}.yaml"
|
|
|
241 |
done
|
|
|
242 |
```
|
|
|
243 |
|
|
|
244 |
### 4. Configuration Deployment
|
|
|
245 |
|
|
|
246 |
Generate initial configuration files from templates:
|
|
|
247 |
```bash
|
|
|
248 |
perlConfigFileUtility -t defaults.pl -o /etc/newapp/config.yaml
|
|
|
249 |
```
|
|
|
250 |
|
|
|
251 |
## Technical Details
|
|
|
252 |
|
|
|
253 |
### Template Files
|
|
|
254 |
|
|
|
255 |
Template files are Perl data structures (hashrefs) that define the configuration schema:
|
|
|
256 |
|
|
|
257 |
```perl
|
|
|
258 |
{
|
|
|
259 |
'setting1' => 'default_value',
|
|
|
260 |
'section' => {
|
|
|
261 |
'nested_setting' => 123,
|
|
|
262 |
'array_setting' => ['item1', 'item2']
|
|
|
263 |
}
|
|
|
264 |
}
|
|
|
265 |
```
|
|
|
266 |
|
|
|
267 |
Inline comments in templates serve as documentation and are preserved when exported to YAML.
|
|
|
268 |
|
|
|
269 |
### Merge Behavior
|
|
|
270 |
|
|
|
271 |
- **Missing keys**: Added from template
|
|
|
272 |
- **Existing keys**: Preserved from config (user values not overwritten)
|
|
|
273 |
- **Nested hashes**: Recursively merged
|
|
|
274 |
- **Arrays**: Extended if template is longer; existing elements preserved
|
|
|
275 |
- **Type mismatches**: Config value takes precedence
|
|
|
276 |
|
|
|
277 |
### Backup Strategy
|
|
|
278 |
|
|
|
279 |
When saving changes, the original file is renamed with `.bak` extension:
|
|
|
280 |
```
|
|
|
281 |
config.yaml → config.yaml.bak (old version)
|
|
|
282 |
config.yaml → (new version)
|
|
|
283 |
```
|
|
|
284 |
|
|
|
285 |
## Troubleshooting
|
|
|
286 |
|
|
|
287 |
**No YAML/JSON library error:**
|
|
|
288 |
Install at least one YAML and one JSON library using your system package manager or CPAN.
|
|
|
289 |
|
|
|
290 |
**Template must be a hashref error:**
|
|
|
291 |
Ensure template file contains a single hashref structure (starts with `{` and ends with `}`).
|
|
|
292 |
|
|
|
293 |
**Config must be a hashref error:**
|
|
|
294 |
The utility only works with hash-based configurations, not arrays or scalars at the root level.
|
|
|
295 |
|
|
|
296 |
**Permission denied errors:**
|
|
|
297 |
Ensure you have write permissions for the output directory when saving files.
|
|
|
298 |
|
| 2 |
rodolico |
299 |
## License
|
|
|
300 |
|
|
|
301 |
Simplified BSD License (FreeBSD License)
|
|
|
302 |
Copyright (c) 2026, Daily Data Inc.
|
|
|
303 |
|
|
|
304 |
## Author
|
|
|
305 |
|
|
|
306 |
R. W. Rodolico <rodo@dailydata.net>
|
|
|
307 |
|
|
|
308 |
## Version
|
|
|
309 |
|
|
|
310 |
1.1.0 (January 2026)
|
|
|
311 |
|
|
|
312 |
### Version History
|
|
|
313 |
|
|
|
314 |
- **1.1.0** (2026-01-15): Added ability to delete keys from hashes and elements from arrays
|
|
|
315 |
- **1.0** (2026-01-13): Initial release with merge, edit, and format conversion capabilities
|