/
Admin.pm
149 lines (109 loc) · 3.06 KB
/
Admin.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package LedgerSMB::Admin;
=head1 NAME
LedgerSMB::Admin -
=head1 SYNOPSIS
=head1 DESCRIPTION
=cut
use strict;
use warnings;
use DBD::Pg;
use DBI;
use File::Spec;
use Getopt::Long qw(GetOptionsFromArray);
use Hash::Merge;
use List::Util qw(first);
use Log::Any::Adapter;
use Log::Any::Adapter::Log4perl;
use Log::Log4perl qw(:easy);
use Module::Runtime qw(use_module compose_module_name);
use Pod::Usage qw(pod2usage);
use YAML::PP qw(LoadFile);
our $VERSION = '0.0.1';
use LedgerSMB::Admin::Configuration;
=head1 METHODS
=cut
my @potential_configs = (
{ path => '/usr/local/etc/ledgersmb-admin.conf',
user => 0,
},
{ path => '/etc/ledgersmb-admin.conf',
user => 0,
},
);
my $merger = Hash::Merge->new();
$merger->set_behavior('LEFT_PRECEDENT');
sub _load_config {
my $cfg_path = first { -f $_->{path} } (
{ path => "$ENV{HOME}/.ledgersmb-admin.conf",
user => 1,
},
@potential_configs
);
return { connect_data => {} } unless $cfg_path;
my $cfg = LoadFile($cfg_path->{path});
###TODO: check type of $cfg... we really need it to be a hash!
my $sys_cfg;
if ($cfg_path->{user}) {
$cfg_path = first { -f $_->{path} } @potential_configs;
if ($cfg_path) {
###TODO: check type of $cfg... we really need it to be a hash!
$sys_cfg = LoadFile($cfg_path->{path});
}
else {
$sys_cfg = {};
}
}
else {
$sys_cfg = $cfg;
}
# base configuration inherits into user configuration
$merger->merge($cfg, $sys_cfg->{base}) if exists $sys_cfg->{base};
delete $cfg->{base};
return $cfg;
}
=head2 help
=cut
sub help {
pod2usage(-verbose => 99, -noperldoc => 1,
-sections => [ qw(SYNOPSIS DESCRIPTION OPTIONS COMMANDS
CONFIGURATION), 'EXIT STATUS' ]);
}
=head2 version
=cut
sub version {
print <<EOF;
ledgersmb-admin version $VERSION
Dependency versions:
DBI $DBI::VERSION
DBD::Pg $DBD::Pg::VERSION
EOF
exit 1;
}
=head2 run_command
=cut
sub run_command {
my (@args) = @_;
my %options = ();
Getopt::Long::Configure(qw(bundling require_order));
GetOptionsFromArray(\@args, \%options, 'help', 'version', 'debug');
Log::Log4perl->easy_init($options{debug} ? $DEBUG : $INFO);
Log::Any::Adapter->set('Log4perl');
my ($cmd, @cmd_args) = @args;
return help() if $options{help};
return version() if $options{version};
return help() if not defined $cmd or $cmd eq 'help';
Getopt::Long::Configure(qw(permute));
my $class = compose_module_name('LedgerSMB::Admin::Command', $cmd);
my $config = LedgerSMB::Admin::Configuration->new(
config => _load_config(),
);
return use_module($class)->new(
config => $config
)->run(@cmd_args);
}
1;
=head1 LICENSE AND COPYRIGHT
Copyright (C) 2020 The LedgerSMB Core Team
This file is licensed under the GNU General Public License version 2, or at your
option any later version. A copy of the license should have been included with
your software.