v4/Config.pm untested

filter: Statementstmt 0.0% 0 / 85 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 127 CC 6 SCAR 17.9
CC
6
Cov
0%
configure
20.0
_build_file
6.0
_build_file_args
6.0
CRAP 6.0
1 package GitLab::API::v4::Config;
2 our $VERSION = '0.27';
3  
4 =encoding utf8
5  
6 =head1 NAME
7  
8 GitLab::API::v4::Config - Load configuration from a file, environment,
9 and/or CLI options.
10  
11 =head1 SYNOPSIS
12  
13 use GitLab::API::v4;
14 use GitLab::API::v4::Config;
15
16 my $config = GitLab::API::v4::Config->new();
17 my $api = GitLab::API::v4->new( $config->args() );
18  
19 =head1 DESCRIPTION
20  
21 This module is used by L<gitlab-api-v4> to load configuration.
22  
23 If you are using L<GitLab::API::v4> directly then this module will not be
24 automatically used, but you are welcome to explicitly use it as shown in the
25 L</SYNOPSIS>.
26  
27 =cut
28  
29 use Getopt::Long;
30 use IO::Prompter;
31 use JSON::MaybeXS;
32 use Log::Any qw( $log );
33 use Path::Tiny;
34 use Types::Common::String -types;
35 use Types::Standard -types;
36  
37 use Moo;
38 use strictures 2;
39 use namespace::clean;
40  
41 sub _filter_args {
42 my ($self, $args) = @_;
43  
44 return {
45 map { $_ => $args->{$_} }
46 grep { $args->{$_} }
47 keys %$args
48 };
49 }
50  
51 =head1 ARGUMENTS
52  
53 =head2 file
54  
55 The file to load configuration from. The file should hold valid JSON.
56  
57 By default this will be set to C<.gitlab-api-v4-config> in the current
58 user's home directory.
59  
60 This can be overridden with the C<GITLAB_API_V4_CONFIG_FILE> environment
61 variable or the C<--config-file=...> command line argument.
62  
63 =cut
64  
65 has file => (
66 is => 'lazy',
67 isa => NonEmptySimpleStr,
68 );
69 sub _build_file {
70 my ($self) = @_;
71  
72 my $file = $self->opt_args->{config_file}
73 || $self->env_args->{config_file};
74 return $file if $file;
75  
76 my ($home) = ( getpwuid($<) )[7];
77 return '' . path( $home )->child('.gitlab-api-v4-config');
78 }
79  
80 =head1 ATTRIBUTES
81  
82 =head2 opt_args
83  
84 Returns a hashref of arguments derived from command line options.
85  
86 Supported options are:
87  
88 --config_file=...
89 --url=...
90 --private-token=...
91 --access-token=...
92 --retries=...
93  
94 Note that the options are read from, and removed from, C<@ARGV>. Due
95 to this the arguments are saved internally and re-used for all instances
96 of this class so that there are no weird race conditions.
97  
98 =cut
99  
100 has opt_args => (
101 is => 'rwp',
102 isa => HashRef,
103 default => sub{ {} },
104 );
105  
106 =head2 env_args
107  
108 Returns a hashref of arguments derived from environment variables.
109  
110 Supported environment variables are:
111  
112 GITLAB_API_V4_CONFIG_FILE
113 GITLAB_API_V4_URL
114 GITLAB_API_V4_PRIVATE_TOKEN
115 GITLAB_API_V4_ACCESS_TOKEN
116 GITLAB_API_V4_RETRIES
117  
118 =cut
119  
120 has env_args => (
121 is => 'lazy',
122 isa => HashRef,
123 );
124 sub _build_env_args {
125 my ($self) = @_;
126  
127 return $self->_filter_args({
128 config_file => $ENV{GITLAB_API_V4_CONFIG_FILE},
129 url => $ENV{GITLAB_API_V4_URL},
130 private_token => $ENV{GITLAB_API_V4_PRIVATE_TOKEN},
131 access_token => $ENV{GITLAB_API_V4_ACCESS_TOKEN},
132 retries => $ENV{GITLAB_API_V4_RETRIES},
133 });
134 }
135  
136 =head2 file_args
137  
138 Returns a hashref of arguments gotten by decoding the JSON in the L</file>.
139  
140 =cut
141  
142 has file_args => (
143 is => 'lazy',
144 isa => HashRef,
145 );
146 sub _build_file_args {
147 my ($self) = @_;
148  
149 my $file = $self->file();
150 return {} if !-r $file;
151  
152 $file = path( $file );
153 $log->debugf( 'Loading configuration for GitLab::API::v4 from: %s', $file->absolute() );
154 my $json = $file->slurp();
155 my $data = decode_json( $json );
156  
157 return $self->_filter_args( $data );
158 }
159  
160 =head2 args
161  
162 Returns a final, combined, hashref of arguments containing everything in
163 L</opt_args>, L</env_args>, and L</file_args>. If there are any duplicate
164 arguments then L</opt_args> has highest precedence, L</env_args> is next, and
165 at the bottom is L</file_args>.
166  
167 =cut
168  
169 sub args {
170 my ($self) = @_;
171  
172 return {
173 %{ $self->file_args() },
174 %{ $self->env_args() },
175 %{ $self->opt_args() },
176 };
177 }
178  
179 =head1 METHODS
180  
181 =head2 get_options
182  
183 =cut
184  
185 sub get_options {
186 my ($self, @extra) = @_;
187  
188 Getopt::Long::Configure(qw(
189 gnu_getopt no_ignore_case
190 ));
191  
192 my $opt_args = {};
193  
194 GetOptions(
195 'config-file=s' => \$opt_args->{config_file},
196 'url=s' => \$opt_args->{url},
197 'private-token=s' => \$opt_args->{private_token},
198 'access-token=s' => \$opt_args->{access_token},
199 'retries=i' => \$opt_args->{retries},
200 @extra,
201 ) or die('Unable to process options!');
202  
203 $opt_args = $self->_filter_args( $opt_args );
204  
205 $self->_set_opt_args( $opt_args );
206  
207 return;
208 }
209  
210 =head2 configure
211  
212 When called this method interactively prompts the user for argument values
213 and then encodes them as JSON and stores them in L</file>. The file will
214 be chmod'ed C<0600> so that only the current user may read or write to the
215 file.
216  
217 =cut
218  
219 sub configure {
220 my ($self) = @_;
221  
222 my $url = prompt(
223 'Full URL to a v4 GitLab API:',
224 '-stdio', '-verbatim',
225 );
226  
227 my $private_token = prompt(
228 'Private Token:',
229 -echo=>'',
230 '-stdio', '-verbatim',
231 );
232  
233 my $access_token = prompt(
234 'Access Token:',
235 -echo=>'',
236 '-stdio', '-verbatim',
237 );
238  
239 my $json = JSON::MaybeXS->new(pretty => 1, canonical => 1)
240 ->encode({
241 $url ? (url=>$url) : (),
242 $private_token ? (private_token=>$private_token) : (),
243 $access_token ? (access_token=>$access_token) : (),
244 });
245  
246 my $file = path( $self->file() );
247 $file->touch();
248 $file->chmod( 0600 );
249 $file->append( {truncate=>1}, $json );
250  
251 $log->infof( 'Configuration for GitLab::API::v4 saved to: %s', $file->absolute() );
252  
253 return;
254 }
255  
256 1;
257 __END__