-
-
Save anonymous/aa857999855a3267a942 to your computer and use it in GitHub Desktop.
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
index 1fca6c5..51a21ac 100644 | |
--- a/lib/Mojolicious/Command/get.pm | |
+++ b/lib/Mojolicious/Command/get.pm | |
@@ -37,7 +37,9 @@ sub run { | |
$url !~ m!^/! ? $ua->proxy->detect : $ua->server->app($self->app); | |
$ua->max_redirects(10) if $redirect; | |
- my $buffer = ''; | |
+ # "-" (STDIN) | |
+ my $buffer = $url eq '-' ? do { local $/; <STDIN> } : ''; | |
+ | |
$ua->on( | |
start => sub { | |
my ($ua, $tx) = @_; | |
@@ -62,21 +64,23 @@ sub run { | |
); | |
# Switch to verbose for HEAD requests | |
- $verbose = 1 if $method eq 'HEAD'; | |
STDOUT->autoflush(1); | |
- my $tx = $ua->start($ua->build_tx($method, $url, \%headers, $content)); | |
- my $err = $tx->error; | |
- $url = encode 'UTF-8', $url; | |
- warn qq{Problem loading URL "$url". ($err->{message})\n} | |
- if $err && !$err->{code}; | |
+ if ($url ne '-') { | |
+ $verbose = 1 if $method eq 'HEAD'; | |
+ my $tx = $ua->start($ua->build_tx($method, $url, \%headers, $content)); | |
+ $charset //= $tx->res->content->charset; | |
+ my $err = $tx->error; | |
+ warn | |
+ qq{Problem loading URL "@{[$tx->req->url->to_abs]}". ($err->{message})\n} | |
+ if $err && !$err->{code}; | |
+ } | |
# JSON Pointer | |
return unless defined $selector; | |
- my $type = $tx->res->headers->content_type // ''; | |
- return _json($buffer, $selector) if $type =~ /json/i; | |
+ return _json($buffer, $selector) if $selector eq '' || $selector =~ m!^/!; | |
# Selector | |
- _select($buffer, $selector, $charset // $tx->res->content->charset, @args); | |
+ _select($buffer, $selector, $charset, @args); | |
} | |
sub _json { | |
@@ -140,6 +144,7 @@ Mojolicious::Command::get - Get command | |
mojo get mojolicio.us '*' attr id | |
mojo get mojolicio.us 'h1, h2, h3' 3 text | |
mojo get https://api.metacpan.org/v0/author/SRI /name | |
+ mojo get mojolicio.us | mojo get - 'h1, h2, h3' | |
Options: | |
-C, --charset <charset> Charset of HTML/XML content, defaults to auto |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment