added listing option
authorHeiko Schlittermann (ZARAFA.ctq.de) <hs@schlittermann.de>
Thu, 28 Apr 2011 16:54:51 +0200
changeset 11 2c1f279d3939
parent 10 3c861b5ebe9c
child 12 09f5c71b3a76
added listing option
ftpipe
--- a/ftpipe	Tue Dec 29 22:32:16 2009 +0100
+++ b/ftpipe	Thu Apr 28 16:54:51 2011 +0200
@@ -1,11 +1,12 @@
 #! /usr/bin/perl
+
+use 5.010;
 use strict;
 use warnings;
 use File::Temp;
 use Pod::Usage;
 use Getopt::Long;
 use if $ENV{DEBUG} => qw(Smart::Comments);
-use feature qw(say switch);
 
 my $opt_put   = 0;
 my $opt_get   = 0;
@@ -20,8 +21,8 @@
     GetOptions(
         "p|put!" => sub { $opt_cmd = "put" },
         "g|get!" => sub { $opt_cmd = "get" },
-
-        #"l|ls!"  =>
+        "l|ls!"  => sub { $opt_cmd = "ls" },
+        "d|L|ll|dir!"  => sub { $opt_cmd = "dir" },
         #"r|rm!"  =>
         "debug!" => \$opt_debug,
         "h|help" => sub { pod2usage(-verbose => 1, -exit => 0) },
@@ -34,7 +35,8 @@
 
     ### %ftp
     die "need hostname" if not defined $ftp{host};
-    die "need filename" if not defined $ftp{file};
+    die "need filename" if not defined $ftp{file}
+	and $opt_cmd ~~ [qw(put get)];
 
     my $ftp = new FTP($ftp{host}, Debug => $opt_debug, Passive => 1);
     $ftp->login($ftp{user}, $ftp{pass});
@@ -44,22 +46,25 @@
     given ($opt_cmd) {
         when ("put") { $ftp->put(*STDIN, $ftp{file}) }
         when ("get") { $ftp->get($ftp{file}, *STDOUT) }
+	when ("ls")  { say join "\n", sort $ftp->ls };
+	when ("dir") { say join "\n", sort $ftp->dir };
         default { pod2usage };
     }
 
     exit;
 
-    $ftp->put(*STDIN, $ftp{file});
-    print $ftp->dir, "\n";
-
 }
 
 sub parse_url($) {
     $_[0] =~ m{^ftp://
 	    (?:(?<user>.*?)(?::(?<pass>.*))?@)?
 	    (?<host>[a-z\d_\.]+)/
-	    (?:(?<dir>.+)/)?(?<file>\S+)?}x;
-    return my %r = %+;
+	    (?<path>.*)}x;
+    my %r = %+;
+
+    ($r{dir}, $r{file}) = $r{path} =~ /^(?:(.*)\/)?(.*)?/;
+
+    return %r;
 }
 
 {
@@ -112,20 +117,32 @@
 
     ftpipe --put ftp://[user[:pass]@]server/dir/file
     ftpipe --get ftp://[user[:pass]@]server/dir/file
+    ftpipe --ls  ftp://[user[:pass]@]server/dir/[file]
+    ftpipe --dir ftp://[user[:pass]@]server/dir/[file]
 
 =head1 OPTIONS
 
 =over
 
+=item B<-d>|B<--dir>
+
+Show a long listing to stdout. The listing is sorted, but by line, not 
+by then file names! Take care to end the URL with a slash B</> if you
+want to see the contents of a directory!
+
 =item B<-g>|B<--get>
 
 Get the file from the remote server and print it to stdout.
 
+=item B<-l>|B<--ls>
+
+Show a (probably short) sorted listing to stdout.
+See above at B<-d> for the notice about the trailing slash.
+
 =item B<-p>|B<--put>
 
 Put the data read from stdin to the remote file.
 
-
 =back
 
 =head1 FILES