BeastBB/lib/BeastBB/ConfigWriter.pm

70 lines
1.6 KiB
Perl

package BeastBB::ConfigWriter;
use 5.30.3;
use strict;
use warnings;
use Params::ValidationCompiler 'validation_for';
use Types::Standard qw( Str Int );
use Crypt::URandom 'urandom';
use BeastBB::Types qw/IsClassTypeGenerator/;
{
my $validator = validation_for(
params => {
config_file => { type => IsClassTypeGenerator('Mojo::File') },
},
);
sub new {
my $class = shift;
my %params = $validator->(@_);
my $config_file = $params{config_file};
return bless { config_file => $config_file }, $class;
}
}
sub ConfigFile {
my $self = shift;
return $self->{config_file};
}
{
my $validator = validation_for(
params => {
content => { type => Str },
}
);
sub WriteToConfigFile {
my $self = shift;
my %params = $validator->(@_);
my $content = $params{content};
my $config_file = $self->ConfigFile;
my $config_file_fh = $config_file->open('>')
or die "Unable to write the config file $config_file";
$config_file_fh->print($content);
}
}
{
my $validator =
validation_for( params => { secret_size => { type => Int } } );
sub PopulateExampleConfigFile {
my $self = shift;
my %params = $validator->(@_);
my $secret_size = $params{secret_size};
my $config = {
secrets => [
unpack 'H*', urandom($secret_size),
]
};
local $Data::Dumper::Terse = 1;
$self->WriteToConfigFile( content => Data::Dumper::Dumper $config);
}
}
1;