App-Filite-Client
view release on metacpan or search on metacpan
lib/App/Filite/Client.pm view on Meta::CPAN
agent => sprintf( '%s/%s ', __PACKAGE__, $VERSION ),
default_headers => { 'Authorization' => "Basic $auth" },
);
}
sub _parse_opts {
my ( $self, $args ) = ( shift, @_ );
my $opts = {};
GetOptionsFromArray(
$args => $opts,
'text|T',
'file|F',
'link|L',
'highlight|H',
'help|usage',
);
return $opts;
}
## no Test::Tabs
sub _print_usage {
print <<"STDERR"; return 0;
filite-client: share via a filite server
Usage:
filite-client -T [filename]
filite-client -F [filename]
filite-client -L [url]
cat blah | filite-client [options]
Options:
--text, -T Share as text
--file, -F Share as file
--link, -L Share as link
--highlight, -H Syntax highligh text
--help, --usage Show this usage information
STDERR
}
## use Test::Tabs
sub execute {
my ( $self, $args ) = ( shift, @_ );
$args //= [ @ARGV ];
my $opts = $self->_parse_opts( $args );
$args = [ '-' ] unless @$args;
if ( $opts->{help} ) {
return $self->_print_usage;
}
for my $file ( @$args ) {
my $url = $self->share( $file, $opts );
print "$url\n";
}
$self->errors;
}
sub _guess_mode {
my ( $self, $file, $opts ) = ( shift, @_ );
return 'link' if $opts->{link};
return 'text' if $opts->{text};
return 'file' if $opts->{file};
return 'link' if $file =~ m{\Ahttps?://\S+\z}is;
return 'text' if $opts->{highlight};
return 'text' if $file eq '-';
return 'file' if -B $file;
return 'text';
}
sub share {
my ( $self, $file, $opts ) = ( shift, @_ );
$opts //= {};
my $mode = $self->_guess_mode( $file, $opts );
my $method = "share_$mode";
return $self->$method( $file, $opts );
}
sub _get_endpoint {
my ( $self, $mode ) = ( shift, @_ );
my $server = $self->server;
$server = "http://$server" unless $server =~ m{https?:}i;
$server .= '/' unless $server =~ m{/$};
return sprintf( '%s%s', $server, lc( substr( $mode, 0, 1 ) ) );
}
sub _handle_response {
my ( $self, $response ) = ( shift, @_ );
if ( $response->{success} ) {
return $response->{content};
}
my $errs = $self->errors;
++$errs;
$self->errors( $errs );
warn sprintf( "ERROR: %s %s\n", $response->{status}, $response->{reason} );
return "-";
}
sub share_file {
my ( $self, $file, $opts ) = ( shift, @_ );
$opts //= {};
my ( $filename, $content );
if ( $file eq '-' ) {
$filename = 'file.data';
local $/;
$content = <STDIN>;
}
else {
my $pt = path( $file );
$filename = $pt->basename;
$content = $pt->slurp;
}
my $endpoint = $self->_get_endpoint( 'file' );
my $response = $self->useragent->post_multipart(
$endpoint => {
file => {
filename => $filename,
content => $content,
content_type => 'application/octet-stream',
},
},
);
return sprintf( '%s/%s', $endpoint, $self->_handle_response( $response ) );
}
sub share_text {
my ( $self, $file, $opts ) = ( shift, @_ );
$opts //= {};
my $content;
if ( $file eq '-' ) {
( run in 2.893 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )