Browse Source

Added a few more knocking features

spesk1 4 years ago
parent
commit
2c46a5ccdd
4 changed files with 84 additions and 46 deletions
  1. 45 40
      README.md
  2. 1 0
      example.config
  3. 10 1
      lib/SimplyGit/Git.pm
  4. 28 5
      sg

+ 45 - 40
README.md

@@ -4,49 +4,53 @@ Project to abstract some of the weirder git operations or things that I forget.
 Mainly writing to learn more about Git and Perl, unlikely to be widely useful.
 Mainly writing to learn more about Git and Perl, unlikely to be widely useful.
 
 
 ```
 ```
-simply-git
-Usage:
-        --view
-        Display git status of files and other information
-
-        --dump-config
-        Dump .git/config to STDOUT. Not really useful but exposed for testing of reading config into internal data structure
-
-        --push-all [--commit-msg]
-        Push all untracked and modified files
-                * Can be used with interactive mode
-                * Can provide a commit msg with --commit-msg (otherwise a generic will be provided)
-
-        --interactive
-        Enable interactive mode with supported opts
-
-        --reset-from-master
-        Reset all current changes so that the file tree matches origin master
-
-        --reset-from-upstream [ --upstream-url ]
-        If upstream is defined will reset local branch to match upstream ( does not push changes to origin )
-                * Assumes you have an upstream configured
-                * Pass SSH/HTTPS URL to --upstream-url to add an upstream
-
-        --configure-local-user [--user,--email]
-        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
-                * See example.config
-
-        --knock
-        Will try and knock the defined git server at the defined ports before any operation
-                * See example.config
-
-        --knock-clone
-        Will try and knock the defined git server and clone the provided repo
-                * Will not check if you're in a git dir
+simply-git                                                                                                                                                                            
+Usage:                                                                                                                                                                                
+        --view                                                                                                                                                                        
+        Display git status of files and other information                                                                                                                             
+                                                                                                                                                                                      
+        --dump-config                                                                                                                                                                 
+        Dump .git/config to STDOUT. Not really useful but exposed for testing of reading config into internal data structure                                                          
+                                                                                                                                                                                      
+        --push-all [--commit-msg]                                                                                                                                                     
+        Push all untracked and modified files                                                                                                                                         
+                * Can be used with interactive mode                                                                                                                                   
+                * Can provide a commit msg with --commit-msg (otherwise a generic will be provided)                                                                                   
+                                                                                                                                                                                      
+        --interactive                                                                                                                                                                 
+        Enable interactive mode with supported opts                                                                                                                                   
+                                                                                                                                                                                      
+        --reset-from-master                                                                                                                                                           
+        Reset all current changes so that the file tree matches origin master                                                                                                         
+                                                                                                                                                                                      
+        --reset-from-upstream [ --upstream-url ]                                                                                                                                      
+        If upstream is defined will reset local branch to match upstream ( does not push changes to origin )                                                                          
+                * Assumes you have an upstream configured                                                                                                                             
+                * Pass SSH/HTTPS URL to --upstream-url to add an upstream                                                                                                             
+                                                                                                                                                                                      
+        --configure-local-user [--user,--email]                                                                                                                                       
+        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                                                                                                              
+                * See example.config                                                                                                                                                  
+                                                                                                                                                                                      
+        --knock                                                                                                                                                                       
+        Will try and knock the defined git server at the defined ports before any operation                                                                                           
+                * See example.config                                                                                                                                                  
+                * Can pass this by itself to perform a knock and exit                                                                                                                 
+                                                                                                                                                                                      
+        --knock-clone                                                                                                                                                                 
+        Will try and knock the defined git server and clone the provided repo                                                                                                         
+                * Will not check if you're in a git dir                                                                                                                               
+                                                                                                                                                                                      
+        --knock-pull                                                                                                                                                                  
+        Will try and knock the defined git server and git pull  
 ```
 ```
 
 
 ## Deps
 ## Deps
-* perl, Log4Perl, which, git
+* perl, Log4Perl, which, git, nmap
 
 
 ## Installation
 ## Installation
 Can use provided install.sh to install
 Can use provided install.sh to install
@@ -56,3 +60,4 @@ Can use provided install.sh to install
 
 
 ## TODO - Stuff to fix:
 ## TODO - Stuff to fix:
 * multipule TODO:'s in various files
 * multipule TODO:'s in various files
+* Code is getting messy, see TODOs

+ 1 - 0
example.config

@@ -5,4 +5,5 @@ user.email = "spesk@pm.me"
 
 
 # Use if your git server requires port knocking
 # Use if your git server requires port knocking
 Knock = "true"
 Knock = "true"
+knock.target = "mygitserver.com"
 ports = [ "1111", "2222" ]
 ports = [ "1111", "2222" ]

+ 10 - 1
lib/SimplyGit/Git.pm

@@ -9,7 +9,7 @@ our @EXPORT_OK = qw(
 	readConfig getStatus returnState addFiles 
 	readConfig getStatus returnState addFiles 
 	commitChanges pushChanges stashAndReset resetFromUpstream 
 	commitChanges pushChanges stashAndReset resetFromUpstream 
 	updateGitIgnore appendRepoUserConfig parseSGConfig 
 	updateGitIgnore appendRepoUserConfig parseSGConfig 
-	warnOnUser basicClone
+	warnOnUser basicClone basicPull
 );
 );
 
 
 # TODO: Add info/debug logging for all subroutines
 # TODO: Add info/debug logging for all subroutines
