| File: | bin/seo-inspect |
| Coverage: | 73.4% |
| line | stmt | bran | cond | sub | time | code |
|---|---|---|---|---|---|---|
| 1 | #!/usr/bin/env perl | |||||
| 2 | ||||||
| 3 | 2 2 2 | 3795 1 31 | use strict; | |||
| 4 | 2 2 2 | 4 0 40 | use warnings; | |||
| 5 | ||||||
| 6 | 2 2 2 | 362 964 125 | use FindBin qw($Bin); | |||
| 7 | 2 2 2 | 642 9134 4 | use Getopt::Long qw(GetOptions); | |||
| 8 | 2 2 2 | 599 26708 55 | use File::Slurp qw(read_file); | |||
| 9 | 2 2 2 | 4 1 46 | use JSON::MaybeXS; | |||
| 10 | 2 2 2 | 572 7813 65 | use Term::ANSIColor; | |||
| 11 | ||||||
| 12 | 2 2 2 | 387 5 131798 | use SEO::Inspector; | |||
| 13 | ||||||
| 14 | # ------------------------------- | |||||
| 15 | # Command line options | |||||
| 16 | # ------------------------------- | |||||
| 17 | 2 | 48293 | my ($url, $file, $json, $no_colour); | |||
| 18 | 2 | 6 | GetOptions( | |||
| 19 | 'url|u=s' => \$url, | |||||
| 20 | 'file|f=s' => \$file, | |||||
| 21 | 'json|j' => \$json, | |||||
| 22 | 'no-colour|n' => \$no_colour, | |||||
| 23 | 'no-color' => \$no_colour, | |||||
| 24 | ) or die "Usage: $0 --url <URL> | --file <HTML file> [--json] [--no-colour]\n"; | |||||
| 25 | ||||||
| 26 | 2 | 845 | die 'Specify either --url or --file' unless $url || $file; | |||
| 27 | ||||||
| 28 | # ------------------------------- | |||||
| 29 | # Create inspector object | |||||
| 30 | # ------------------------------- | |||||
| 31 | 2 | 10 | my $inspector = SEO::Inspector->new( | |||
| 32 | url => $url, | |||||
| 33 | plugin_dirs => [ "$Bin/lib/SEO/Inspector/Plugin" ] | |||||
| 34 | ); | |||||
| 35 | ||||||
| 36 | # ------------------------------- | |||||
| 37 | # Fetch HTML and run checks | |||||
| 38 | # ------------------------------- | |||||
| 39 | 2 | 2 | my ($html, $plugin_results, $builtin_results); | |||
| 40 | ||||||
| 41 | 2 | 4 | if ($file) { | |||
| 42 | 2 | 5 | $html = read_file($file); | |||
| 43 | 2 | 101 | die 'File is empty' unless $html; | |||
| 44 | ||||||
| 45 | 2 | 5 | $plugin_results = $inspector->check_html($html); | |||
| 46 | 2 | 3 | $builtin_results = $inspector->run_all($html); | |||
| 47 | } else { | |||||
| 48 | 0 | 0 | my $all_results = $inspector->check_url($url); | |||
| 49 | 0 | 0 | $html = delete $all_results->{_html}; # remove internal HTML | |||
| 50 | 0 0 0 | 0 0 0 | $plugin_results = { map { $_ => $all_results->{$_} } grep { exists $inspector->{plugins}{$_} } keys %$all_results }; | |||
| 51 | 0 0 0 | 0 0 0 | $builtin_results = { map { $_ => $all_results->{$_} } grep { !exists $inspector->{plugins}{$_} } keys %$all_results }; | |||
| 52 | } | |||||
| 53 | ||||||
| 54 | # ------------------------------- | |||||
| 55 | # Merge results | |||||
| 56 | # ------------------------------- | |||||
| 57 | 2 | 6 | my %results = (%$plugin_results, %$builtin_results); | |||
| 58 | ||||||
| 59 | # ------------------------------- | |||||
| 60 | # Output results | |||||
| 61 | # ------------------------------- | |||||
| 62 | 2 | 5 | if ($json) { | |||
| 63 | 1 | 50 | print JSON::MaybeXS::encode_json(\%results), "\n"; | |||
| 64 | } else { | |||||
| 65 | 1 | 4 | for my $key (sort keys %results) { | |||
| 66 | 18 | 12 | my $r = $results{$key}; | |||
| 67 | 18 | 13 | next unless ref($r) eq 'HASH'; | |||
| 68 | ||||||
| 69 | 18 | 8 | if($no_colour) { | |||
| 70 | 0 | 0 | printf "[%s] %s: %s\n", $r->{status} // 'unknown', $r->{name} // $key, $r->{notes} // ''; | |||
| 71 | } else { | |||||
| 72 | 18 | 15 | my $status = $r->{status} // 'unknown'; | |||
| 73 | 18 | 10 | my $color = | |||
| 74 | $status eq 'ok' ? 'green' : | |||||
| 75 | $status eq 'error' ? 'red' : | |||||
| 76 | # $status eq 'warn' ? 'yellow' : | |||||
| 77 | # $status eq 'unknown' ? 'yellow' : | |||||
| 78 | 'yellow'; # fallback for anything else | |||||
| 79 | ||||||
| 80 | 18 | 14 | print color('bold'); | |||
| 81 | 18 | 158 | print color($color); | |||
| 82 | 18 | 143 | print "[$status] "; | |||
| 83 | 18 | 12 | print color('reset'); | |||
| 84 | 18 | 130 | print color('bold'); | |||
| 85 | 18 | 137 | printf "%s: %s\n", $r->{name} // $key, $r->{notes} // ''; | |||
| 86 | 18 | 12 | print color('reset'); | |||
| 87 | } | |||||
| 88 | ||||||
| 89 | 18 | 147 | if ($r->{'resolution'} && length($r->{'resolution'})) { | |||
| 90 | 7 | 4 | print "\t", $r->{'resolution'}, "\n"; | |||
| 91 | } | |||||
| 92 | } | |||||
| 93 | } | |||||