| 1 |
#!/usr/bin/perl |
| 2 |
# Read and edit PostgreSQL config files |
| 3 |
# vim:sw=4:et: |
| 4 |
# |
| 5 |
# (C) 2014-2017 Christoph Berg <[email protected]> |
| 6 |
# |
| 7 |
# This program is free software; you can redistribute it and/or modify |
| 8 |
# it under the terms of the GNU General Public License as published by |
| 9 |
# the Free Software Foundation; either version 2 of the License, or |
| 10 |
# (at your option) any later version. |
| 11 |
# |
| 12 |
# This program is distributed in the hope that it will be useful, |
| 13 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
# GNU General Public License for more details. |
| 16 |
|
| 17 |
use strict; |
| 18 |
use warnings; |
| 19 |
use Getopt::Long; |
| 20 |
use PgCommon qw(quote_conf_value user_cluster_map read_cluster_conf_file |
| 21 |
read_conf_file set_conffile_value set_conf_value disable_conffile_value |
| 22 |
disable_conf_value cluster_exists error config_bool); |
| 23 |
|
| 24 |
## option parsing |
| 25 |
|
| 26 |
sub help ($;$) |
| 27 |
{ |
| 28 |
my ($exit, $error) = @_; |
| 29 |
print STDERR "Error: $error\n" if ($error); |
| 30 |
print "Usage: |
| 31 |
$0 [options] [<version> <cluster name>] [<configfile>] <command> |
| 32 |
|
| 33 |
Options: |
| 34 |
-b --boolean Format output as boolean |
| 35 |
-s --short Print only value |
| 36 |
-v --verbose Verbose output |
| 37 |
--help This help |
| 38 |
|
| 39 |
Commands: |
| 40 |
show <parameter>|all |
| 41 |
set <parameter> <value> |
| 42 |
remove <parameter> |
| 43 |
edit |
| 44 |
"; |
| 45 |
exit $exit; |
| 46 |
} |
| 47 |
|
| 48 |
my $boolean = 0; |
| 49 |
my $short = 0; |
| 50 |
my $verbose = 0; |
| 51 |
|
| 52 |
Getopt::Long::Configure ("bundling"); |
| 53 |
help(1) unless GetOptions ( |
| 54 |
'help' => sub { help(0); }, |
| 55 |
'b|boolean' => \$boolean, |
| 56 |
's|short' => \$short, |
| 57 |
'v|verbose' => \$verbose, |
| 58 |
); |
| 59 |
|
| 60 |
# find command in argument array |
| 61 |
my $cmdidx; |
| 62 |
for (my $i = 0; $i < @ARGV; $i++) { |
| 63 |
if ($ARGV[$i] =~ /^(show|get|set|remove|delete|disable|edit)$/) { |
| 64 |
$cmdidx = $i; |
| 65 |
last; |
| 66 |
} |
| 67 |
} |
| 68 |
help(1, 'No command given') unless (defined $cmdidx); |
| 69 |
|
| 70 |
my ($version, $cluster, $conffile); |
| 71 |
if ($cmdidx == 0) { # operate on postgresql.conf in default cluster |
| 72 |
($version, $cluster) = user_cluster_map(); |
| 73 |
error "No default cluster found" unless ($cluster); |
| 74 |
$conffile = 'postgresql.conf'; |
| 75 |
} elsif ($cmdidx == 1) { # operate on given file; default cluster must exist if file is relative |
| 76 |
$conffile = $ARGV[0]; |
| 77 |
unless ($conffile =~ m!^/!) { |
| 78 |
($version, $cluster) = user_cluster_map(); |
| 79 |
error "No default cluster found" unless ($cluster); |
| 80 |
} |
| 81 |
} elsif ($cmdidx == 2) { # version cluster, postgresql.conf |
| 82 |
($version, $cluster, $conffile) = ($ARGV[0], $ARGV[1], 'postgresql.conf'); |
| 83 |
} elsif ($cmdidx == 3) { # version cluster conffile |
| 84 |
($version, $cluster, $conffile) = ($ARGV[0], $ARGV[1], $ARGV[2]); |
| 85 |
} else { |
| 86 |
help(1, 'Too many arguments before command'); |
| 87 |
} |
| 88 |
|
| 89 |
if ($cluster) { # the cluster needs to exist unless an absolute conffile was given |
| 90 |
error "Cluster $version $cluster does not exist" |
| 91 |
unless (cluster_exists $version, $cluster); |
| 92 |
} |
| 93 |
|
| 94 |
splice @ARGV, 0, $cmdidx; # remove everything before the command |
| 95 |
$ARGV[0] = 'show' if ($ARGV[0] eq 'get'); # accept alternative variants of some commands |
| 96 |
$ARGV[0] = 'remove' if ($ARGV[0] =~ /delete|disable/); |
| 97 |
|
| 98 |
my ($cmd, $key, $value); |
| 99 |
if ($ARGV[0] =~ /^(edit)$/) { |
| 100 |
help(1, "$ARGV[0] takes no argument") unless (@ARGV == 1); |
| 101 |
($cmd) = @ARGV; |
| 102 |
} elsif ($ARGV[0] =~ /^(show|remove)$/) { |
| 103 |
help(1, "$ARGV[0] needs exactly one argument") unless (@ARGV == 2); |
| 104 |
($cmd, $key) = @ARGV; |
| 105 |
} else { |
| 106 |
help(1, "$ARGV[0] needs exactly two arguments") unless (@ARGV == 3); |
| 107 |
($cmd, $key, $value) = @ARGV; |
| 108 |
} |
| 109 |
#print "$version $cluster $conffile $cmd $key $value\n"; |
| 110 |
|
| 111 |
## main |
| 112 |
|
| 113 |
if ($cmd eq 'show') { |
| 114 |
my %conf; |
| 115 |
if ($conffile =~ m!^/!) { |
| 116 |
%conf = read_conf_file ($conffile, 0); |
| 117 |
} else { |
| 118 |
%conf = read_cluster_conf_file ($version, $cluster, $conffile, 0); |
| 119 |
} |
| 120 |
|
| 121 |
if ($key eq 'all') { |
| 122 |
foreach my $k (sort keys %conf) { |
| 123 |
printf "%s = %s\n", $k, quote_conf_value($conf{$k}); |
| 124 |
} |
| 125 |
} elsif (exists $conf{$key}) { |
| 126 |
$conf{$key} = config_bool($conf{$key}) ? 'on' : 'off' if ($boolean); # normalize boolean output on request |
| 127 |
if ($short) { |
| 128 |
printf "%s\n", $conf{$key}; |
| 129 |
} else { |
| 130 |
printf "%s = %s\n", $key, quote_conf_value($conf{$key}); |
| 131 |
} |
| 132 |
} else { |
| 133 |
print "# $key not found in $conffile\n" if ($verbose); |
| 134 |
exit 1; |
| 135 |
} |
| 136 |
|
| 137 |
} elsif ($cmd eq 'set') { |
| 138 |
if ($conffile =~ m!^/!) { |
| 139 |
set_conffile_value ($conffile, $key, $value); |
| 140 |
} else { |
| 141 |
set_conf_value ($version, $cluster, $conffile, $key, $value); |
| 142 |
} |
| 143 |
|
| 144 |
} elsif ($cmd eq 'remove') { |
| 145 |
if ($conffile =~ m!^/!) { |
| 146 |
disable_conffile_value ($conffile, $key); |
| 147 |
} else { |
| 148 |
disable_conf_value ($version, $cluster, $conffile, $key); |
| 149 |
} |
| 150 |
|
| 151 |
} elsif ($cmd eq 'edit') { |
| 152 |
my $editor = 'vi'; |
| 153 |
if ($ENV{EDITOR}) { |
| 154 |
($editor) = $ENV{EDITOR} =~ /(.*)/; # untaint |
| 155 |
} |
| 156 |
if ($conffile =~ m!^/!) { |
| 157 |
system $editor, $conffile; |
| 158 |
} else { |
| 159 |
system $editor, "$PgCommon::confroot/$version/$cluster/$conffile"; |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
__END__ |
| 164 |
|
| 165 |
=head1 NAME |
| 166 |
|
| 167 |
pg_conftool - read and edit PostgreSQL cluster configuration files |
| 168 |
|
| 169 |
=head1 SYNOPSIS |
| 170 |
|
| 171 |
B<pg_conftool> [I<options>] [I<version> I<cluster>] [I<configfile>] B<command> |
| 172 |
|
| 173 |
=head1 DESCRIPTION |
| 174 |
|
| 175 |
B<pg_conftool> allows showing and setting parameters in PostgreSQL configuration |
| 176 |
files. |
| 177 |
|
| 178 |
If I<version> I<cluster> is omitted, it defaults to the default cluster (see |
| 179 |
user_clusters(5) and postgresqlrc(5)). If I<configfile> is omitted, it defaults |
| 180 |
to B<postgresql.conf>. I<configfile> can also be a path, in which case |
| 181 |
I<version> I<cluster> is ignored. |
| 182 |
|
| 183 |
=head1 OPTIONS |
| 184 |
|
| 185 |
=over 4 |
| 186 |
|
| 187 |
=item B<-b>, B<--boolean> |
| 188 |
|
| 189 |
Format boolean value as B<on> or B<off> (not for "show all"). |
| 190 |
|
| 191 |
=item B<-s>, B<--short> |
| 192 |
|
| 193 |
Show only the value (instead of key = value pair). |
| 194 |
|
| 195 |
=item B<-v>, B<--verbose> |
| 196 |
|
| 197 |
Verbose output. |
| 198 |
|
| 199 |
=item B<--help> |
| 200 |
|
| 201 |
Print help. |
| 202 |
|
| 203 |
=back |
| 204 |
|
| 205 |
=head1 COMMANDS |
| 206 |
|
| 207 |
=over 4 |
| 208 |
|
| 209 |
=item B<show> I<parameter>|B<all> |
| 210 |
|
| 211 |
Show a parameter, or all present in this config file. |
| 212 |
|
| 213 |
=item B<set> I<parameter> I<value> |
| 214 |
|
| 215 |
Set or update a parameter. |
| 216 |
|
| 217 |
=item B<remove> I<parameter> |
| 218 |
|
| 219 |
Remove (comment out) a parameter from a config file. |
| 220 |
|
| 221 |
=item B<edit> |
| 222 |
|
| 223 |
Open the config file in an editor. Unless B<$EDITOR> is set, B<vi> is used. |
| 224 |
|
| 225 |
=back |
| 226 |
|
| 227 |
=head1 SEE ALSO |
| 228 |
|
| 229 |
user_clusters(5), postgresqlrc(5) |
| 230 |
|
| 231 |
=head1 AUTHOR |
| 232 |
|
| 233 |
Christoph Berg L<E<lt>[email protected]<gt>> |