summaryrefslogtreecommitdiff
path: root/lib/App/Sqitch/Config.pm
blob: 8297a0c91c43b48b45ea9a623106041b760fba7d (plain)
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package App::Sqitch::Config;

use 5.010;
use Moo;
use strict;
use warnings;
use Path::Class;
use Locale::TextDomain qw(App-Sqitch);
use App::Sqitch::X qw(hurl);
use Config::GitLike 1.15;
use utf8;

extends 'Config::GitLike';

our $VERSION = 'v1.4.1'; # VERSION

has '+confname' => ( default => 'sqitch.conf' );
has '+encoding' => ( default => 'UTF-8' );

# Set by ./Build; see Module::Build::Sqitch for details.
my $SYSTEM_DIR = undef;

sub user_dir {
    my $hd = $^O eq 'MSWin32' && "$]" < '5.016' ? $ENV{HOME} || $ENV{USERPROFILE} : (glob('~'))[0];
    hurl config => __("Could not determine home directory") if not $hd;
    return dir $hd, '.sqitch';
}

sub system_dir {
    dir $SYSTEM_DIR || do {
        require Config;
        $Config::Config{prefix}, 'etc', 'sqitch';
    };
}

sub system_file {
    my $self = shift;
    return file $ENV{SQITCH_SYSTEM_CONFIG}
        || $self->system_dir->file( $self->confname );
}

sub global_file { shift->system_file }

sub user_file {
    my $self = shift;
    return file $ENV{SQITCH_USER_CONFIG}
        || $self->user_dir->file( $self->confname );
}

sub local_file {
    return file $ENV{SQITCH_CONFIG} if $ENV{SQITCH_CONFIG};
    return file shift->confname;
}

sub dir_file { shift->local_file }

# Section keys always have the top section lowercase, and subsections are
# left as-is.
sub _skey($) {
    my $key = shift // return '';
    my ($sec, $sub, $name) = Config::GitLike::_split_key($key);
    return lc $key unless $sec;
    return lc($sec) . '.' . join '.',   grep { defined } $sub, $name;
}

sub get_section {
    my ( $self, %p ) = @_;
    $self->load unless $self->is_loaded;
    my $section = _skey $p{section};
    my $data    = $self->data;
    return {
        map  {
            ( split /[.]/ => $self->initial_key("$section.$_") )[-1],
            $data->{"$section.$_"}
        }
        grep { s{^\Q$section.\E([^.]+)$}{$1} } keys %{$data}
    };
}

sub initial_key {
    my $key = shift->original_key(shift);
    return ref $key ? $key->[0] : $key;
}

sub initialized {
    my $self = shift;
    $self->load unless $self->is_loaded;
    return $self->{_initialized};
}

sub load_dirs {
    my $self = shift;
    local $self->{__loading_dirs} = 1;
    $self->SUPER::load_dirs(@_);
}

sub load_file {
    my $self = shift;
    $self->{_initialized} ||= $self->{__loading_dirs};
    $self->SUPER::load_file(@_);
}

1;

=head1 Name

App::Sqitch::Config - Sqitch configuration management

=head1 Synopsis

  my $config = App::Sqitch::Config->new;
  say scalar $config->dump;

=head1 Description

This class provides the interface to Sqitch configuration. It inherits from
L<Config::GitLike>, and therefore provides the complete interface of that
module.

=head1 Interface

=head2 Instance Methods

=head3 C<confname>

Returns the configuration file base name, which is F<sqitch.conf>.

=head3 C<system_dir>

Returns the path to the system configuration directory, which is
F<$(prefix)/etc/sqitch/templates>. Call C<sqitch --etc-path> to find out
where, exactly (e.g., C<$(sqitch --etc-path)/sqitch.plan>).

=head3 C<user_dir>

Returns the path to the user configuration directory, which is F<~/.sqitch/>.

=head3 C<system_file>

Returns the path to the system configuration file. The value returned will be
the contents of the C<$SQITCH_SYSTEM_CONFIG> environment variable, if it's
defined, or else F<$(prefix)/etc/sqitch/templates>. Call C<sqitch --etc-path>
to find out where, exactly (e.g., C<$(sqitch --etc-path)/sqitch.plan>).

=head3 C<global_file>

An alias for C<system_file()> for use by the parent class.

=head3 C<user_file>

Returns the path to the user configuration file. The value returned will be
the contents of the C<$SQITCH_USER_CONFIG> environment variable, if it's
defined, or else C<~/.sqitch/sqitch.conf>.

=head3 C<local_file>

Returns the path to the local configuration file, which is just
F<./sqitch.conf>, unless C<$SQITCH_CONFIG> is set, in which case its value
will be returned.

=head3 C<dir_file>

An alias for C<local_file()> for use by the parent class.

=head3 C<initialized>

  say 'Project not initialized' unless $config->initialized;

Returns true if the project configuration file was found, and false if it was
not. Useful for detecting when a command has been run from a directory with no
Sqitch configuration.

=head3 C<get_section>

  my $core = $config->get_section(section => 'core');
  my $pg   = $config->get_section(section => 'engine.pg');

Returns a hash reference containing only the keys within the specified
section or subsection.

=head3 C<add_comment>

Adds a comment to the configuration file.

=head3 C<initial_key>

  my $key = $config->initial_key($data_key);

Given the lowercase key from the loaded data, this method returns it in its
original case. This is like C<original_key>, only in the case where there are
multiple keys (for multivalue keys), only the first key is returned.

=head1 See Also

=over

=item * L<Config::GitLike>

=item * L<App::Sqitch::Command::config>

=item * L<sqitch-config>

=back

=head1 Author

David E. Wheeler <david@justatheory.com>

=head1 License

Copyright (c) 2012-2024 iovation Inc., David E. Wheeler

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

=cut