| 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 |
|
|
|
105 |
## Requirements
|
|
|
106 |
|
|
|
107 |
- **Perl** 5.x or higher
|
|
|
108 |
- **File::Slurp** - File reading utilities
|
|
|
109 |
- **Data::Dumper** - Data structure serialization
|
|
|
110 |
- **Getopt::Long** - Command-line option parsing
|
|
|
111 |
|
|
|
112 |
## License
|
|
|
113 |
|
|
|
114 |
Simplified BSD License (FreeBSD License)
|
|
|
115 |
Copyright (c) 2026, Daily Data Inc.
|
|
|
116 |
|
|
|
117 |
## Author
|
|
|
118 |
|
|
|
119 |
R. W. Rodolico <rodo@dailydata.net>
|
|
|
120 |
|
|
|
121 |
## Version
|
|
|
122 |
|
|
|
123 |
1.1.0 (January 2026)
|
|
|
124 |
|
|
|
125 |
### Version History
|
|
|
126 |
|
|
|
127 |
- **1.1.0** (2026-01-15): Added ability to delete keys from hashes and elements from arrays
|
|
|
128 |
- **1.0** (2026-01-13): Initial release with merge, edit, and format conversion capabilities
|