Browse Source

Split of Shellex.pm from simply-git

spesk1 4 years ago
parent
commit
d837bad3d3
3 changed files with 69 additions and 0 deletions
  1. 5 0
      README.md
  2. 11 0
      install.sh
  3. 53 0
      lib/Shellex/Shellex.pm

+ 5 - 0
README.md

@@ -2,3 +2,8 @@
 
 General purpose module that I use for shelling out in a way that I like in Perl.
 May or may not be useful to others.
+
+## Depends on ##
+* which
+* Log4Perl Perl lib
+* Assumes your shell is bash

+ 11 - 0
install.sh

@@ -0,0 +1,11 @@
+#!/bin/bash
+
+libDir="/usr/local/lib/Shellex/"
+
+if [ ! -d $libDir ]; then
+	echo "Making $libDir"
+	sudo mkdir -p $libDir
+fi
+
+echo "Calling sudo to copy libs"
+sudo cp ./lib/Shellex/* $libDir

+ 53 - 0
lib/Shellex/Shellex.pm

@@ -0,0 +1,53 @@
+package Shellex::Shellex;
+use strict;
+use warnings;
+use Log::Log4perl qw(:easy);
+use Exporter qw(import);
+our @EXPORT_OK = qw(shellex findBin);
+
+sub shellex {
+
+        my $cmd = shift;
+	my $logger = shift;
+	if ( defined $logger && $logger ne '' ) {
+		$logger->info("Running: $cmd 2>&1");
+	}
+
+        my $output = `$cmd 2>&1`;
+	chomp $output;
+	my $rc = $?;
+	if ( defined $logger && $logger ne '' ) {
+		$logger->info("Returned: $rc");
+	}
+
+	if ( $rc != 0 ) {
+		if ( defined $logger && $logger ne '' ) {
+			$logger->error("$cmd returned non-zero: $rc");
+		}
+
+		print "$cmd returned non-zero: $rc\n";
+		exit 1;
+	}
+
+        return $output;
+
+}
+
+sub findBin {
+
+	my $cmd = shift;
+	my $logger = shift;
+	my $binPath = shellex("which $cmd",$logger);
+	my $rc = $?;
+	chomp $binPath;
+	if ( $rc != 0 ) {
+		if ( defined $logger && $logger ne '' ) {
+			$logger->error("Couldn't find a path for $cmd, exiting...");
+		}
+		exit 1;
+	} else {
+		return $binPath;
+	}
+
+}
+1;