Skip to content

Add openqa-config script for showing/changing config values #6591

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 49 additions & 1 deletion lib/OpenQA/Config.pm
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@ use Config::IniFiles;
use Exporter qw(import);
use OpenQA::Log qw(log_info);
use Mojo::File qw(path);
use File::Copy qw(copy);
use YAML::PP;

our @EXPORT = qw(config_dir_within_app_home lookup_config_files parse_config_files parse_config_files_as_hash);
our @EXPORT = qw(
config_dir_within_app_home lookup_config_files parse_config_files parse_config_files_as_hash
show_config write_config
);

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

sub show_config ($file, $params) {
my $conf = Config::IniFiles->new(-file => $file);
my ($section, $param, $val) = @$params;
unless (defined $section) {
my @sections = $conf->Sections;
return YAML::PP::Dump({Sections => \@sections});
}
unless (defined $param) {
my @keys = $conf->Parameters($section);
return YAML::PP::Dump(
{
"Parameters($section)" => {
map { $_ => $conf->val($section, $_) } @keys
}});
}
unless (defined $val) {
$val = $conf->val($section, $param);
return $val;
}
return;
}

sub write_config ($file, $params, $backup = 0) {
my $conf = Config::IniFiles->new(-file => $file);
my ($section, $param, $val) = @$params;
if (!defined $section || !defined $param || !defined $val) {
warn "You need to pass section, parameter and value\n";
return 1;
}
$conf->newval($section, $param, $val);
if (my $bak = $backup) {
copy $file, "$file.$bak";
}
if (ref $file) {
# we have something other than just a filename; just print
$conf->OutputConfig;
}
else {
$conf->RewriteConfig();
}
}


1;
81 changes: 81 additions & 0 deletions script/openqa-config
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#!/usr/bin/env perl
# Copyright SUSE LLC
# SPDX-License-Identifier: GPL-2.0-or-later

use Mojo::Base -strict, -signatures;
use FindBin;
use lib "$FindBin::RealBin/../lib";

use Getopt::Long;
Getopt::Long::Configure("no_ignore_case");
use Config::IniFiles;
use OpenQA::Config qw(show_config write_config);

sub usage ($r) { require Pod::Usage; Pod::Usage::pod2usage($r) }

GetOptions(\my %options, 'help|h|?', 'backup=s', 'write') or usage(1);

usage(0) if $options{help};

my ($file, $section, $key, $val) = @ARGV;

my $rc = 0;
if ($options{write}) {
$rc = write_config($file, [$section, $key, $val], $options{backup});
}
else {
my $out = show_config($file, [$section, $key, $val]);
if (defined $out) {
chomp $out;
say $out;
}
}
exit($rc || 0);

=pod

=head1 SYNOPSIS

openqa-config file.ini [section] [parameter] [value]

Show or save config values in a file

=over

=item List sections

openqa-config file.ini

=item List parameters and values in a section

openqa-config file.ini section

=item Print value of a parameter

openqa-config file.ini section parameter

=item Set new value of a parameter

openqa-config file.ini --write section parameter value

=back

=head1 OPTIONS

=over 4

=item B<--help>

=over 4

=item B<--write>

Write new value to file

=item B<--backup> suffix

Copy original C<file.ini> to C<file.ini.suffix>

=back

=cut
24 changes: 24 additions & 0 deletions t/config.t
Original file line number Diff line number Diff line change
Expand Up @@ -356,4 +356,28 @@ subtest 'Lookup precedence/hiding' => sub {
is_deeply lookup_config_files(@args), \@expected, 'drop-in in overriden dir hides all other config';
};

subtest 'show and write config values' => sub {
my $ini = <<~'EOM';
[section1]
A = 1
B=2
EOM
my $out = show_config(\$ini, []);
like $out, qr{section1}, 'show sections';
$out = show_config(\$ini, ['section1']);
like $out, qr{section1.*A.*1.*B.*2}s, 'show section';
$out = show_config(\$ini, ['section1', 'B']);
is $out, 2, 'show section param';

# Config::IniFiles does not have a method to get the ini output as
# a simple string, it can only write to files and file handles
my $string;
my $scalar = IO::Scalar->new(\$string);
my $orig = select $scalar;
write_config(\$ini, [section2 => X => 2]);
select $orig;
diag $string;
like $string, qr{section2.*X=2}s, 'write_config new section / param';
};

done_testing();