line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Prophet::Config; |
2
|
40
|
|
|
40
|
|
179
|
use Any::Moose; |
|
40
|
|
|
|
|
58
|
|
|
40
|
|
|
|
|
196
|
|
3
|
40
|
|
|
40
|
|
15923
|
use File::Spec; |
|
40
|
|
|
|
|
56
|
|
|
40
|
|
|
|
|
977
|
|
4
|
40
|
|
|
40
|
|
3959
|
use Prophet::Util; |
|
40
|
|
|
|
|
62
|
|
|
40
|
|
|
|
|
2272
|
|
5
|
|
|
|
|
|
|
extends 'Config::GitLike'; |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
has app_handle => ( |
8
|
|
|
|
|
|
|
is => 'ro', |
9
|
|
|
|
|
|
|
weak_ref => 1, |
10
|
|
|
|
|
|
|
isa => 'Prophet::App', |
11
|
|
|
|
|
|
|
required => 1 |
12
|
|
|
|
|
|
|
); |
13
|
|
|
|
|
|
|
|
14
|
40
|
|
|
40
|
|
216
|
use constant FORMAT_VERSION => 0; |
|
40
|
|
|
|
|
56
|
|
|
40
|
|
|
|
|
33415
|
|
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
# reload config after setting values |
17
|
|
|
|
|
|
|
override group_set => sub { |
18
|
|
|
|
|
|
|
my $self = shift; |
19
|
|
|
|
|
|
|
my ($filename, $args_ref, $override) = @_; |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
# Set a config format version on this config file if |
22
|
|
|
|
|
|
|
# it doesn't have one already. |
23
|
|
|
|
|
|
|
unshift @$args_ref, { |
24
|
|
|
|
|
|
|
key => 'core.config-format-version', |
25
|
|
|
|
|
|
|
value => $self->FORMAT_VERSION, |
26
|
|
|
|
|
|
|
} unless _file_has_config_format_version( $filename ); |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
$self->SUPER::group_set($filename, $args_ref); |
29
|
|
|
|
|
|
|
$self->load unless $override; |
30
|
|
|
|
|
|
|
}; |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
sub _file_has_config_format_version { |
33
|
0
|
|
|
0
|
|
|
my $filename = shift; |
34
|
0
|
0
|
|
|
|
|
my $content = -f $filename ? Prophet::Util->slurp($filename) : ''; |
35
|
|
|
|
|
|
|
|
36
|
0
|
|
|
|
|
|
return $content =~ 'core.config-format-version'; |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
# per-replica config filename |
40
|
|
|
|
|
|
|
override dir_file => sub { 'config' }; |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
# Override the replica config file with the PROPHET_APP_CONFIG |
43
|
|
|
|
|
|
|
# env var if it's set. Also, don't walk up the given path if no replica |
44
|
|
|
|
|
|
|
# config is found. |
45
|
|
|
|
|
|
|
override load_dirs => sub { |
46
|
|
|
|
|
|
|
my $self = shift; |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
$self->load_file( $self->replica_config_file ) |
49
|
|
|
|
|
|
|
if -f $self->replica_config_file; |
50
|
|
|
|
|
|
|
}; |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
# If PROPHET_APP_CONFIG is set, don't load anything else |
53
|
|
|
|
|
|
|
override user_file => sub { |
54
|
|
|
|
|
|
|
my $self = shift; |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
return exists $ENV{PROPHET_APP_CONFIG} ? '' : $self->SUPER::user_file(@_); |
57
|
|
|
|
|
|
|
}; |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
override global_file => sub { |
60
|
|
|
|
|
|
|
my $self = shift; |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
return exists $ENV{PROPHET_APP_CONFIG} ? '' : $self->SUPER::global_file(@_); |
63
|
|
|
|
|
|
|
}; |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
# grab all values in the 'alias' section (of the file, if given) and strip |
66
|
|
|
|
|
|
|
# away the section name |
67
|
|
|
|
|
|
|
sub aliases { |
68
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
69
|
0
|
|
|
|
|
|
my $file = shift; |
70
|
|
|
|
|
|
|
|
71
|
0
|
|
|
|
|
|
my %new_aliases; |
72
|
0
|
0
|
|
|
|
|
if ( $file ) { |
73
|
|
|
|
|
|
|
# parse the given config file with parse_content and use the |
74
|
|
|
|
|
|
|
# callbacks to add to an array |
75
|
0
|
0
|
|
|
|
|
my $content = -f $file ? Prophet::Util->slurp( $file ) : ''; |
76
|
|
|
|
|
|
|
$self->parse_content( |
77
|
|
|
|
|
|
|
content => $content, |
78
|
|
|
|
|
|
|
callback => sub { |
79
|
0
|
|
|
0
|
|
|
my %args = @_; |
80
|
0
|
0
|
|
|
|
|
return unless defined $args{name}; |
81
|
0
|
0
|
|
|
|
|
if ( $args{section} eq 'alias' ) { |
82
|
0
|
|
|
|
|
|
$new_aliases{$args{name}} = $args{value}; |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
}, |
85
|
|
|
|
|
|
|
# Most of the time this error sub won't get triggered since |
86
|
|
|
|
|
|
|
# Prophet loads the config file whenever it first tries to use |
87
|
|
|
|
|
|
|
# a value from the config file, and errors are detected at that |
88
|
|
|
|
|
|
|
# point. This always happens before this since every command |
89
|
|
|
|
|
|
|
# triggers alias processing. So this should really only explode |
90
|
|
|
|
|
|
|
# if we're running a shell and the config file has changed |
91
|
|
|
|
|
|
|
# in a bad way since we started up. |
92
|
|
|
|
|
|
|
error => sub { |
93
|
0
|
|
|
0
|
|
|
Config::GitLike::error_callback( @_, filename => $file ); |
94
|
|
|
|
|
|
|
}, |
95
|
0
|
|
|
|
|
|
); |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
else { |
98
|
0
|
|
|
|
|
|
my %aliases = $self->get_regexp( key => '^alias\.' ); |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
%new_aliases = map { |
101
|
0
|
|
|
|
|
|
my $alias = $_; |
|
0
|
|
|
|
|
|
|
102
|
0
|
|
|
|
|
|
$alias =~ s/^alias\.//; |
103
|
0
|
|
|
|
|
|
( $alias => $aliases{$_} ); |
104
|
|
|
|
|
|
|
} keys %aliases; |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
|
107
|
0
|
0
|
|
|
|
|
return wantarray ? %new_aliases : \%new_aliases; |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
# grab all the replicas we know of and return a hash of |
111
|
|
|
|
|
|
|
# name => variable, or variable => name if $args{by_variable} is true |
112
|
|
|
|
|
|
|
sub sources { |
113
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
114
|
0
|
|
|
|
|
|
my %args = ( |
115
|
|
|
|
|
|
|
by_url => undef, |
116
|
|
|
|
|
|
|
variable => 'url', |
117
|
|
|
|
|
|
|
@_, |
118
|
|
|
|
|
|
|
); |
119
|
|
|
|
|
|
|
|
120
|
0
|
|
|
|
|
|
my %sources = $self->get_regexp( key => "^replica[.].*[.]$args{variable}\$" ); |
121
|
|
|
|
|
|
|
my %new_sources = map { |
122
|
0
|
|
|
|
|
|
$_ =~ /^replica\.(.*)\.$args{variable}$/; |
|
0
|
|
|
|
|
|
|
123
|
0
|
0
|
|
|
|
|
$args{by_variable} ? ( $sources{$_} => $1 ) : ( $1 => $sources{$_} ); |
124
|
|
|
|
|
|
|
} keys %sources; |
125
|
|
|
|
|
|
|
|
126
|
0
|
0
|
|
|
|
|
return wantarray ? %new_sources : \%new_sources; |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
sub replica_config_file { |
130
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
return exists $ENV{PROPHET_APP_CONFIG} ? $ENV{PROPHET_APP_CONFIG} |
133
|
0
|
0
|
|
|
|
|
: File::Spec->catfile( |
134
|
|
|
|
|
|
|
$self->app_handle->handle->fs_root, $self->dir_file |
135
|
|
|
|
|
|
|
); |
136
|
|
|
|
|
|
|
} |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
sub _file_if_exists { |
139
|
0
|
|
|
0
|
|
|
my $self = shift; |
140
|
0
|
|
0
|
|
|
|
my $file = shift || ''; # quiet warnings |
141
|
|
|
|
|
|
|
|
142
|
0
|
0
|
|
|
|
|
return (-e $file) ? $file : ''; |
143
|
|
|
|
|
|
|
} |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
__PACKAGE__->meta->make_immutable; |
146
|
40
|
|
|
40
|
|
239
|
no Any::Moose; |
|
40
|
|
|
|
|
48
|
|
|
40
|
|
|
|
|
224
|
|
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
1; |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
__END__ |