You've already forked pgbackrest
							
							
				mirror of
				https://github.com/pgbackrest/pgbackrest.git
				synced 2025-10-30 23:37:45 +02:00 
			
		
		
		
	Implement help command in C.
This commit is contained in:
		| @@ -30,12 +30,16 @@ use constant CONFIG_HELP_NAME                                       => 'name'; | ||||
| use constant CONFIG_HELP_OPTION                                     => 'option'; | ||||
|     push @EXPORT, qw(CONFIG_HELP_OPTION); | ||||
| use constant CONFIG_HELP_SECTION                                    => 'section'; | ||||
|     push @EXPORT, qw(CONFIG_HELP_SECTION); | ||||
| use constant CONFIG_HELP_SUMMARY                                    => 'summary'; | ||||
|     push @EXPORT, qw(CONFIG_HELP_SUMMARY); | ||||
|  | ||||
| use constant CONFIG_HELP_SOURCE                                     => 'source'; | ||||
|     push @EXPORT, qw(CONFIG_HELP_SOURCE); | ||||
| use constant CONFIG_HELP_SOURCE_DEFAULT                             => 'default'; | ||||
| use constant CONFIG_HELP_SOURCE_SECTION                             => CONFIG_HELP_SECTION; | ||||
| use constant CONFIG_HELP_SOURCE_COMMAND                             => CONFIG_HELP_COMMAND; | ||||
|     push @EXPORT, qw(CONFIG_HELP_SOURCE_COMMAND); | ||||
|  | ||||
| #################################################################################################################################### | ||||
| # Config Section Types | ||||
| @@ -339,263 +343,6 @@ sub process | ||||
|     logDebugReturn($strOperation); | ||||
| } | ||||
|  | ||||
| #################################################################################################################################### | ||||
| # helpDataWrite | ||||
| # | ||||
| # Write help data into a perl module so it can be accessed by backrest for command-line help. | ||||
| #################################################################################################################################### | ||||
| sub helpDataWrite | ||||
| { | ||||
|     my $self = shift; | ||||
|  | ||||
|     # Assign function parameters, defaults, and log debug info | ||||
|     my | ||||
|     ( | ||||
|         $strOperation, | ||||
|         $oManifest | ||||
|     ) = | ||||
|         logDebugParam | ||||
|         ( | ||||
|             __PACKAGE__ . '->helpDataWrite', \@_, | ||||
|             {name => 'oManifest'} | ||||
|         ); | ||||
|  | ||||
|     # Iterate options | ||||
|     my $oConfigHash = $self->{oConfigHash}; | ||||
|     my $strOptionData; | ||||
|  | ||||
|     foreach my $strOption (sort(keys(%{$$oConfigHash{&CONFIG_HELP_OPTION}}))) | ||||
|     { | ||||
|         my $oOptionHash = $$oConfigHash{&CONFIG_HELP_OPTION}{$strOption}; | ||||
|  | ||||
|         if (defined($strOptionData)) | ||||
|         { | ||||
|             $strOptionData .= ",\n\n"; | ||||
|         } | ||||
|  | ||||
|         # Format the option for output | ||||
|         $strOptionData .= | ||||
|             '        # ' . uc($strOption) . " Option Help\n" . | ||||
|             '        #' . ('-' x 123) . "\n" . | ||||
|             "        '${strOption}' =>\n" . | ||||
|             "        {\n" . | ||||
|             (defined($$oOptionHash{&CONFIG_HELP_SECTION}) ? '            ' . &CONFIG_HELP_SECTION . | ||||
|                 ' => \'' . $$oOptionHash{&CONFIG_HELP_SECTION} . "',\n" : '') . | ||||
|             '            ' . &CONFIG_HELP_SUMMARY . " =>\n" . | ||||
|             helpDataWriteFormatText($oManifest, $self->{oDocRender}, $$oOptionHash{&CONFIG_HELP_SUMMARY}, 16, 112) . ",\n" . | ||||
|             '            ' . &CONFIG_HELP_DESCRIPTION . " =>\n" . | ||||
|             helpDataWriteFormatText($oManifest, $self->{oDocRender}, $$oOptionHash{&CONFIG_HELP_DESCRIPTION}, 16, 112) . "\n" . | ||||
|             "        }"; | ||||
|     } | ||||
|  | ||||
|     # Iterate commands | ||||
|     my $strCommandData; | ||||
|  | ||||
|     foreach my $strCommand (sort(keys(%{$$oConfigHash{&CONFIG_HELP_COMMAND}}))) | ||||
|     { | ||||
|         my $oCommandHash = $$oConfigHash{&CONFIG_HELP_COMMAND}{$strCommand}; | ||||
|  | ||||
|         if (defined($strCommandData)) | ||||
|         { | ||||
|             $strCommandData .= ",\n\n"; | ||||
|         } | ||||
|  | ||||
|         # Format the command for output | ||||
|         $strCommandData .= | ||||
|             '        # ' . uc($strCommand) . " Command Help\n" . | ||||
|             '        #' . ('-' x 123) . "\n" . | ||||
|             "        '${strCommand}' =>\n" . | ||||
|             "        {\n" . | ||||
|             '            ' . &CONFIG_HELP_SUMMARY . " =>\n" . | ||||
|             helpDataWriteFormatText($oManifest, $self->{oDocRender}, $$oCommandHash{&CONFIG_HELP_SUMMARY}, 16, 112) . ",\n" . | ||||
|             '            ' . &CONFIG_HELP_DESCRIPTION . " =>\n" . | ||||
|             helpDataWriteFormatText($oManifest, $self->{oDocRender}, $$oCommandHash{&CONFIG_HELP_DESCRIPTION}, 16, 112) . ",\n" . | ||||
|             "\n"; | ||||
|  | ||||
|         # Iterate options | ||||
|         my $strOptionData; | ||||
|         my $bExtraLinefeed = false; | ||||
|  | ||||
|         if (defined($$oCommandHash{&CONFIG_HELP_OPTION})) | ||||
|         { | ||||
|             $strCommandData .= | ||||
|                 '            ' . CONFIG_HELP_OPTION . " =>\n" . | ||||
|                 "            {\n"; | ||||
|  | ||||
|             foreach my $strOption (sort(keys(%{$$oCommandHash{&CONFIG_HELP_OPTION}}))) | ||||
|             { | ||||
|                 my $oOptionHash = $$oCommandHash{&CONFIG_HELP_OPTION}{$strOption}; | ||||
|  | ||||
|                 if (defined($strOptionData)) | ||||
|                 { | ||||
|                     $strOptionData .= ",\n"; | ||||
|                 } | ||||
|  | ||||
|                 # If option came from the command then output details | ||||
|                 if ($$oOptionHash{&CONFIG_HELP_SOURCE} eq CONFIG_HELP_SOURCE_COMMAND) | ||||
|                 { | ||||
|                     if (defined($strOptionData)) | ||||
|                     { | ||||
|                         $strOptionData .= "\n"; | ||||
|                     } | ||||
|  | ||||
|                     # Format the command option for output | ||||
|                     $strOptionData .= | ||||
|                         '                # ' . uc($strOption) . " Option Help\n" . | ||||
|                         '                #' . ('-' x 115) . "\n" . | ||||
|                         "                '${strOption}' =>\n" . | ||||
|                         "                {\n" . | ||||
|                         '                    ' . &CONFIG_HELP_SUMMARY . " =>\n" . | ||||
|                         helpDataWriteFormatText($oManifest, $self->{oDocRender}, | ||||
|                                                 $$oOptionHash{&CONFIG_HELP_SUMMARY}, 24, 104) . ",\n" . | ||||
|                         '                    ' . &CONFIG_HELP_DESCRIPTION . " =>\n" . | ||||
|                         helpDataWriteFormatText($oManifest, $self->{oDocRender}, | ||||
|                                                 $$oOptionHash{&CONFIG_HELP_DESCRIPTION}, 24, 104) . "\n" . | ||||
|                         "                }"; | ||||
|  | ||||
|                     $bExtraLinefeed = true; | ||||
|                 } | ||||
|                 # Else output a reference to indicate the option is in the global option list | ||||
|                 else | ||||
|                 { | ||||
|                     if ($bExtraLinefeed) | ||||
|                     { | ||||
|                         $strOptionData .= "\n"; | ||||
|                         $bExtraLinefeed = false; | ||||
|                     } | ||||
|  | ||||
|                     $strOptionData .= | ||||
|                         "                '${strOption}' => '" . $$oOptionHash{&CONFIG_HELP_SOURCE} . "'"; | ||||
|                 } | ||||
|             } | ||||
|  | ||||
|             $strCommandData .= | ||||
|                 $strOptionData . "\n" . | ||||
|                 "            }\n"; | ||||
|         } | ||||
|  | ||||
|         $strCommandData .= | ||||
|             "        }"; | ||||
|     } | ||||
|  | ||||
|     # Format the perl module | ||||
|     my $strHelpData = | ||||
|         ('#' x 132) . "\n" . | ||||
|         "# CONFIG HELP DATA MODULE\n" . | ||||
|         "#\n" . | ||||
|         "# This module is automatically generated by doc.pl and should never be manually edited.\n" . | ||||
|         ('#' x 132) . "\n" . | ||||
|         "package pgBackRest::Config::ConfigHelpData;\n" . | ||||
|         "\n" . | ||||
|         "use strict;\n" . | ||||
|         "use warnings FATAL => qw(all);\n" . | ||||
|         "use Carp qw(confess);\n" . | ||||
|         "\n" . | ||||
|         "use Exporter qw(import);\n" . | ||||
|         "    our \@EXPORT = qw();\n" . | ||||
|         "\n" . | ||||
|         ('#' x 132) . "\n" . | ||||
|         "# Data used by the ConfigHelp module to generate command-line help\n" . | ||||
|         ('#' x 132) . "\n" . | ||||
|         "my \$oConfigHelpData =\n". | ||||
|         "{\n" . | ||||
|         "    # Option Help\n" . | ||||
|         '    #' . ('-' x 127) . "\n" . | ||||
|         "    " . CONFIG_HELP_OPTION . " =>\n" . | ||||
|         "    {\n" . | ||||
|         $strOptionData . "\n" . | ||||
|         "    },\n" . | ||||
|         "\n" . | ||||
|         "    # Command Help\n" . | ||||
|         '    #' . ('-' x 127) . "\n" . | ||||
|         "    " . CONFIG_HELP_COMMAND . " =>\n" . | ||||
|         "    {\n" . | ||||
|         $strCommandData . "\n" . | ||||
|         "    }\n" . | ||||
|         "};\n" . | ||||
|         "\n" . | ||||
|         ('#' x 132) . "\n" . | ||||
|         "# configHelpDataGet\n" . | ||||
|         ('#' x 132) . "\n" . | ||||
|         "sub configHelpDataGet\n" . | ||||
|         "{\n" . | ||||
|         "    return \$oConfigHelpData;\n" . | ||||
|         "}\n" . | ||||
|         "\n" . | ||||
|         "push \@EXPORT, qw(configHelpDataGet);\n" . | ||||
|         "\n" . | ||||
|         "1;\n"; | ||||
|  | ||||
|     # Write the perl module into the lib path | ||||
|     $oManifest->storage()->put(dirname(dirname($0)) . '/lib/pgBackRest/Config/ConfigHelpData.pm', $strHelpData); | ||||
|  | ||||
|     # Return from function and log return values if any | ||||
|     logDebugReturn($strOperation); | ||||
| } | ||||
|  | ||||
| # Helper function for helpDataWrite() used to format text by quoting it and splitting lines so it looks good in the module. | ||||
| sub helpDataWriteFormatText | ||||
| { | ||||
|     my $oManifest = shift; | ||||
|     my $oDocRender = shift; | ||||
|     my $oText = shift; | ||||
|     my $iIndent = shift; | ||||
|     my $iLength = shift; | ||||
|  | ||||
|     # Split the string into lines for processing | ||||
|     my @stryText = split("\n", trim($oManifest->variableReplace($oDocRender->processText($oText)))); | ||||
|     my $strText; | ||||
|     my $iIndex = 0; | ||||
|  | ||||
|     foreach my $strLine (@stryText) | ||||
|     { | ||||
|         # Add a linefeed if this is not the first line | ||||
|         if (defined($strText)) | ||||
|         { | ||||
|             $strText .= " .\n"; | ||||
|         } | ||||
|  | ||||
|         # Escape perl special characters | ||||
|         $strLine =~ s/\@/\\@/g; | ||||
|         $strLine =~ s/\$/\\\$/g; | ||||
|         $strLine =~ s/\"/\\"/g; | ||||
|  | ||||
|         my $strPart; | ||||
|         my $bFirst = true; | ||||
|  | ||||
|         # Split the line for output if it's too long | ||||
|         do | ||||
|         { | ||||
|             ($strPart, $strLine) = stringSplit($strLine, ' ', defined($strPart) ? $iLength - 4 : $iLength); | ||||
|  | ||||
|             $strText .= ' ' x $iIndent; | ||||
|  | ||||
|             if (!$bFirst) | ||||
|             { | ||||
|                 $strText .= "    "; | ||||
|             } | ||||
|  | ||||
|             $strText .= "\"${strPart}"; | ||||
|  | ||||
|             if (defined($strLine)) | ||||
|             { | ||||
|                 $strText .= "\" .\n"; | ||||
|             } | ||||
|             else | ||||
|             { | ||||
|                 $strText .= ($iIndex + 1 < @stryText ? '\n' : '') . '"'; | ||||
|             } | ||||
|  | ||||
|             $bFirst = false; | ||||
|         } | ||||
|         while (defined($strLine)); | ||||
|  | ||||
|         $iIndex++; | ||||
|     } | ||||
|  | ||||
|     return $strText; | ||||
| } | ||||
|  | ||||
| #################################################################################################################################### | ||||
| # manGet | ||||
| # | ||||
|   | ||||
| @@ -851,6 +851,12 @@ sub processText | ||||
|         # $strBuffer =~ s/\_/\\_/g; | ||||
|     } | ||||
|  | ||||
|     if ($strType eq 'text') | ||||
|     { | ||||
|         $strBuffer =~ s/\&mdash\;/--/g; | ||||
|         $strBuffer =~ s/\<\;/\</g; | ||||
|     } | ||||
|  | ||||
|     $strBuffer = $self->variableReplace($strBuffer); | ||||
|  | ||||
|     # Return from function and log return values if any | ||||
|   | ||||
		Reference in New Issue
	
	Block a user