Created
January 16, 2017 11:21
-
-
Save araraloren/b33529c8e2b353888e2c10236d9b6c7c to your computer and use it in GitHub Desktop.
use perl5 Net::FTP module in perl6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl6 | |
use JSON::Fast; | |
use Inline::Perl5; | |
#`{ | |
{ | |
"user-list":[ | |
"xxx" | |
], | |
"xx":[ | |
{ | |
"ip":"192.169.126.100", | |
"pass":"xxxmm", | |
"ext":"" | |
} | |
] | |
} | |
} | |
my $default-config = $*HOME ~ '/.ftpuser.p6.cfg'; | |
sub MAIN(Str :c(:$config-file) = "", *@file) { | |
if +@file == 0 { | |
say "No file argument"; | |
return; | |
} | |
my $config-json; | |
if $config-file.chars == 0 { | |
say "Use default config path -> $default-config"; | |
$config-json = $default-config; | |
}else{ | |
$config-json = $config-file; | |
} | |
my $json-path = IO::Path.new($config-json); | |
if (!($json-path.e && $json-path.r)) { | |
say "Config file not exist path -> $json-path"; | |
exit | |
} | |
my $json; | |
try { | |
$json = from-json($json-path.slurp()); | |
CATCH { | |
default { | |
"User config has error".say(); | |
"Stack ----->".say(); | |
... | |
} | |
} | |
} | |
my @users = show-user-list($json, key => "user-list"); | |
if !defined(@users) { | |
say "Json config not have user-list"; | |
exit | |
} | |
my $index = -1, my $count = +@users; | |
while $count > 0 { | |
input-prompt(); | |
$index = get($*IN); | |
if $index < 0 || $index >= $count { | |
say "Error, please input again ....."; | |
}else{ | |
last; | |
} | |
} | |
my $user = @users[+$index]; | |
my $x = get-user-info(user => $user, $json); | |
if (!defined($x)) { | |
say "Config error ... Please check"; | |
exit; | |
} | |
my $perl5 = Inline::Perl5.new(); | |
$perl5.run(' | |
use Net::FTP; | |
use File::Basename qw/ basename /; | |
use strict; | |
use utf8; | |
sub update{ | |
my ($file, $user, $info) = @_; | |
my ($pass, $ip, $port, $dir, $ext) = ( | |
$info->{pass}, | |
$info->{ip}, | |
$info->{port}, | |
$info->{dir}, | |
$info->{ext}, | |
); | |
my $ftp = Net::FTP->new($ip, Port => $port); | |
if(! defined($ftp)){ | |
print "Can not connect to ftp ${ip}:${port}.\n"; | |
return; | |
} | |
if(! $ftp->login($user, $pass)){ | |
print "Can not login ${ip}:${port} with username $user"; | |
print "check you username or password.\n"; | |
return; | |
} | |
$ftp->mkdir($dir); | |
$ftp->binary; | |
my $filename = basename $file; | |
my $tmpfile = $filename.$ext; | |
rename $file => $tmpfile; | |
print "Move $file => $tmpfile\n"; | |
my $remote = "$dir/$tmpfile"; | |
$ftp->put($file.$ext, $remote); | |
print "Upload $file => $remote\n"; | |
$ftp->quit; | |
} | |
sub get_name { | |
my $file = shift @_; | |
if($file =~ /\/(.*)/){ | |
return $1; | |
}else { | |
return $file; | |
} | |
} | |
'); | |
for @file -> $sf { | |
say "ready update file $sf to " ~ $x{'ip'} ~ " with user $user"; | |
$perl5.call('update', | |
$sf, | |
$user, | |
$x); | |
} | |
$perl5.DESTROY; | |
} | |
sub show-user-list($json, :$key) { | |
if $json{$key}:exists == True { | |
my @user-list = $json{$key}.split(' '); | |
"Please chose a index".say(); | |
"User list \n--------------------".say(); | |
loop (my $i = 0;$i < +@user-list; $i++) { | |
say ~$i ~ ".\t" ~ @user-list[$i]; | |
} | |
"--------------------".say(); | |
return @user-list; | |
} | |
} | |
sub get-user-info($json, :$user) { | |
if $json{$user}:exists == True { | |
return $json{$user}; | |
} | |
} | |
sub input-prompt($prompt = ">>>>>") { | |
print $prompt; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment