Thread (34 messages) flat view 34 messages, 2 authors, 2016-06-15
DORMANTno replies

Revision v4 of 3 in this series.

Revisions (3)
  1. v2 [diff vs current]
  2. v4 current
  3. v6 [diff vs current]

[PATCH v4 21/31] git-remote-mediawiki: Put long code into a subroutine

From: Célestin Matte <hidden>
Date: 2016-06-15 22:57:42
Subsystem: the rest · Maintainer: Linus Torvalds

Signed-off-by: Célestin Matte <redacted>
Signed-off-by: Matthieu Moy <redacted>
---
 contrib/mw-to-git/git-remote-mediawiki.perl |   50 +++++++++++++++++----------
 1 file changed, 31 insertions(+), 19 deletions(-)
diff --git a/contrib/mw-to-git/git-remote-mediawiki.perl b/contrib/mw-to-git/git-remote-mediawiki.perl
index 7acbec8..da022af 100755
--- a/contrib/mw-to-git/git-remote-mediawiki.perl
+++ b/contrib/mw-to-git/git-remote-mediawiki.perl
@@ -126,28 +126,13 @@ $wiki_name =~ s{[^/]*://}{};
 $wiki_name =~ s/^.*@//;
 
 # Commands parser
-my @cmd;
+my @cmds;
 while (<STDIN>) {
 	chomp;
-	@cmd = split(/ /);
-	if (defined($cmd[0])) {
+	@cmds = split(/ /);
+	if (defined($cmds[0])) {
 		# Line not blank
-		if ($cmd[0] eq "capabilities") {
-			die("Too many arguments for capabilities\n") if (defined($cmd[1]));
-			mw_capabilities();
-		} elsif ($cmd[0] eq "list") {
-			die("Too many arguments for list\n") if (defined($cmd[2]));
-			mw_list($cmd[1]);
-		} elsif ($cmd[0] eq "import") {
-			die("Invalid arguments for import\n") if ($cmd[1] eq "" || defined($cmd[2]));
-			mw_import($cmd[1]);
-		} elsif ($cmd[0] eq "option") {
-			die("Too many arguments for option\n") if ($cmd[1] eq "" || $cmd[2] eq "" || defined($cmd[3]));
-			mw_option($cmd[1],$cmd[2]);
-		} elsif ($cmd[0] eq "push") {
-			mw_push($cmd[1]);
-		} else {
-			print STDERR "Unknown command. Aborting...\n";
+		if (!parse_command(\@cmds)) {
 			last;
 		}
 	} else {
@@ -167,6 +152,33 @@ sub exit_error_usage {
     die "ERROR: git-remote-mediawiki module was not called with a correct number of parameters\n";
 }
 
+sub parse_command {
+	my $cmdref = shift;
+	my @cmd = @{$cmdref};
+	if ($cmd[0] eq "capabilities") {
+		die("Too many arguments for capabilities\n")
+		    if (defined($cmd[1]));
+		mw_capabilities();
+	} elsif ($cmd[0] eq "list") {
+		die("Too many arguments for list\n") if (defined($cmd[2]));
+		mw_list($cmd[1]);
+	} elsif ($cmd[0] eq "import") {
+		die("Invalid arguments for import\n")
+		    if ($cmd[1] eq "" || defined($cmd[2]));
+		mw_import($cmd[1]);
+	} elsif ($cmd[0] eq "option") {
+		die("Too many arguments for option\n")
+		    if ($cmd[1] eq "" || $cmd[2] eq "" || defined($cmd[3]));
+		mw_option($cmd[1],$cmd[2]);
+	} elsif ($cmd[0] eq "push") {
+		mw_push($cmd[1]);
+	} else {
+		print STDERR "Unknown command. Aborting...\n";
+		return 0;
+	}
+	return 1;
+}
+
 # MediaWiki API instance, created lazily.
 my $mediawiki;
 
-- 
1.7.9.5
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help