Rev 2 | Rev 4 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
# perlConfigFileUtility
A Perl utility for managing configuration files with template merging, interactive editing, and format conversion capabilities.
## Overview
`perlConfigFileUtility` is designed to simplify configuration file management by:
- Merging template structures with existing configuration files
- Providing an interactive hierarchical editor for modifying configurations
- Converting between YAML and JSON formats
- Automatically backing up config files when saving changes
## Background
This utility solves two primary problems:
1. **User-Friendly Editing**: Enables users unfamiliar with JSON/YAML syntax to safely edit configuration files through an interactive interface.
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.
### Implementation Approach
- Default configuration is stored as a Perl data structure in a separate file (loaded via `do` statement in project script)
- This template can be shared across multiple applications
- The utility loads existing YAML/JSON configs, merges missing values from the template, and presents an interactive editor
- Format conversion (YAML ↔ JSON) was added as a natural extension of the merge functionality
## Features
- **Template Merging**: Combine Perl data structure templates with existing YAML/JSON config files
- **Interactive Editor**: Navigate and edit nested hashes and arrays with a menu-driven interface
- **Dynamic Modifications**: Add new keys to hashes and elements to arrays on the fly
- **Delete Operations**: Remove keys from hashes and elements from arrays, including all nested children
- **Format Conversion**: Seamlessly convert between YAML and JSON configuration formats
- **Safe Updates**: Automatic backup creation before saving changes
## Usage
```bash
# Basic usage
perlConfigFileUtility [-t <template_file>] [-c <config_file>] [-o <output_file>] [-e]
# Positional arguments (backward compatibility)
perlConfigFileUtility <template_file> [config_file]
```
## Example of template file
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).
```perl
# Default configuration structure for sneakernet
# This file is loaded by the sneakernet script to provide default configuration values
# Variables from $programDefinition hashref (scriptDirectory, scriptFullPath) will be interpolated at runtime
{
'dryrun' => 0, # if set to 1, will not perform any changes, just log what would be done
'verbosity' => 1, # verbosity level for logging
'debug' => 0, # set to number greater than 0 for debugging program
'status_file' => "<scriptDirectory>/sneakernet_target.status", # file created on source server to track last copied dataset
'log_file' => "<scriptFullPath>.log",
'displayLogsOnTTY' => '', # if set to path of a tty, log messages will be sent there also. Example: /dev/ttyv1
'displayLogsOnConsole' => 1, # If set to non-zero, will send log messages to screen in addition to log file
'source' => { # information about source server
'hostname' => '', # used to see if we are on source
'poolname' => 'pool', # name of the ZFS default parent pool to export
'cleanUpScriptsDir' => '<scriptDirectory>/cleanupScripts', # location on disk where scripts to be sent to target are located
'report' => { # if set, will generate a report via email or by storing on a drive
'email' => '', # if set, will email the report to this address
'subject' => '', # subject of the report email (will be auto-generated if empty)
'targetDrive' => { # if set, will store the report on this drive
'label' => '', # the GPT or msdos label of the report drive, REQUIRED
'fstype' => '', # filesystem type of the report drive, default is msdos
'check_interval' => 15, # how often to check for the disk (seconds), message displayed every interval
'wait_timeout' => 300, # time to wait for the disk to appear in seconds
'mount_point' => '', # where to mount the report drive, default is /mnt/label
}
}
},
# ... additional sections: 'target', 'transport', 'datasets' (see full file)
}
```
### How it works
The template file serves dual purposes:
1. **Default Values**: The Perl script loads it at runtime using `$config = do 'scriptname.datastructure'`, which returns the hash reference with all defaults.
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`).
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.
## Options
| Option | Description |
|--------|-------------|
| `-t, --template <file>` | Template file (Perl hashref) |
| `-c, --config <file>` | Config file (YAML or JSON) |
| `-o, --output <file>` | Output file (default: STDOUT) |
| `-e, --edit` | Interactive edit mode |
| `-v, --version` | Show version information |
| `-h, --help` | Show help message |
## Requirements
- **Perl** 5.x or higher
- **File::Slurp** - File reading utilities
- **Data::Dumper** - Data structure serialization
- **Getopt::Long** - Command-line option parsing
## License
Simplified BSD License (FreeBSD License)
Copyright (c) 2026, Daily Data Inc.
## Author
R. W. Rodolico <rodo@dailydata.net>
## Version
1.1.0 (January 2026)
### Version History
- **1.1.0** (2026-01-15): Added ability to delete keys from hashes and elements from arrays
- **1.0** (2026-01-13): Initial release with merge, edit, and format conversion capabilities