From 5c43e7035a2adfafa0c8f3a72dd46913ce0cb771 Mon Sep 17 00:00:00 2001 From: David Steele Date: Tue, 26 May 2015 11:36:03 -0400 Subject: [PATCH] Implement issue #79: Support comment characters in config file --- README.md | 2 ++ doc/doc.xml | 3 +++ lib/BackRest/Utility.pm | 22 +++++++++++++++++-- test/lib/BackRestTest/CommonTest.pm | 34 +++++++++++++---------------- 4 files changed, 40 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index d3dec0a43..17a90c858 100644 --- a/README.md +++ b/README.md @@ -739,6 +739,8 @@ example: db-path=/data/db * Fixed an issue where archive-copy would fail on an incr/diff backup when hardlink=n. In this case the pg_xlog path does not already exist and must be created. Reported by Michael Renner +* Allow comments/disabling in pg_backrest.conf using #. Suggested by Michael Renner. + * Replaced JSON module with JSON::PP which ships with core Perl. ### v0.65: Improved resume and restore logging, compact restores diff --git a/doc/doc.xml b/doc/doc.xml index e6ac12ef8..3647d1e20 100644 --- a/doc/doc.xml +++ b/doc/doc.xml @@ -692,6 +692,9 @@ Run a full backup on the db stanza. --type can Fixed an issue where archive-copy would fail on an incr/diff backup when hardlink=n. In this case the pg_xlog path does not already exist and must be created. Reported by Michael Renner + + Allow comments/disabling in pg_backrest.conf using #. Suggested by Michael Renner. + Replaced JSON module with JSON::PP which ships with core Perl. diff --git a/lib/BackRest/Utility.pm b/lib/BackRest/Utility.pm index 06e23b0bb..437b600f4 100644 --- a/lib/BackRest/Utility.pm +++ b/lib/BackRest/Utility.pm @@ -553,7 +553,8 @@ sub ini_load { $strLine = trim($strLine); - if ($strLine ne '') + # Skip lines that are blank or comments + if ($strLine ne '' && $strLine !~ '^#.*') { # Get the section if (index($strLine, '[') == 0) @@ -610,23 +611,40 @@ sub ini_save open($hFile, '>', $strFile) or confess &log(ERROR, "unable to open ${strFile}"); - # Create the JSON object canonical so that fields are alpha ordered and pass unit tests + # Create the JSON object canonical so that fields are alpha ordered to pass unit tests my $oJSON = JSON::PP->new()->canonical(); # Write the INI file foreach my $strSection (sort(keys $oConfig)) { + # Add a linefeed between sections if (!$bFirst) { syswrite($hFile, "\n") or confess "unable to write lf: $!"; } + # Write the section comment if present + if (defined(${$oConfig}{$strSection}{'[comment]'})) + { + syswrite($hFile, "# " . ${$oConfig}{$strSection}{'[comment]'} . "\n") + or confess "unable to comment for section ${strSection}: $!"; + } + + # Write the section syswrite($hFile, "[${strSection}]\n") or confess "unable to write section ${strSection}: $!"; + # Iterate through all keys in the section foreach my $strKey (sort(keys ${$oConfig}{"${strSection}"})) { + # Skip comments + if ($strKey eq '[comment]') + { + next; + } + + # If the value is a hash then convert it to JSON, otherwise store as is my $strValue = ${$oConfig}{"${strSection}"}{"${strKey}"}; if (defined($strValue)) diff --git a/test/lib/BackRestTest/CommonTest.pm b/test/lib/BackRestTest/CommonTest.pm index a4687c8b5..32ec290f9 100755 --- a/test/lib/BackRestTest/CommonTest.pm +++ b/test/lib/BackRestTest/CommonTest.pm @@ -872,6 +872,7 @@ sub BackRestTestCommon_ConfigCreate $oParamHash{'global:command'}{'cmd-remote'} = $strCommonCommandRemote; } + $oParamHash{'global:command'}{'[comment]'} = 'psql command and options'; $oParamHash{'global:command'}{'cmd-psql'} = $strCommonCommandPsql; if (defined($strRemote) && $strRemote eq BACKUP) @@ -885,9 +886,12 @@ sub BackRestTestCommon_ConfigCreate $oParamHash{$strCommonStanza}{'db-user'} = $strCommonUser; } + $oParamHash{'global:log'}{'[comment]'} = 'file and console log settings'; $oParamHash{'global:log'}{'log-level-console'} = 'debug'; $oParamHash{'global:log'}{'log-level-file'} = 'trace'; + $oParamHash{'global:general'}{'[comment]'} = 'general settings for all operations'; + if ($strLocal eq BACKUP) { $oParamHash{'global:general'}{'repo-path'} = $strCommonRepoPath; @@ -898,13 +902,6 @@ sub BackRestTestCommon_ConfigCreate if (defined($strRemote)) { -# $oParamHash{'global:log'}{'log-level-console'} = 'trace'; - - # if ($bArchiveAsync) - # { - # $oParamHash{'global:archive'}{path} = BackRestTestCommon_LocalPathGet(); - # } - $oParamHash{'global:general'}{'repo-remote-path'} = $strCommonRepoPath; } else @@ -914,12 +911,8 @@ sub BackRestTestCommon_ConfigCreate if ($bArchiveAsync) { + $oParamHash{'global:archive'}{'[comment]'} = 'WAL archive settings'; $oParamHash{'global:archive'}{'archive-async'} = 'y'; - # - # if (!$bCompressAsync) - # { - # $oParamHash{'global:archive'}{'compress_async'} = 'n'; - # } } } else @@ -934,8 +927,8 @@ sub BackRestTestCommon_ConfigCreate if (($strLocal eq BACKUP) || ($strLocal eq DB && !defined($strRemote))) { - $oParamHash{'db:command'}{'cmd-psql-option'} = "--port=${iCommonDbPort}"; - $oParamHash{'global:backup'}{'thread-max'} = $iThreadMax; + $oParamHash{"${strCommonStanza}:command"}{'[comment]'} = 'cluster-specific command options'; + $oParamHash{"${strCommonStanza}:command"}{'cmd-psql-option'} = "--port=${iCommonDbPort}"; if (defined($bHardlink) && $bHardlink) { @@ -950,13 +943,16 @@ sub BackRestTestCommon_ConfigCreate $oParamHash{'global:general'}{'compress'} = 'n'; } - # if (defined($bChecksum) && $bChecksum) - # { - # $oParamHash{'global:backup'}{'checksum'} = 'y'; - # } - + # Stanza settings + $oParamHash{$strCommonStanza}{'[comment]'} = "cluster-specific settings"; $oParamHash{$strCommonStanza}{'db-path'} = $strCommonDbCommonPath; + # Comments + if (defined($oParamHash{'global:backup'})) + { + $oParamHash{'global:backup'}{'[comment]'} = "backup settings"; + } + # Write out the configuration file my $strFile = BackRestTestCommon_TestPathGet() . '/pg_backrest.conf'; ini_save($strFile, \%oParamHash);