| 1 | package Mojolicious::Command::nopaste::Service::gist; | ||||||||||||
| 2 | 1 | ▶ | use Mojo::Base 'Mojolicious::Command::nopaste::Service'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 3 | 1 | ▶ | use Mojo::Path; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 1 | ▶ | use File::HomeDir; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 1 | ▶ | use File::Spec (); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 1 | ▶ | use Mojo::URL; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | |||||||||||||
| 8 | 1 | has description => "Post to gist.github.com\n"; | |||||||||||
| 9 | 0 | ▶ | has token => sub { | ||||||||||
|
Subroutine
__ANON__: not called CC=4 SCAR=30.0
| |||||||||||||
| 10 | 0 | my $self = shift; | |||||||||||
| 11 | 0 | ▶ | return $ENV{GIST_TOKEN} if $ENV{GIST_TOKEN}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 12 | 0 | my $home = File::HomeDir->my_home; | |||||||||||
| 13 | 0 | my $path = File::Spec->catfile($home, '.gist_token'); | |||||||||||
| 14 | 0 | ▶ | return undef unless -e $path; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 15 | 0 | my $token = $self->slurp($path); | |||||||||||
| 16 | 0 | chomp $token; | |||||||||||
| 17 | 0 | ▶ | return $token || undef; | ||||||||||
|
Condition0%0/2
$token || undef
MC/DC0%0/1
$token || undef
$token
Truth table0%0/2
A
A= $token
| |||||||||||||
| 18 | 1 | }; | |||||||||||
| 19 | |||||||||||||
| 20 | 1 | my $token_usage = <<'TOKEN'; | |||||||||||
| 21 | A token is required for posting to gist. | ||||||||||||
| 22 | |||||||||||||
| 23 | See how to create one at: | ||||||||||||
| 24 | https://help.github.com/articles/creating-an-access-token-for-command-line-use | ||||||||||||
| 25 | To specify your token one the command line, set the --token (-t) command line | ||||||||||||
| 26 | switch to a path to your token or the token value itself. | ||||||||||||
| 27 | Alternatively you can specify the token using the GIST_TOKEN env variable, or | ||||||||||||
| 28 | by creating a file called .gist_token in your home directory. | ||||||||||||
| 29 | |||||||||||||
| 30 | TOKEN | ||||||||||||
| 31 | |||||||||||||
| 32 | 1 | has service_usage => $token_usage; | |||||||||||
| 33 | |||||||||||||
| 34 | 0 | ▶ | sub paste { | ||||||||||
|
Subroutine
paste: not called CC=9 SCAR=45.0
Pod
undocumented
| |||||||||||||
| 35 | 0 | my $self = shift; | |||||||||||
| 36 | 0 | ▶ | my $token = $self->token or die $token_usage; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 37 | 0 | ▶ | my $data = { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 38 | public => $self->private ? \0 : \1, | ||||||||||||
| 39 | }; | ||||||||||||
| 40 | |||||||||||||
| 41 | 0 | my $files = $self->files; | |||||||||||
| 42 | 0 | ▶ | if (@$files) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 43 | 0 | $data->{files}{Mojo::Path->new($_)->[-1]}{content} = $self->slurp($_) for @$files; | |||||||||||
| 44 | } else { | ||||||||||||
| 45 | 0 | $data->{files}{noname}{content} = $self->slurp; | |||||||||||
| 46 | } | ||||||||||||
| 47 | |||||||||||||
| 48 | 0 | ▶ | $data->{description} = $self->desc if $self->desc; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 49 | |||||||||||||
| 50 | 0 | my $url = Mojo::URL->new('https://api.github.com/gists'); | |||||||||||
| 51 | 0 | my $method = 'POST'; | |||||||||||
| 52 | |||||||||||||
| 53 | 0 | ▶ | if (my $update = $self->update) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 54 | 0 | push @{ $url->path->parts }, $update; | |||||||||||
| 55 | 0 | $method = 'PATCH'; | |||||||||||
| 56 | } | ||||||||||||
| 57 | |||||||||||||
| 58 | 0 | my $ua = $self->ua; | |||||||||||
| 59 | 0 | my $tx = $ua->build_tx( | |||||||||||
| 60 | $method => $url, | ||||||||||||
| 61 | { | ||||||||||||
| 62 | Accept => 'application/vnd.github.v3+json', | ||||||||||||
| 63 | Authorization => "token $token", | ||||||||||||
| 64 | 'User-Agent' => 'Mojolicious-Command-nopaste (author: jberger)', | ||||||||||||
| 65 | }, | ||||||||||||
| 66 | json => $data, | ||||||||||||
| 67 | ); | ||||||||||||
| 68 | 0 | $ua->start($tx); | |||||||||||
| 69 | |||||||||||||
| 70 | 0 | ▶ | if (my $error = $tx->error) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 71 | 0 | ▶ | if ($error->{code}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 72 | 0 | say "Request failed, code $error->{code}: $error->{message}"; | |||||||||||
| 73 | 0 | say $tx->res->body; | |||||||||||
| 74 | } else { | ||||||||||||
| 75 | 0 | say "Connection error: $error->{message}"; | |||||||||||
| 76 | } | ||||||||||||
| 77 | 0 | exit 1; | |||||||||||
| 78 | } | ||||||||||||
| 79 | |||||||||||||
| 80 | 0 | return $tx->res->json->{html_url}; | |||||||||||
| 81 | } | ||||||||||||
| 82 | |||||||||||||
| 83 | 1 | 1; | |||||||||||
| 84 | |||||||||||||