ADD: first version of plugin

This commit is contained in:
Dominik Meyer 2019-03-24 16:11:02 +01:00
parent ffdab07ba5
commit 8bf0e6d5e3
No known key found for this signature in database
GPG Key ID: B4C312B600606B64
2 changed files with 99 additions and 0 deletions

View File

@ -0,0 +1,80 @@
package Mojolicious::Plugin::GitConfig;
# ABSTRACT: Mojolicious Plugin for using Config::GitLike as the main configuration provider
use Mojo::Base 'Mojolicious::Plugin';
use Mojolicious::Plugin::GitConfig::Config;
use Config::GitLike;
use Try::Tiny;
=head1 DESCRIPTION
This modules uses the Config::GitLike Module to implement the Mojolicious App configuration.
# uses the default git repository configuration files
$self->plugin('GitConfig' => {git=>1});
# uses a given configuration file
$self->plugin('GitConfig' => {file=>"myconfig.conf"});
# uses the default Mojolicious configuration files
$self->plugin('GitConfig');
$self->gitconfig() returns the Config::GitLike class
=cut
=attr configdata
attribute holding the configuration data after loading the configuration file/files
=cut
has 'configdata' => undef;
=method register
method called by Mojolicous while loading this plugin
@param #1 - the class itself
@param #2 - the mojolicious app context
@param #3 - the configuration provided by loading the plugin
=cut
sub register {
my $self = shift;
my $app = shift;
my $conf = shift;
# select config file
my $file = $conf->{file} || $ENV{MOJO_CONFIG} || "config";
# if we use the git configuration files we have to do something a bit different
if ($conf->{git})
{
$self->configdata(Mojolicious::Plugin::GitConfig::Config->new(confname=>"config",compatible => 1, cascade => 1));
$self->configdata()->load();
$app->log->info("git configuration files loaded");
}
else
{
try {
$self->configdata(Config::GitLike->load_file($file));
} catch {
$app->log->fatal("could not load configuration file " . $file);
die("could not load configuration file " . $file);
};
}
$app->log->debug(__PACKAGE__ . ": register helper gitconfig");
$app->helper(gitconfig => sub {
shift;
my $params = shift;
$self->configdata();
}
);
};
1;

View File

@ -0,0 +1,19 @@
package Mojolicious::Plugin::GitConfig::Config;
# ABSTRACT: extended Config:GitLike class to use git repository config files
use Mojo::Base 'Config::GitLike';
sub global_file {
return "/etc/gitconfig";
}
sub user_file {
return $ENV{HOME} . "/.gitconfig";
}
sub dir_file {
return ".git/config";
}
1;