@@ -337,3 +337,12 @@ sub basicClone($$) {
 	print "Successfully cloned $cloneTarget\n";
 	print "Successfully cloned $cloneTarget\n";
 
 
 }
 }
+
+sub basicPull($) {
+
+	my $logger = shift;
+	my $gitCmd = findBin("git",$logger);
+	my $gitPullReturn = shellex("$gitCmd pull",$logger);
+	print "git pull returned:\n$gitPullReturn\n";
+
+}

+ 28 - 5
sg

@@ -12,7 +12,7 @@ use SimplyGit::Git qw(
 	readConfig getStatus returnState addFiles 
 	readConfig getStatus returnState addFiles 
 	commitChanges pushChanges stashAndReset resetFromUpstream 
 	commitChanges pushChanges stashAndReset resetFromUpstream 
 	updateGitIgnore appendRepoUserConfig parseSGConfig 
 	updateGitIgnore appendRepoUserConfig parseSGConfig 
-	warnOnUser basicClone
+	warnOnUser basicClone basicPull
 );
 );
 
 
 sub initSG($) {
 sub initSG($) {
@@ -82,10 +82,11 @@ GetOptions(
 	'knock',
 	'knock',
 	'knock-clone=s',
 	'knock-clone=s',
 	'help',
 	'help',
+	'knock-pull',
 );
 );
 
 
 # TODO: This should maybe be more robust?
 # TODO: This should maybe be more robust?
-if ( ! -d ".git" && ! defined $args{'knock-clone'} && ! defined $args{'help'} ) {
+if ( ! -d ".git" && ( ! defined $args{'knock-clone'} && ! defined $args{'knock'} && ! defined $args{'help'} ) ) {
 	print "Not a git dir, exiting...\n";
 	print "Not a git dir, exiting...\n";
 	exit 1;
 	exit 1;
 }
 }
@@ -128,11 +129,15 @@ Usage:
 	--knock
 	--knock
 	Will try and knock the defined git server at the defined ports before any operation
 	Will try and knock the defined git server at the defined ports before any operation
 		* See example.config
 		* See example.config
+		* Can pass this by itself to perform a knock and exit
 	
 	
 	--knock-clone 
 	--knock-clone 
 	Will try and knock the defined git server and clone the provided repo
 	Will try and knock the defined git server and clone the provided repo
 		* Will not check if you're in a git dir
 		* Will not check if you're in a git dir
 
 
+	--knock-pull
+	Will try and knock the defined git server and git pull
+
 EOF
 EOF
 ;
 ;
 
 
@@ -225,7 +230,15 @@ sub parseArgs {
 
 
 	if ( defined $args{'knock-clone'} ) {
 	if ( defined $args{'knock-clone'} ) {
 		if ( scalar keys %args > 2 ) {
 		if ( scalar keys %args > 2 ) {
-			print "Knock clone accepts no other args\n";
+			print "--knock-clone accepts no other args\n";
+			exit 1;
+		}
+	}
+	
+	if ( defined $args{'knock-pull'} ) {
+		if ( scalar keys %args > 2 ) {
+			print "--knock-pull accepts no other args\n";
+			exit 1;
 		}
 		}
 	}
 	}
 
 
@@ -233,17 +246,22 @@ sub parseArgs {
 
 
 parseArgs();
 parseArgs();
 my %sgConfig = parseSGConfig($args{'config-file'},$logger);
 my %sgConfig = parseSGConfig($args{'config-file'},$logger);
-if ( defined $sgConfig{'UserWarn'} ) {
+if ( defined $sgConfig{'UserWarn'}  && -d ".git" ) {
 	warnOnUser($sgConfig{'user.name'},$sgConfig{'user.email'},$logger);
 	warnOnUser($sgConfig{'user.name'},$sgConfig{'user.email'},$logger);
 }
 }
 
 
 sub knock() {
 sub knock() {
-	if ( defined $sgConfig{'Knock'} && ( defined $args{'knock'} || defined $args{'knock-clone'} ) ) {
+	if ( defined $sgConfig{'Knock'} && ( defined $args{'knock'} || defined $args{'knock-clone'} || defined $args{'knock-pull'} ) ) {
 		print "Knocking...\n";
 		print "Knocking...\n";
 		knocker($sgConfig{'knock.target'},$sgConfig{'ports'},$logger);
 		knocker($sgConfig{'knock.target'},$sgConfig{'ports'},$logger);
 	}
 	}
 }
 }
 
 
+if ( defined $args{'knock'} && scalar keys %args == 2 ) {
+	print "Just knocking then exiting...\n";
+	knock();
+	exit 1;
+}
 
 
 # TODO: This sub could be more concise with a sub to print array refs
 # TODO: This sub could be more concise with a sub to print array refs
 if ( defined $args{'view'} ) {
 if ( defined $args{'view'} ) {
@@ -440,3 +458,8 @@ if ( defined $args{'knock-clone'} ) {
 	knock();
 	knock();
 	basicClone($args{'knock-clone'},$logger);
 	basicClone($args{'knock-clone'},$logger);
 }
 }
+
+if ( defined $args{'knock-pull'} ) {
+	knock();
+	basicPull($logger);
+}