Skip to content

Commit 141a589

Browse files
committed
Add openqa-config script for showing/changing config values
Related issue: https://progress.opensuse.org/issues/184459 I was tired of either having to add instructions to manually edit config files, or to have to use some `sed` statement to manipulate a config file.
1 parent a61b311 commit 141a589

File tree

3 files changed

+154
-1
lines changed

3 files changed

+154
-1
lines changed

lib/OpenQA/Config.pm

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,13 @@ use Config::IniFiles;
88
use Exporter qw(import);
99
use OpenQA::Log qw(log_info);
1010
use Mojo::File qw(path);
11+
use File::Copy qw(copy);
12+
use YAML::PP;
1113

12-
our @EXPORT = qw(config_dir_within_app_home lookup_config_files parse_config_files parse_config_files_as_hash);
14+
our @EXPORT = qw(
15+
config_dir_within_app_home lookup_config_files parse_config_files parse_config_files_as_hash
16+
show_config write_config
17+
);
1318

1419
sub _config_dirs ($config_dir_within_home) {
1520
return [[$ENV{OPENQA_CONFIG} // ()], [$config_dir_within_home // ()], ['/etc/openqa', '/usr/etc/openqa']];
@@ -56,4 +61,47 @@ sub parse_config_files_as_hash ($config_file_paths) {
5661
return \%config_hash;
5762
}
5863

64+
sub show_config ($file, $params) {
65+
my $conf = Config::IniFiles->new(-file => $file);
66+
my ($section, $param, $val) = @$params;
67+
unless (defined $section) {
68+
my @sections = $conf->Sections;
69+
return YAML::PP::Dump({Sections => \@sections});
70+
}
71+
unless (defined $param) {
72+
my @keys = $conf->Parameters($section);
73+
return YAML::PP::Dump(
74+
{
75+
"Parameters($section)" => {
76+
map { $_ => $conf->val($section, $_) } @keys
77+
}});
78+
}
79+
unless (defined $val) {
80+
$val = $conf->val($section, $param);
81+
return $val;
82+
}
83+
return;
84+
}
85+
86+
sub write_config ($file, $params, $backup = 0) {
87+
my $conf = Config::IniFiles->new(-file => $file);
88+
my ($section, $param, $val) = @$params;
89+
if (!defined $section || !defined $param || !defined $val) {
90+
warn "You need to pass section, parameter and value\n";
91+
return 1;
92+
}
93+
$conf->newval($section, $param, $val);
94+
if (my $bak = $backup) {
95+
copy $file, "$file.$bak";
96+
}
97+
if (ref $file) {
98+
# we have something other than just a filename; just print
99+
$conf->OutputConfig;
100+
}
101+
else {
102+
$conf->RewriteConfig();
103+
}
104+
}
105+
106+
59107
1;

script/openqa-config

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#!/usr/bin/env perl
2+
# Copyright SUSE LLC
3+
# SPDX-License-Identifier: GPL-2.0-or-later
4+
5+
use Mojo::Base -strict, -signatures;
6+
use FindBin;
7+
use lib "$FindBin::RealBin/../lib";
8+
9+
use Getopt::Long;
10+
Getopt::Long::Configure("no_ignore_case");
11+
use Config::IniFiles;
12+
use OpenQA::Config qw(show_config write_config);
13+
14+
sub usage ($r) { require Pod::Usage; Pod::Usage::pod2usage($r) }
15+
16+
GetOptions(\my %options, 'help|h|?', 'backup=s', 'write') or usage(1);
17+
18+
usage(0) if $options{help};
19+
20+
my ($file, $section, $key, $val) = @ARGV;
21+
22+
my $rc = 0;
23+
if ($options{write}) {
24+
$rc = write_config($file, [$section, $key, $val], $options{backup});
25+
}
26+
else {
27+
my $out = show_config($file, [$section, $key, $val]);
28+
if (defined $out) {
29+
chomp $out;
30+
say $out;
31+
}
32+
}
33+
exit($rc || 0);
34+
35+
=pod
36+
37+
=head1 SYNOPSIS
38+
39+
openqa-config file.ini [section] [parameter] [value]
40+
41+
Show or save config values in a file
42+
43+
=over
44+
45+
=item List sections
46+
47+
openqa-config file.ini
48+
49+
=item List parameters and values in a section
50+
51+
openqa-config file.ini section
52+
53+
=item Print value of a parameter
54+
55+
openqa-config file.ini section parameter
56+
57+
=item Set new value of a parameter
58+
59+
openqa-config file.ini --write section parameter value
60+
61+
=back
62+
63+
=head1 OPTIONS
64+
65+
=over 4
66+
67+
=item B<--help>
68+
69+
=over 4
70+
71+
=item B<--write>
72+
73+
Write new value to file
74+
75+
=item B<--backup> suffix
76+
77+
Copy original C<file.ini> to C<file.ini.suffix>
78+
79+
=back
80+
81+
=cut

t/config.t

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,4 +356,28 @@ subtest 'Lookup precedence/hiding' => sub {
356356
is_deeply lookup_config_files(@args), \@expected, 'drop-in in overriden dir hides all other config';
357357
};
358358

359+
subtest 'show and write config values' => sub {
360+
my $ini = <<~'EOM';
361+
[section1]
362+
A = 1
363+
B=2
364+
EOM
365+
my $out = show_config(\$ini, []);
366+
like $out, qr{section1}, 'show sections';
367+
$out = show_config(\$ini, ['section1']);
368+
like $out, qr{section1.*A.*1.*B.*2}s, 'show section';
369+
$out = show_config(\$ini, ['section1', 'B']);
370+
is $out, 2, 'show section param';
371+
372+
# Config::IniFiles does not have a method to get the ini output as
373+
# a simple string, it can only write to files and file handles
374+
my $string;
375+
my $scalar = IO::Scalar->new(\$string);
376+
my $orig = select $scalar;
377+
write_config(\$ini, [section2 => X => 2]);
378+
select $orig;
379+
diag $string;
380+
like $string, qr{section2.*X=2}s, 'write_config new section / param';
381+
};
382+
359383
done_testing();

0 commit comments

Comments
 (0)