Shellex.pm 943 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package SimplyGit::Shellex;
  2. use strict;
  3. use warnings;
  4. use Log::Log4perl qw(:easy);
  5. use Exporter qw(import);
  6. our @EXPORT_OK = qw(shellex findBin);
  7. sub shellex {
  8. my $cmd = shift;
  9. my $logger = shift;
  10. if ( defined $logger && $logger ne '' ) {
  11. $logger->info("Running: $cmd 2>&1");
  12. }
  13. my $output = `$cmd 2>&1`;
  14. my $rc = $?;
  15. if ( defined $logger && $logger ne '' ) {
  16. $logger->info("Returned: $rc");
  17. }
  18. if ( $rc != 0 ) {
  19. if ( defined $logger && $logger ne '' ) {
  20. $logger->error("$cmd returned non-zero: $rc");
  21. }
  22. print "$cmd returned non-zero: $rc, exiting\n";
  23. exit 1;
  24. }
  25. return $output;
  26. }
  27. sub findBin {
  28. my $cmd = shift;
  29. my $logger = shift;
  30. my $binPath = shellex("which $cmd",$logger);
  31. my $rc = $?;
  32. chomp $binPath;
  33. if ( $rc != 0 ) {
  34. if ( defined $logger && $logger ne '' ) {
  35. $logger->error("Couldn't find a path for $cmd, exiting...");
  36. }
  37. exit 1;
  38. } else {
  39. return $binPath;
  40. }
  41. }
  42. 1;