git-remote-mediawiki: put long code into a subroutine

Signed-off-by: Célestin Matte <celestin.matte@ensimag.fr> Signed-off-by: Matthieu Moy <matthieu.moy@grenoble-inp.fr> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Célestin Matte committed Jun 14, 2013 at 15:50 UTC 6a316beeeecc76d19ff60fda62e00a7e2e041137
1 file changed +32 -24
contrib/mw-to-git/git-remote-mediawiki.perl
+32 -24
@@ -122,32 +122,10 @@ $wiki_name =~ s{[^/]*://}{};
122 $wiki_name =~ s/^.*@//;
123
124 # Commands parser
125 -my @cmd;
125 while (<STDIN>) {
126 chomp;
128 - @cmd = split(/ /);
129 - if (defined($cmd[0])) {
130 - # Line not blank
131 - if ($cmd[0] eq "capabilities") {
132 - die("Too many arguments for capabilities\n") if (defined($cmd[1]));
133 - mw_capabilities();
134 - } elsif ($cmd[0] eq "list") {
135 - die("Too many arguments for list\n") if (defined($cmd[2]));
136 - mw_list($cmd[1]);
137 - } elsif ($cmd[0] eq "import") {
138 - die("Invalid arguments for import\n") if ($cmd[1] eq "" || defined($cmd[2]));
139 - mw_import($cmd[1]);
140 - } elsif ($cmd[0] eq "option") {
141 - die("Too many arguments for option\n") if ($cmd[1] eq "" || $cmd[2] eq "" || defined($cmd[3]));
142 - mw_option($cmd[1],$cmd[2]);
143 - } elsif ($cmd[0] eq "push") {
144 - mw_push($cmd[1]);
145 - } else {
146 - print STDERR "Unknown command. Aborting...\n";
147 - last;
148 - }
149 - } else {
150 - # blank line: we should terminate
127 +
128 + if (!parse_command($_)) {
129 last;
130 }
131
@@ -157,6 +135,36 @@ while (<STDIN>) {
135
136 ########################## Functions ##############################
137
138 +sub parse_command {
139 + my ($line) = @_;
140 + my @cmd = split(/ /, $line);
141 + if (!defined $cmd[0]) {
142 + return 0;
143 + }
144 + if ($cmd[0] eq "capabilities") {
145 + die("Too many arguments for capabilities\n")
146 + if (defined($cmd[1]));
147 + mw_capabilities();
148 + } elsif ($cmd[0] eq "list") {
149 + die("Too many arguments for list\n") if (defined($cmd[2]));
150 + mw_list($cmd[1]);
151 + } elsif ($cmd[0] eq "import") {
152 + die("Invalid arguments for import\n")
153 + if ($cmd[1] eq "" || defined($cmd[2]));
154 + mw_import($cmd[1]);
155 + } elsif ($cmd[0] eq "option") {
156 + die("Too many arguments for option\n")
157 + if ($cmd[1] eq "" || $cmd[2] eq "" || defined($cmd[3]));
158 + mw_option($cmd[1],$cmd[2]);
159 + } elsif ($cmd[0] eq "push") {
160 + mw_push($cmd[1]);
161 + } else {
162 + print STDERR "Unknown command. Aborting...\n";
163 + return 0;
164 + }
165 + return 1;
166 +}
167 +
168 # MediaWiki API instance, created lazily.
169 my $mediawiki;
170