Browse Source

Added config file fuctionality that can be expanded and warning if youre not using the local user you have configured in the config file

spesk1 4 years ago
parent
commit
62d64d68f6
6 changed files with 78 additions and 14 deletions
  1. 3 5
      README.md
  2. 4 0
      example.config
  3. 1 1
      install.sh
  4. 44 1
      lib/SimplyGit/Git.pm
  5. 1 0
      lib/SimplyGit/Shellex.pm
  6. 25 7
      sg

+ 3 - 5
README.md

@@ -33,10 +33,8 @@ Usage:
                 * Can be used with interactive mode
 ```
 
-# TODO:
+## TODO - Features:
+* Warn about upstream updates
 
-## Features:
-* No more ideas. Open to suggestions.
-
-## Stuff to fix:
+## TODO - Stuff to fix:
 * multipule TODO:'s in various files

+ 4 - 0
example.config

@@ -0,0 +1,4 @@
+# Warn user if following user is not set in .git/config or global user is not as below
+UserWarn = "true"
+user.name = "spesk1"
+user.email = "spesk@pm.me"

+ 1 - 1
install.sh

@@ -10,7 +10,7 @@ fi
 
 if [ ! -d $libDir ]; then
 	echo "Making $libDir"
-	mkdir -p $libDir
+	sudo mkdir -p $libDir
 fi
 
 echo "Copying bin"

+ 44 - 1
lib/SimplyGit/Git.pm

@@ -5,7 +5,7 @@ use Log::Log4perl qw(:easy);
 use lib ".";
 use SimplyGit::Shellex qw(shellex findBin);
 use Exporter qw(import);
-our @EXPORT_OK = qw(readConfig getStatus returnState addFiles commitChanges pushChanges stashAndReset resetFromUpstream updateGitIgnore appendRepoUserConfig);
+our @EXPORT_OK = qw(readConfig getStatus returnState addFiles commitChanges pushChanges stashAndReset resetFromUpstream updateGitIgnore appendRepoUserConfig parseSGConfig warnOnUser);
 
 # TODO: Add info/debug logging for all subroutines
 
@@ -20,6 +20,49 @@ sub checkPath($$) {
 
 }
 
+sub warnOnUser($$$) {
+
+	my $user = shift;
+	my $email = shift;
+	my $logger = shift;
+
+	my $gitCmd = findBin("git",$logger);
+	my $configuredUser = shellex("$gitCmd config --get user.name",$logger);
+	my $configuredEmail = shellex("$gitCmd config --get user.email",$logger);
+
+	if ( $configuredUser ne $user || $configuredEmail ne $email ) {
+		print "***************\n";
+		print "Your configured user/email don't match what you declared in the config file!\n";
+		print "Desired User: $user\nConfigured User: $configuredUser\nDesired Email: $email\nConfigured Email: $configuredEmail\n";
+		print "***************\n";
+	}
+
+}
+
+sub parseSGConfig($$) {
+
+	my $config = shift;
+	my $logger = shift;
+	if ( ! -e $config ) {
+		$logger->error("$config doesn't look like a regular file, exiting...");
+		exit 1;
+	}
+	my $catCmd = findBin("cat",$logger);
+	my @configLines = split("\n",shellex("$catCmd $config",$logger));
+	my %configHash;
+	foreach my $line ( @configLines ) {
+		chomp $line;
+		if ( $line =~ m/^(.*)\ =\ "(.*)"$/ ) {
+			$configHash{$1} = $2;
+		}
+	}
+
+	if ( defined $configHash{'UserWarn'} ) {
+		warnOnUser($configHash{'user.name'},$configHash{'user.email'},$logger);
+	}
+
+}
+
 sub returnConfigPath($$) {
 
 	my $path = shift;

+ 1 - 0
lib/SimplyGit/Shellex.pm

@@ -14,6 +14,7 @@ sub shellex {
 	}
 
         my $output = `$cmd 2>&1`;
+	chomp $output;
 	my $rc = $?;
 	if ( defined $logger && $logger ne '' ) {
 		$logger->info("Returned: $rc");

+ 25 - 7
sg

@@ -8,7 +8,7 @@ use Log::Log4perl qw(:easy);
 # TODO: This needs to be scoped properly
 use lib "/usr/local/lib";
 use SimplyGit::Shellex qw(shellex findBin);
-use SimplyGit::Git qw(readConfig getStatus returnState addFiles commitChanges pushChanges stashAndReset resetFromUpstream updateGitIgnore appendRepoUserConfig);
+use SimplyGit::Git qw(readConfig getStatus returnState addFiles commitChanges pushChanges stashAndReset resetFromUpstream updateGitIgnore appendRepoUserConfig parseSGConfig warnOnUser);
 
 # TODO: This should maybe be more robust?
 if ( ! -d ".git" ) {
@@ -23,6 +23,7 @@ sub initSG($) {
 	chomp $homeDir;
 	my $path = $homeDir . "/" . $sgDir;
 	my $logFile = $homeDir . "/" . $sgDir . "/" . "sgLog.txt";
+	my $configFile = $homeDir . "/" . $sgDir . "/" . "sg.config";
 	if ( ! -d $path ) {
 		print "Creating $path\n";
 		shellex("mkdir $path");
@@ -33,19 +34,29 @@ sub initSG($) {
 		shellex("touch $logFile");
 	}
 
-	return ( $path, $logFile );
+	if ( ! -f $configFile ) {
+		print "Creating $logFile\n";
+		shellex("touch $logFile");
+	}
+
+	return ( $path, $logFile, $configFile );
 	
 }
 
-my ( $sgPath, $sgLogFile ) = initSG(".sg");
+my ( $sgPath, $sgLogFile, $sgConfigFile ) = initSG(".sg");
 sub getLogName { return $sgLogFile; };
 my $log_conf = q(
-        log4perl.rootLogger              = ERROR, LOG1
+        log4perl.rootLogger              = ERROR, LOG1, screen
         log4perl.appender.LOG1           = Log::Log4perl::Appender::File
         log4perl.appender.LOG1.filename  = sub { getLogName(); }
         log4perl.appender.LOG1.mode      = append
         log4perl.appender.LOG1.layout    = Log::Log4perl::Layout::PatternLayout
-        log4perl.appender.LOG1.layout.ConversionPattern = %d %p %m %n
+        log4perl.appender.LOG1.layout.ConversionPattern = %d %p >> %m %n
+
+	log4perl.appender.screen = Log::Log4perl::Appender::Screen
+	log4perl.appender.screen.stderr = 0
+	log4perl.appender.screen.layout = PatternLayout
+	log4perl.appender.screen.layout.ConversionPattern = %d %p >> %m %n
 );
 
 Log::Log4perl::init(\$log_conf);
@@ -68,6 +79,7 @@ GetOptions(
 	'configure-local-user',
 	'user=s',
 	'email=s',
+	'config-file=s',
 );
 
 sub printHelp {
@@ -101,6 +113,9 @@ Usage:
 	Configure local git user
 		* Can be used with interactive mode
 
+	--config-file
+	Default is ~/.sg/sg.config, can use this opt to use another file
+
 EOF
 ;
 
@@ -182,11 +197,14 @@ sub parseArgs {
 		}
 	}
 
+	if ( ! defined $args{'config-file'} ) {
+		$args{'config-file'} = $sgConfigFile;
+	}
+
 }
 
 parseArgs();
-#print "Args parsed successfully\n";
-#exit 0;
+parseSGConfig($args{'config-file'},$logger);
 
 # TODO: This sub could be more concise with a sub to print array refs
 if ( defined $args{'view'} ) {