App-Filite-Client

 view release on metacpan or  search on metacpan

lib/App/Filite/Client.pm  view on Meta::CPAN

use 5.010001;
use strict;
use warnings;

package App::Filite::Client;

our $AUTHORITY = 'cpan:TOBYINK';
our $VERSION   = '0.001001';

use Carp qw( croak );
use File::XDG;
use Getopt::Long qw( GetOptionsFromArray );
use HTTP::Tiny;
use HTTP::Tiny::Multipart;
use JSON::PP qw( encode_json decode_json );
use MIME::Base64 qw( encode_base64 );
use Path::Tiny qw( path );

use Class::Tiny {
	password   => sub { croak "Missing option: password" },
	server     => sub { croak "Missing option: server" },
	useragent  => sub { shift->_build_useragent },
	errors     => sub { 0 },
};

use namespace::autoclean;

sub new_from_config {
	my ( $class ) = ( shift );
	
	state $xdg = File::XDG->new( name => 'filite-client', api => 1 );
	my $config_file = $ENV{FILITE_CLIENT_CONFIG} // $xdg->config_home->child( 'config.json' );
	if ( not ref $config_file ) {
		$config_file = path( $config_file );
	}
	croak "Expected config file: $config_file" unless $config_file->is_file;
	
	my $args = decode_json( $config_file->slurp_utf8 );
	my $self = $class->new( %$args );
	return $self;
}

sub _build_useragent {
	my ( $self ) = ( shift );
	my $auth = encode_base64( sprintf( 'admin:%s', $self->password ) );
	chomp $auth;
	return HTTP::Tiny->new(
		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



( run in 0.707 second using v1.01-cache-2.11-cpan-39bf76dae61 )