App-cpanel
view release on metacpan or search on metacpan
[](https://metacpan.org/pod/App::cpanel)
# SYNOPSIS
$ cpanel uapi Notifications get_notifications_count
$ cpanel uapi ResourceUsage get_usages
$ cpanel uapi Fileman list_files dir=public_html
$ cpanel uapi Fileman get_file_content dir=public_html file=index.html
$ cpanel download public_html/index.html
$ cpanel api2 Fileman fileop op=chmod metadata=0755 sourcefiles=public_html/cgi-bin/hello-world
$ cpanel api2 Fileman fileop op=unlink sourcefiles=public_html/cgi-bin/hello-world
$ cpanel api2 Fileman mkdir path= name=new-dir-at-top
# this one is one at a time but can overwrite files
$ cpanel api2 Fileman savefile dir=public_html/cgi-bin filename=hello-world content="$(cat public_html/cgi-bin/hello-world)"
# this is multiple files but refuses to overwrite
$ cpanel upload public_html/cgi-bin hello-world
# download
$ cpanel mirror public_html public_html cpanel localfs
### read
Takes `$dir`, `$file`. Returns a promise of the file contents.
### write
Takes `$dir`, `$file`. Returns a promise of having written the file
contents.
### chmod
Takes `$path`, `$perms`. Returns a promise of having changed the
permissions.
# SEE ALSO
[https://documentation.cpanel.net/display/DD/Guide+to+UAPI](https://documentation.cpanel.net/display/DD/Guide+to+UAPI)
[https://documentation.cpanel.net/display/DD/Guide+to+cPanel+API+2](https://documentation.cpanel.net/display/DD/Guide+to+cPanel+API+2)
lib/App/cpanel.pm view on Meta::CPAN
=end markdown
=head1 SYNOPSIS
$ cpanel uapi Notifications get_notifications_count
$ cpanel uapi ResourceUsage get_usages
$ cpanel uapi Fileman list_files dir=public_html
$ cpanel uapi Fileman get_file_content dir=public_html file=index.html
$ cpanel download public_html/index.html
$ cpanel api2 Fileman fileop op=chmod metadata=0755 sourcefiles=public_html/cgi-bin/hello-world
$ cpanel api2 Fileman fileop op=unlink sourcefiles=public_html/cgi-bin/hello-world
$ cpanel api2 Fileman mkdir path= name=new-dir-at-top
# this one is one at a time but can overwrite files
$ cpanel api2 Fileman savefile dir=public_html/cgi-bin filename=hello-world content="$(cat public_html/cgi-bin/hello-world)"
# this is multiple files but refuses to overwrite
$ cpanel upload public_html/cgi-bin hello-world
# download
$ cpanel mirror public_html public_html cpanel localfs
lib/App/cpanel.pm view on Meta::CPAN
=head3 read
Takes C<$dir>, C<$file>. Returns a promise of the file contents.
=head3 write
Takes C<$dir>, C<$file>. Returns a promise of having written the file
contents.
=head3 chmod
Takes C<$path>, C<$perms>. Returns a promise of having changed the
permissions.
=head1 SEE ALSO
L<https://documentation.cpanel.net/display/DD/Guide+to+UAPI>
L<https://documentation.cpanel.net/display/DD/Guide+to+cPanel+API+2>
lib/App/cpanel.pm view on Meta::CPAN
api2 => [ \&api2_p, 1 ],
mirror => [ \&mirror_p, 1 ],
);
my $token_file = "$ENV{HOME}/.cpanel-token";
my $domain_file = "$ENV{HOME}/.cpanel-domain";
my %localfs_map = (
ls => \&localfs_ls,
mkdir => \&localfs_mkdir,
read => \&localfs_read,
write => \&localfs_write,
chmod => \&localfs_chmod,
);
my %cpanel_map = (
ls => \&cpanel_ls,
mkdir => \&cpanel_mkdir,
read => \&cpanel_read,
write => \&cpanel_write,
chmod => \&cpanel_chmod,
);
our %MAP2HASH = (
localfs => \%localfs_map,
cpanel => \%cpanel_map,
);
sub dispatch_cmd_print {
my $cmd = shift;
die "No command\n" unless $cmd;
die "Unknown command '$cmd'\n" unless my $info = $cmd2func{$cmd};
lib/App/cpanel.pm view on Meta::CPAN
my $from_dir_perms;
$to_map->{ls}->($to_dir)->catch(sub {
# only create if ls fails
$to_map->{mkdir}->($to_dir)
})->then(sub {
$from_map->{ls}->(path($from_dir)->dirname)
})->then(sub {
my ($dirs, $files) = @_;
$from_dir_perms = $dirs->{path($from_dir)->basename}[0] || '0755';
})->then(sub {
$to_map->{chmod}->($to_dir, $from_dir_perms)
})->then(sub {
$from_map->{ls}->($from_dir)
})->then(sub {
my ($dirs, $files) = @_;
my @dir_create_p = map
dir_walk_p("$from_dir/$_", "$to_dir/$_", $from_map, $to_map),
sort keys %$dirs;
my @file_create_p = map {
my $this_file = $_;
$from_map->{read}->($from_dir, $this_file)
->then(sub { $to_map->{write}->($to_dir, $this_file, $_[0]) })
->then(sub { $to_map->{chmod}->("$to_dir/$this_file", $files->{$this_file}[0]) })
} sort keys %$files;
return Mojo::Promise->resolve(1) unless @dir_create_p + @file_create_p;
Mojo::Promise->all(@dir_create_p, @file_create_p);
});
}
sub mirror_p {
my ($from_dir, $to_dir, $from_map, $to_map) = @_;
die "No from_dir\n" unless $from_dir;
die "No to_dir\n" unless $to_dir;
lib/App/cpanel.pm view on Meta::CPAN
Mojo::Promise->resolve($path->slurp);
}
sub localfs_write {
my ($dir, $file, $content) = @_;
my $path = path($dir)->child($file);
$path->spurt($content);
Mojo::Promise->resolve(1);
}
sub localfs_chmod {
my ($path, $perms) = @_;
$path = path($path);
$path->chmod(oct $perms);
Mojo::Promise->resolve(1);
}
sub cpanel_ls {
my ($dir) = @_;
uapi_p(qw(Fileman list_files), { dir => $dir })->then(sub {
my (%dirs, %files);
($_->{type} eq 'dir' ? \%dirs : \%files)->{$_->{file}} =
[ $_->{nicemode}, $_->{mtime} ]
for @{ $_[0]->{data} };
lib/App/cpanel.pm view on Meta::CPAN
api2_p qw(Fileman mkdir), { path => $dir->dirname, name => $dir->basename };
}
sub cpanel_write {
my ($dir, $file, $content) = @_;
api2_p qw(Fileman savefile), {
dir => $dir, filename => $file, content => $content,
};
}
sub cpanel_chmod {
my ($path, $perms) = @_;
api2_p qw(Fileman fileop), {
op => 'chmod', metadata => $perms, sourcefiles => $path,
};
}
1;
t/dirwalk.t view on Meta::CPAN
use Test::More;
use App::cpanel qw(dir_walk_p);
use Mojo::Promise;
my (@mkdirs, @writes, @chmods);
my %dir2contents = (
'.' => [
{ 'public_html' => [qw(0755 30)] },
{},
],
'public_html' => [
{ 'cgi-bin' => [qw(0755 30)], 'logs' => [qw(0755 30)] },
{ 'index.html' => [qw(0644 30)], 'other.html' => [qw(0644 30)] },
],
'public_html/cgi-bin' => [
t/dirwalk.t view on Meta::CPAN
);
my %test_map = (
ls => sub {
$dir2contents{$_[0]}
? Mojo::Promise->resolve(@{$dir2contents{$_[0]}})
: Mojo::Promise->reject("$_[0] does not exist");
},
mkdir => sub { push @mkdirs, $_[0]; Mojo::Promise->resolve(1); },
read => sub { Mojo::Promise->resolve($file2contents{"$_[0]/$_[1]"}); },
write => sub { push @writes, [ @_ ]; Mojo::Promise->resolve(1); },
chmod => sub { push @chmods, [ @_ ]; Mojo::Promise->resolve(1); },
);
my @errors;
dir_walk_p(qw(public_html other), \%test_map, \%test_map)
->catch(sub { @errors = @_ })->wait;
ok !@errors, 'errors' or diag explain \@errors;
is_deeply \@mkdirs, [ qw(other other/cgi-bin other/logs) ], 'mkdirs'
or diag explain \@mkdirs;
is_deeply \@writes, [
[ qw(other index.html), 'the index' ],
[ qw(other other.html), 'other page' ],
[ qw(other/cgi-bin hello), 'hello' ],
], 'writes' or diag explain \@writes;
is_deeply \@chmods, [
[ 'other', '0755' ],
[ 'other/index.html', '0644' ],
[ 'other/other.html', '0644' ],
[ 'other/cgi-bin', '0755' ],
[ 'other/logs', '0755' ],
[ 'other/cgi-bin/hello', '0755' ],
], 'chmods' or diag explain \@chmods;
done_testing;
( run in 0.377 second using v1.01-cache-2.11-cpan-496ff517765 )