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 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
|
#!/usr/bin/env perl
use strictures 2;
use GitLab::API::v4::Config;
use GitLab::API::v4::Constants qw( :all );
use GitLab::API::v4;
use JSON::MaybeXS;
use Log::Any qw( $log );
use Log::Any::Adapter::Screen;
use Log::Any::Adapter;
use Pod::Usage qw( pod2usage );
use Try::Tiny;
if (!@ARGV) {
print "USAGE: gitlab-api-v4 help\n";
exit 0;
}
my $config = GitLab::API::v4::Config->new();
$config->get_options(
'a|all' => \my $all,
'p|pretty' => \my $pretty,
'c|canonical' => \my $canonical,
'h|help' => \my $help,
'v|verbose' => \my $verbose,
'q|quiet' => \my $quiet,
);
if ($help or (@ARGV and $ARGV[0] eq 'help')) {
pod2usage( -verbose => 2 );
exit 0;
}
if (@ARGV and $ARGV[0] eq 'configure') {
$config->configure();
exit 0;
}
my $min_level = 'info';
$min_level = 'trace' if $verbose;
$min_level = 'error' if $quiet;
Log::Any::Adapter->set(
'Screen',
min_level => $min_level,
stderr => 1,
);
my $method = shift @ARGV;
die "ERROR: No API method specified.\n" if !$method;
my $orig_method = $method;
$method =~ s{-}{_}g;
die "ERROR: Unknown API method \"$orig_method\".\n"
if !GitLab::API::v4->can( $method );
my @args;
while (@ARGV and $ARGV[0] !~ m{:}) {
my $arg = shift @ARGV;
next if $arg eq '--';
push @args, $arg;
}
my $aliases = {
access_level => {
guest => $GITLAB_ACCESS_LEVEL_GUEST,
reporter => $GITLAB_ACCESS_LEVEL_REPORTER,
developer => $GITLAB_ACCESS_LEVEL_DEVELOPER,
master => $GITLAB_ACCESS_LEVEL_MASTER,
owner => $GITLAB_ACCESS_LEVEL_OWNER,
},
};
my $params = {};
while (@ARGV) {
my $arg = shift @ARGV;
next if $arg eq '--';
if ($arg =~ m{^([^:]+):(.*)$}s) {
my ($key, $value) = ($1, $2);
$key =~ s{-}{_}g;
if ($aliases->{$key} and exists $aliases->{$key}->{$value}) {
$value = $aliases->{$key}->{$value};
}
$params->{$key} = $value;
}
else {
die "ERROR: Invalid API parameter \"$arg\".\n";
}
}
# Make sure we don't leak tokens in the logs.
my $debug_config = { %{ $config->args() } };
$debug_config->{private_token} = 'xxxx' if $debug_config->{private_token};
$debug_config->{access_token} = 'xxxx' if $debug_config->{access_token};
$log->debug('config: ' . encode_json($debug_config));
$log->debug("method: $method");
$log->debug('arguments: ' . encode_json(\@args));
$log->debug('params: ' . encode_json($params));
my $api = GitLab::API::v4->new( $config->args() );
if ($all) {
unshift @args, $method;
$method = 'paginator';
}
my $data = $api->$method(
@args,
%$params ? $params : (),
);
$data = $data->all() if $all;
binmode STDOUT, ':utf8';
my $json = JSON::MaybeXS->new(allow_nonref => 1);
$json->pretty() if $pretty;
$json->canonical() if $canonical;
print $json->encode( $data );
__END__
=encoding utf8
=head1 NAME
gitlab-api-v4 - Command line interface to the GitLab API v4.
=head1 SYNOPSIS
# Generally:
gitlab-api-v4 [<options>] <method> [<arg> ...] [<param>:<value> ...]
# List all groups:
gitlab-api-v4 groups
# List information about a project:
gitlab-api-v4 project <project_id>
# Create an admin user:
gitlab-api-v4 create-user \
username:foo \
password:xxxxxxxx \
email:user@example.com \
"name:Foo Smith" \
admin:1
=head1 CONFIGURING
You may configure this module with environment variables, command line options,
and a configuration file. To setup the configuration file run:
gitlab-api-v4 configure
This will ask several interactive questions to help you configure this script.
The information, which may include GitLab authentication tokens, is stored in
C<~/.gitlab-api-v4.json>.
Read more at L<GitLab::API::v4::Config>.
=head1 OPTIONS
=head2 url
--url=<url>
Sets L<GitLab::API::v4/url>.
=head2 access-token
--access-token=<token>
Sets L<GitLab::API::v4/access_token>.
=head2 private-token
--private-token=<token>
Sets L<GitLab::API::v4/private_token>.
=head2 retries
--retries=<count>
Sets L<GitLab::API::v4/retries>.
=head2 all
--all
-a
Retrieves all results when the results would normally be paged.
See L<GitLab::API::v4::Paginator/all> for details.
=head2 pretty
--pretty
-p
Enables the L<JSON::PP/pretty> feature.
=head2 canonical
--canonical
-c
Enables the L<JSON::PP/canonical> feature.
=head1 API METHOD
<method>
The API method to call - one of the methods documented in
L<GitLab::API::v4/API METHODS>.
=head1 API ARGUMENTS
<arg> ...
Any arguments that the L</API METHOD> requires.
=head1 API PARAMETERS
<param>:<value> ...
Any parameters that the L</API METHOD> accepts.
=head2 access-level
access-level:guest
access-level:reporter
access-level:developer
access-level:master
access-level:owner
There are mappings setup for the various C<access-level> parameters
so that you can, for example, specify C<access-level:guest> and it
will be automatically converted to C<access-level:10>.
=head1 SUPPORT
See L<GitLab::API::v4/SUPPORT>.
=head1 AUTHORS
See L<GitLab::API::v4/AUTHORS>.
=head1 COPYRIGHT AND LICENSE
See L<GitLab::API::v4/COPYRIGHT AND LICENSE>.
=cut
|