/bin/bit-rest
Perl | 258 lines | 184 code | 60 blank | 14 comment | 27 complexity | 9eebd60116ec5cebe28beaebbc27d71b MD5 | raw file
- #!/usr/bin/env perl
- # vim: set ft=perl et ts=4 sw=4:
- use 5.10.0;
- use strict;
- use warnings;
- use feature 'switch';
- use Config::Tiny;
- use Getopt::Long qw(:config auto_abbrev gnu_compat require_order);
- use Term::ReadKey;
- use Pod::Usage;
- use BitRest;
- my $VERSION = '0.43';
- # default values for parameters and command line options
- my ($conf, $user, $owner, $pass, $repo, $help, $man, $ver);
- my $color = 'auto';
- # path where to search config file
- my @CONF = (
- ".bit-rest",
- "$ENV{XDG_CONFIG_HOME}/bit-rest/bit-rest.conf",
- "$ENV{HOME}/.bit-rest/bit-rest.conf",
- "$ENV{HOME}/.bit-rest"
- );
- my $CONF; # reference to a hash with read config data
- # command line options
- my %OPTIONS = (
- 'config|C=s' => \$conf,
- 'noconfig|N' => sub { $conf = 'NO' },
- 'color|c:s' => \$color,
- 'user|u:s' => sub { $user = $_[1] || (getpwuid $>)[0] },
- 'owner|o=s' => \$owner,
- 'repository|r=s' => \$repo,
- 'help|h' => \$help,
- 'manual|M' => \$man,
- 'version|V' => \$ver
- );
- #### Subroutines
- # print text message and exit with specified code
- sub end {
- my ($s, $e) = @_;
- local $\ = "\n";
- print $s;
- exit $e // 0;
- }
- # read password from user input without echo
- sub read_pass {
- print "password: ";
- ReadMode 'noecho';
- my $p = ReadLine 0;
- ReadMode 'normal';
- print "\n";
- chomp $p;
- return $p;
- }
- #### Main
- # read configuration file
- if ( $conf && $conf ne 'NO' ) {
- if ( !( $conf && -r $conf ) ) {
- for (@CONF) {
- $conf = $_ if -r $_;
- }
- }
- $CONF = Config::Tiny->read($conf);
- if ( defined $CONF ) {
- $color = $CONF->{config}{color};
- $user = $CONF->{config}{user};
- $pass = $CONF->{config}{pass};
- $repo = $CONF->{config}{repo};
- }
- }
- # parse command line options
- GetOptions(%OPTIONS) or pod2usage( -exitval => 1, -verbose => 1 );
- pod2usage( -exitval => 0, -verbose => 1 ) if $help;
- pod2usage( -exitval => 0, -verbose => 3 ) if $man;
- end("$0 version $VERSION", 0) if $ver;
- # command
- my $cmd = '';
- $cmd = lc shift if $ARGV[0];
- given ($cmd) {
- when ('help') {
- $cmd = shift || '';
- if ( $cmd ~~ /^res(?:ources)?$/ ) {
- local ( $\, $, ) = ( "\n", "\n" );
- print BitRest->resource;
- exit 0;
- }
- BitRest->help($cmd) if BitRest->resource($cmd);
- pod2usage(-exitval => 0, -verbose => 99,
- -sections => 'NAME|SYNOPSIS|OPTIONS|COMMANDS');
- }
- default {
- $cmd = BitRest->alias($cmd)
- || pod2usage(-exitval => 2, -verbose => 0,
- -msg => "$0: invalid command: '$cmd'\n");
- }
- }
- # resource
- my $res_name = shift || pod2usage( -exitval => 3, -verbose => 1 );
- my $res = BitRest->new($res_name);
- end("$0: $BitRest::errstr", 4) unless defined $res;
- # get password from command line option or from user input
- if ( $user ) {
- if ( $user ~~ /^([^:]+):(.*)$/ ) {
- $user = $1;
- $pass = $2;
- }
- elsif ( !$pass ) {
- $pass = read_pass;
- }
- }
- my $id = 0;
- $id = shift if $ARGV[0] && $ARGV[0] ~~ /^[^-]/;
- if ( !$id && ( $cmd eq 'del' || $cmd eq 'put' ) ){
- end("$0: ${res_name}'s id must be specified", 5);
- }
- $res->id($id);
- $res->repo($repo);
- $res->color($color);
- my $r = $res->request(
- $cmd, $CONF->{$res_name} // {},
- $owner || $user, $user, $pass
- );
- end("$0: $BitRest::errstr\n", 6) unless $r;
- __END__
- =head1 NAME
- bit-rest - remote repository management client with command line interface
- =head1 SYNOPSIS
- B<bit-rest> [I<options>] I<command> [I<args ...>]
- =head2 Commands
- B<get>, B<add>, B<put>, B<del>
- =head1 DESCRIPTION
- bit-rest implements Bitbucket REST APIs.
- It's written to get a quick and convinient access to Bitbucket repositories.
- The aim is to work with repo without a browser using command line only.
- =head1 OPTIONS
- =over 4
- =item B<-u>, B<--user> [I<username>[B<:>I<password>]]
- Specify the user name (and password) to use for authentication.
- If there is only the user name (without a colon)
- then a password will be prompted.
- If no parameter given then the current user name will be used.
- =item B<-o>, B<--owner> I<username>
- Specify the user who owns the repository if not the same as B<--user>.
- =item B<-r>, B<--repository> I<repo>
- Specify the repository name.
- =item B<-c>, B<--color> [B<auto>|B<always>|B<never>]
- Colorize the output. The default is B<auto>.
- =item B<-h>, B<--help>
- Display usage information and exit.
- =item B<-M>, B<--man>
- Display full manual text and exit.
- =item B<-V>, B<--version>
- Display version information and exit.
- =back
- =head1 COMMANDS
- =over 4
- =item B<get> I<resource> [I<id>] [I<options ...>]
- Get existing resource by id or list resources.
- =item B<add> I<resource> [I<options ...>]
- Create new resource.
- =item B<put> I<resource> [I<id>] [I<options ...>]
- Update existing resource or create new.
- =item B<del> I<resource> [I<id>] [I<options ...>]
- Delete resource.
- =item B<help> [I<command>|I<resource>]
- Display help information about command or resource.
- With no parameter given, the usage information will be displayed.
- =item B<help resources>
- List available resources.
- =back
- =head1 SEE ALSO
- L<Bitbucket REST APIs|https://confluence.atlassian.com/display/BITBUCKET/Using+the+bitbucket+REST+APIs>.
- =head1 SOURCE
- The source code repository for bit-rest can be found at
- L<https://bitbucket.org/vvp/bit-rest>.
- =head1 BUGS
- See the repository issue tracker at
- L<https://bitbucket.org/vvp/bit-rest/issues>
- to report and view bugs.
- =head1 AUTHOR
- vvp <vvp.psu[at]gmail.com>
- =head1 LICENSE AND COPYRIGHT
- Copyright (c) 2012 vvp (vvp.psu[at]gmail.com).
- All rights reserved.
- This program is free software; you can redstribute it and/or modify it under
- the same terms as Perl itself. See L<perlartistic>. This program is
- distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
- without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
- PARTICULAR PURPOSE.