btrbk: add "allow_multiple" flag for config_options declaration; use for option "group"

pull/204/merge
Axel Burri 2018-02-13 19:26:54 +01:00
parent 3aaafa3d88
commit 0ebe2ea2e1
2 changed files with 42 additions and 23 deletions

62
btrbk
View File

@ -118,7 +118,7 @@ my %config_options = (
kdf_keysize => { default => "32", accept_numeric => 1 },
kdf_keygen => { default => "once", accept => [ "once", "each" ] },
group => { default => undef, accept_regexp => qr/^$group_match(\s*,\s*$group_match)*$/, split => qr/\s*,\s*/ },
group => { default => undef, accept_regexp => qr/^$group_match(\s*,\s*$group_match)*$/, allow_multiple => 1, split => qr/\s*,\s*/ },
backend => { default => "btrfs-progs", accept => [ "btrfs-progs", "btrfs-progs-btrbk", "btrfs-progs-sudo" ] },
backend_local => { default => undef, accept => [ "no", "btrfs-progs", "btrfs-progs-btrbk", "btrfs-progs-sudo" ] },
@ -3102,19 +3102,18 @@ sub config_dump_keys($;@)
next unless exists($config->{$key});
$val = $config->{$key};
}
if(defined($val)) {
if($config_options{$key}->{accept_preserve_matrix}) {
$val = format_preserve_matrix($val);
}
if(ref($val) eq "ARRAY") {
my $val2 = join(',', @$val);
$val = $val2;
my @valary = (ref($val) eq "ARRAY") ? @$val : $val;
foreach(@valary) {
if(defined($_)) {
if($config_options{$key}->{accept_preserve_matrix}) {
$_ = format_preserve_matrix($_);
}
}
$_ //= exists($config->{$key}) ? "no" : "<unset>";
my $len = length($key);
$maxlen = $len if($len > $maxlen);
push @ret, { key => $key, val => $_, len => $len };
}
$val //= exists($config->{$key}) ? "no" : "<unset>";
my $len = length($key);
$maxlen = $len if($len > $maxlen);
push @ret, { key => $key, val => $val, len => $len };
}
# print as table
return map { ($opts{prefix} // "") . $_->{key} . (' ' x (1 + $maxlen - $_->{len})) . ' ' . $_->{val} } @ret;
@ -3200,11 +3199,6 @@ sub append_config_option($$$$;@)
return undef;
}
if($opt->{split}) {
$value = [ split($opt->{split}, $value) ];
TRACE "splitted option \"$key\": " . join(',', @$value);
}
if($opt->{require_bin} && (not check_exe($opt->{require_bin}))) {
WARN "Found option \"$key\", but required executable \"$opt->{require_bin}\" does not exist on your system. Please install \"$opt->{require_bin}\".";
WARN "Ignoring option \"$key\" $error_statement";
@ -3239,6 +3233,21 @@ sub append_config_option($$$$;@)
}
}
if($opt->{allow_multiple}) {
my $aref = $config->{$key} // [];
if($opt->{split}) {
push(@$aref, split($opt->{split}, $value));
}
else {
push(@$aref, $value);
}
TRACE "pushing option \"$key=$value\" to $aref=[" . join(',', @$aref) . "]";
$value = $aref;
}
elsif(exists($config->{$key})) {
WARN "Option \"$key\" redefined $error_statement";
}
TRACE "adding option \"$key=$value\" to $context context";
$value = undef if($value eq "no"); # we don't want to check for "no" all the time
$config->{$key} = $value;
@ -4226,7 +4235,7 @@ MAIN:
my $start_time = time;
@tm_now = localtime($start_time);
my %config_override_cmdline;
my @config_override_cmdline;
my ($config_cmdline, $quiet, $verbose, $preserve_snapshots, $preserve_backups, $wipe_snapshots, $skip_snapshots, $skip_backups, $print_schedule, $lockfile_cmdline);
my $resume_only_DEPRECATED; # as of btrbk-v0.26.0
unless(GetOptions(
@ -4247,7 +4256,7 @@ MAIN:
'format=s' => \$output_format,
'print-schedule' => \$print_schedule,
'lockfile=s' => \$lockfile_cmdline,
'override=s' => \%config_override_cmdline, # e.g. --override=incremental=no
'override=s' => \@config_override_cmdline, # e.g. --override=incremental=no
))
{
VERSION_MESSAGE();
@ -4409,9 +4418,18 @@ MAIN:
HELP_MESSAGE(0);
exit 2;
}
foreach my $key (keys %config_override_cmdline) {
DEBUG "config_override: \"$key=$config_override_cmdline{$key}\"";
unless(append_config_option(\%config_override, $key, $config_override_cmdline{$key}, "root", error_statement => "in option \"--override\"")) {
foreach(@config_override_cmdline) {
if(/(.*?)=(.*)/) {
my $key = $1;
my $value = $2;
DEBUG "config_override: \"$key=$value\"";
unless(append_config_option(\%config_override, $key, $value, "root", error_statement => "in option \"--override\"")) {
HELP_MESSAGE(0);
exit 2;
}
}
else {
ERROR "Option \"override\" requires \"<config_option>=<value>\" format";
HELP_MESSAGE(0);
exit 2;
}

View File

@ -146,7 +146,8 @@ Note that using ``long-iso'' has implications on the scheduling, see
*group* <group-name>[,<group-name>]...::
Add the current section (volume, subvolume or target) to a
user-defined group, which can be used as filter for most btrbk
commands.
commands. This option can be set multiple times within the same
context.
=== Retention Policy Options