Skip to content
This repository was archived by the owner on Jul 26, 2020. It is now read-only.

Commit 405fddd

Browse files
author
Stephen Bennett
committed
Clean up GMS::Config interface slightly
1 parent 98e6b19 commit 405fddd

13 files changed

+85
-94
lines changed

bin/add_user_role.pl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@
1212

1313
die "Usage: $0 <account name> <roles...>" if ! $accountname;
1414

15-
my $db = GMS::Schema->connect($GMS::Config::dbstring,
16-
$GMS::Config::dbuser, $GMS::Config::dbpass);
15+
my $atheme_config = GMS::Config->atheme;
16+
my $db = GMS::Schema->do_connect;
1717

18-
my $session = RPC::Atheme::Session->new($GMS::Config::atheme_host,
19-
$GMS::Config::atheme_port);
20-
$session->login($GMS::Config::atheme_master_login, $GMS::Config::atheme_master_pass)
18+
my $session = RPC::Atheme::Session->new($atheme_config->{hostname},
19+
$atheme_config->{port});
20+
$session->login($atheme_config->{master_login}, $atheme_config->{master_password})
2121
or die "Couldn't log in to atheme";
2222

23-
my $accountid = $session->command($GMS::Config::service, 'accountid', $accountname);
23+
my $accountid = $session->command($atheme_config->{service}, 'accountid', $accountname);
2424
my $account = $db->resultset('Account')->find({ id => $accountid });
2525

2626
print "Found account ID ", $account->id, ", named ", $account->accountname, "\n";

bin/check_verifications.pl

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,14 @@
44
use lib "$FindBin::Bin/../lib";
55

66
use GMS::Schema;
7-
use GMS::Config;
87

98
use LWP::UserAgent;
109
use HTTP::Request;
1110

1211
my $ua = LWP::UserAgent->new;
1312
$ua->agent('GMS/0.1');
1413

15-
my $db = GMS::Schema->connect($GMS::Config::dbstring,
16-
$GMS::Config::dbuser, $GMS::Config::dbpass);
14+
my $db = GMS::Schema->do_connect;
1715

1816
my $rs = $db->resultset('Group');
1917

bin/createdb.pl

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,8 @@
77
use lib "$FindBin::Bin/../lib";
88

99
use GMS::Schema;
10-
use GMS::Config;
1110

12-
my $db = GMS::Schema->connect($GMS::Config::dbstring,
13-
$GMS::Config::dbuser, $GMS::Config::dbpass);
11+
my $db = GMS::Schema->do_connect;
1412

1513
$db->deploy();
1614

bin/dump_fixtures.pl

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,10 @@
77
use lib "$FindBin::Bin/../lib";
88

99
use GMS::Schema;
10-
use GMS::Config;
1110

1211
use DBIx::Class::Fixtures;
1312

14-
my $db = GMS::Schema->connect($GMS::Config::dbstring,
15-
$GMS::Config::dbuser, $GMS::Config::dbpass);
13+
my $db = GMS::Schema->do_connect;
1614

1715
my $config_dir = "$FindBin::Bin/../t/etc";
1816

bin/update_schema.pl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
use DBIx::Class::Schema::Loader qw/make_schema_at/;
1212

13+
my $db_config = GMS::Config->database;
14+
1315
make_schema_at(
1416
'GMS::Schema',
1517
{
@@ -18,6 +20,6 @@
1820
components => 'InflateColumn::DateTime',
1921
},
2022
[
21-
$GMS::Config::dbstring, $GMS::Config::dbuser, $GMS::Config::dbpass
23+
$db_config->{dsn}, $db_config->{user}, $db_config->{password}
2224
],
2325
);

gms_web.conf

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@ name GMS::Web
44

55
<Model::DB>
66
<connect_info>
7-
dsn dbi:Pg:dbname=gms;host=localhost
7+
dsn dbi:Pg:dbname=gmstest;host=localhost
88
</connect_info>
99
</Model::DB>
1010

1111
<Model::Atheme>
12-
atheme_host 127.0.0.1
13-
atheme_port 8080
12+
hostname 127.0.0.1
13+
port 8080
1414
master_account GMS
1515
master_password goat
16+
service GroupServ
1617
</Model::Atheme>
1718

1819
<email>

gms_web_tests.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ name GMS::Web
1414
# atheme_port 8080
1515
# master_account GMS
1616
# master_password goat
17+
# service GroupServ
1718
#</Model::Atheme>
1819

1920
#<email>

lib/GMS/Config.pm

Lines changed: 54 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,59 @@ use strict;
44
use warnings;
55
use Config::JFDI;
66
use Dir::Self;
7-
use vars qw($dbstring $dbuser $dbpass $dbconnectinfo
8-
$atheme_host $atheme_port $service
9-
$atheme_master_login $atheme_master_pass);
10-
11-
my $config_loader = Config::JFDI->new(
12-
name => "gms_web",
13-
path => $ENV{GMS_WEB_CONFIG_PATH} || __DIR__ . "/../..",
14-
);
15-
my $config = $config_loader->get;
16-
17-
$dbstring = $config->{"Model::DB"}{connect_info}{dsn};
18-
$dbuser = $config->{"Model::DB"}{connect_info}{user};
19-
$dbpass = $config->{"Model::DB"}{connect_info}{password};
20-
$dbconnectinfo = $config->{"Model::DB"}{connect_info};
21-
22-
$atheme_host = $config->{"Model::Atheme"}{atheme_host};
23-
$atheme_port = $config->{"Model::Atheme"}{atheme_port};
24-
$service = 'GroupServ';
25-
$atheme_master_login = $config->{"Model::Atheme"}{master_account};
26-
$atheme_master_pass = $config->{"Model::Atheme"}{master_password};
7+
8+
my $config;
9+
10+
=head1 NAME
11+
12+
GMS::Config
13+
14+
=head1 DESCRIPTION
15+
16+
GMS::Config provides an interface to GMS configuration for non-Catalyst code
17+
that cannot simply use C<$c->config>. Uses L<Config::JFDI>, so settings such as
18+
C<GMS_WEB_CONFIG_LOCAL_SUFFIX> are respected.
19+
20+
=head1 METHODS
21+
22+
=head2 load_config
23+
24+
Loads the relevant configuration files. Called internally when required -- you
25+
shouldn't need to call this directly.
26+
27+
=cut
28+
29+
sub load_config {
30+
my $config_loader = Config::JFDI->new(
31+
name => "gms_web",
32+
path => $ENV{GMS_WEB_CONFIG_PATH} || __DIR__ . "/../..",
33+
);
34+
$config = $config_loader->get;
35+
}
36+
37+
=head2 database
38+
39+
Returns the database connect_info defined in the C<Model::DB> section of the
40+
configuration file(s).
41+
42+
=cut
43+
44+
sub database {
45+
load_config unless $config;
46+
return $config->{"Model::DB"}->{connect_info};
47+
}
48+
49+
=head2 atheme
50+
51+
Returns the Atheme connection info defined in the C<Model::Atheme> section of
52+
the configuration file(s).
53+
54+
=cut
55+
56+
sub atheme {
57+
load_config unless $config;
58+
return $config->{"Model::Atheme"};
59+
}
60+
2761

2862
1;

lib/GMS/Schema.pm

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,13 @@ use GMS::Config;
4848
sub do_connect {
4949
my $self = shift;
5050

51+
my $connectinfo = GMS::Config->database;
52+
5153
$self->connect(
52-
$GMS::Config::dbstring,
53-
$GMS::Config::dbuser,
54-
$GMS::Config::dbpass,
55-
$GMS::Config::dbconnectinfo);
54+
$connectinfo->{dsn},
55+
$connectinfo->{user},
56+
$connectinfo->{password},
57+
$connectinfo);
5658
}
5759

5860

lib/GMS/Session.pm

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,24 +36,25 @@ sub new {
3636
_control_session => $controlsession,
3737
};
3838

39+
my $config = $GMS::Config->atheme;
40+
3941
$self->{_source} = "GMS:$user";
4042
$self->{_source} .= "(" . $config{source} . ")" if $config{source};
4143

4244
$self->{_rpcsession} = RPC::Atheme::Session->new(
43-
$GMS::Config::atheme_host,
44-
$GMS::Config::atheme_port
45+
$config->{hostname},
46+
$config->{port}
4547
);
4648

4749
$self->{_rpcsession}->login($user, $pass, $self->{_source});
4850

49-
$self->{_db} = GMS::Schema->connect($GMS::Config::dbstring,
50-
$GMS::Config::dbuser, $GMS::Config::dbpass);
51+
$self->{_db} = GMS::Schema->do_connect;
5152
my $account_rs = $self->{_db}->resultset('Account');
5253

5354
my $account = undef;
5455

5556
try {
56-
my $accountid = $self->{_control_session}->command($GMS::Config::service, 'accountid', $user);
57+
my $accountid = $self->{_control_session}->command($config->{service}, 'accountid', $user);
5758
$account = $account_rs->find({ id => $accountid });
5859
}
5960
catch (RPC::Atheme::Error $e) {
@@ -66,7 +67,7 @@ sub new {
6667
my $result = $account_rs->create({
6768
accountname => $user,
6869
});
69-
$self->{_control_session}->command($GMS::Config::service, 'accountid',
70+
$self->{_control_session}->command($config->{service}, 'accountid',
7071
$user, $result->id);
7172
});
7273
};

lib/GMS/Web/Model/Atheme.pm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ sub start_session {
1616
}
1717

1818
my $session = RPC::Atheme::Session->new(
19-
$self->{atheme_host}, $self->{atheme_port}
19+
$self->{hostname}, $self->{port}
2020
);
2121

2222
$session->login(

t/etc/schema.pl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
{
55
schema_class => 'GMS::Schema',
6-
#connect_info => $GMS::Config::dbstring,
76
resultsets => [
87
'Account', 'Contact', 'GroupContact', 'Group'
98
],

test/test_session.pl

Lines changed: 0 additions & 43 deletions
This file was deleted.

0 commit comments

Comments
 (0)