PageRenderTime 26ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/bin/bit-rest

https://bitbucket.org/vvp/bit-rest
Perl | 258 lines | 184 code | 60 blank | 14 comment | 27 complexity | 9eebd60116ec5cebe28beaebbc27d71b MD5 | raw file
  1. #!/usr/bin/env perl
  2. # vim: set ft=perl et ts=4 sw=4:
  3. use 5.10.0;
  4. use strict;
  5. use warnings;
  6. use feature 'switch';
  7. use Config::Tiny;
  8. use Getopt::Long qw(:config auto_abbrev gnu_compat require_order);
  9. use Term::ReadKey;
  10. use Pod::Usage;
  11. use BitRest;
  12. my $VERSION = '0.43';
  13. # default values for parameters and command line options
  14. my ($conf, $user, $owner, $pass, $repo, $help, $man, $ver);
  15. my $color = 'auto';
  16. # path where to search config file
  17. my @CONF = (
  18. ".bit-rest",
  19. "$ENV{XDG_CONFIG_HOME}/bit-rest/bit-rest.conf",
  20. "$ENV{HOME}/.bit-rest/bit-rest.conf",
  21. "$ENV{HOME}/.bit-rest"
  22. );
  23. my $CONF; # reference to a hash with read config data
  24. # command line options
  25. my %OPTIONS = (
  26. 'config|C=s' => \$conf,
  27. 'noconfig|N' => sub { $conf = 'NO' },
  28. 'color|c:s' => \$color,
  29. 'user|u:s' => sub { $user = $_[1] || (getpwuid $>)[0] },
  30. 'owner|o=s' => \$owner,
  31. 'repository|r=s' => \$repo,
  32. 'help|h' => \$help,
  33. 'manual|M' => \$man,
  34. 'version|V' => \$ver
  35. );
  36. #### Subroutines
  37. # print text message and exit with specified code
  38. sub end {
  39. my ($s, $e) = @_;
  40. local $\ = "\n";
  41. print $s;
  42. exit $e // 0;
  43. }
  44. # read password from user input without echo
  45. sub read_pass {
  46. print "password: ";
  47. ReadMode 'noecho';
  48. my $p = ReadLine 0;
  49. ReadMode 'normal';
  50. print "\n";
  51. chomp $p;
  52. return $p;
  53. }
  54. #### Main
  55. # read configuration file
  56. if ( $conf && $conf ne 'NO' ) {
  57. if ( !( $conf && -r $conf ) ) {
  58. for (@CONF) {
  59. $conf = $_ if -r $_;
  60. }
  61. }
  62. $CONF = Config::Tiny->read($conf);
  63. if ( defined $CONF ) {
  64. $color = $CONF->{config}{color};
  65. $user = $CONF->{config}{user};
  66. $pass = $CONF->{config}{pass};
  67. $repo = $CONF->{config}{repo};
  68. }
  69. }
  70. # parse command line options
  71. GetOptions(%OPTIONS) or pod2usage( -exitval => 1, -verbose => 1 );
  72. pod2usage( -exitval => 0, -verbose => 1 ) if $help;
  73. pod2usage( -exitval => 0, -verbose => 3 ) if $man;
  74. end("$0 version $VERSION", 0) if $ver;
  75. # command
  76. my $cmd = '';
  77. $cmd = lc shift if $ARGV[0];
  78. given ($cmd) {
  79. when ('help') {
  80. $cmd = shift || '';
  81. if ( $cmd ~~ /^res(?:ources)?$/ ) {
  82. local ( $\, $, ) = ( "\n", "\n" );
  83. print BitRest->resource;
  84. exit 0;
  85. }
  86. BitRest->help($cmd) if BitRest->resource($cmd);
  87. pod2usage(-exitval => 0, -verbose => 99,
  88. -sections => 'NAME|SYNOPSIS|OPTIONS|COMMANDS');
  89. }
  90. default {
  91. $cmd = BitRest->alias($cmd)
  92. || pod2usage(-exitval => 2, -verbose => 0,
  93. -msg => "$0: invalid command: '$cmd'\n");
  94. }
  95. }
  96. # resource
  97. my $res_name = shift || pod2usage( -exitval => 3, -verbose => 1 );
  98. my $res = BitRest->new($res_name);
  99. end("$0: $BitRest::errstr", 4) unless defined $res;
  100. # get password from command line option or from user input
  101. if ( $user ) {
  102. if ( $user ~~ /^([^:]+):(.*)$/ ) {
  103. $user = $1;
  104. $pass = $2;
  105. }
  106. elsif ( !$pass ) {
  107. $pass = read_pass;
  108. }
  109. }
  110. my $id = 0;
  111. $id = shift if $ARGV[0] && $ARGV[0] ~~ /^[^-]/;
  112. if ( !$id && ( $cmd eq 'del' || $cmd eq 'put' ) ){
  113. end("$0: ${res_name}'s id must be specified", 5);
  114. }
  115. $res->id($id);
  116. $res->repo($repo);
  117. $res->color($color);
  118. my $r = $res->request(
  119. $cmd, $CONF->{$res_name} // {},
  120. $owner || $user, $user, $pass
  121. );
  122. end("$0: $BitRest::errstr\n", 6) unless $r;
  123. __END__
  124. =head1 NAME
  125. bit-rest - remote repository management client with command line interface
  126. =head1 SYNOPSIS
  127. B<bit-rest> [I<options>] I<command> [I<args ...>]
  128. =head2 Commands
  129. B<get>, B<add>, B<put>, B<del>
  130. =head1 DESCRIPTION
  131. bit-rest implements Bitbucket REST APIs.
  132. It's written to get a quick and convinient access to Bitbucket repositories.
  133. The aim is to work with repo without a browser using command line only.
  134. =head1 OPTIONS
  135. =over 4
  136. =item B<-u>, B<--user> [I<username>[B<:>I<password>]]
  137. Specify the user name (and password) to use for authentication.
  138. If there is only the user name (without a colon)
  139. then a password will be prompted.
  140. If no parameter given then the current user name will be used.
  141. =item B<-o>, B<--owner> I<username>
  142. Specify the user who owns the repository if not the same as B<--user>.
  143. =item B<-r>, B<--repository> I<repo>
  144. Specify the repository name.
  145. =item B<-c>, B<--color> [B<auto>|B<always>|B<never>]
  146. Colorize the output. The default is B<auto>.
  147. =item B<-h>, B<--help>
  148. Display usage information and exit.
  149. =item B<-M>, B<--man>
  150. Display full manual text and exit.
  151. =item B<-V>, B<--version>
  152. Display version information and exit.
  153. =back
  154. =head1 COMMANDS
  155. =over 4
  156. =item B<get> I<resource> [I<id>] [I<options ...>]
  157. Get existing resource by id or list resources.
  158. =item B<add> I<resource> [I<options ...>]
  159. Create new resource.
  160. =item B<put> I<resource> [I<id>] [I<options ...>]
  161. Update existing resource or create new.
  162. =item B<del> I<resource> [I<id>] [I<options ...>]
  163. Delete resource.
  164. =item B<help> [I<command>|I<resource>]
  165. Display help information about command or resource.
  166. With no parameter given, the usage information will be displayed.
  167. =item B<help resources>
  168. List available resources.
  169. =back
  170. =head1 SEE ALSO
  171. L<Bitbucket REST APIs|https://confluence.atlassian.com/display/BITBUCKET/Using+the+bitbucket+REST+APIs>.
  172. =head1 SOURCE
  173. The source code repository for bit-rest can be found at
  174. L<https://bitbucket.org/vvp/bit-rest>.
  175. =head1 BUGS
  176. See the repository issue tracker at
  177. L<https://bitbucket.org/vvp/bit-rest/issues>
  178. to report and view bugs.
  179. =head1 AUTHOR
  180. vvp <vvp.psu[at]gmail.com>
  181. =head1 LICENSE AND COPYRIGHT
  182. Copyright (c) 2012 vvp (vvp.psu[at]gmail.com).
  183. All rights reserved.
  184. This program is free software; you can redstribute it and/or modify it under
  185. the same terms as Perl itself. See L<perlartistic>. This program is
  186. distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  187. without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
  188. PARTICULAR PURPOSE.