File: | bin/seo-inspect |
Coverage: | 73.4% |
line | stmt | bran | cond | sub | time | code |
---|---|---|---|---|---|---|
1 | #!/usr/bin/env perl | |||||
2 | ||||||
3 | 2 2 2 | 3644 1 31 | use strict; | |||
4 | 2 2 2 | 4 0 39 | use warnings; | |||
5 | ||||||
6 | 2 2 2 | 353 914 92 | use FindBin qw($Bin); | |||
7 | 2 2 2 | 666 8889 3 | use Getopt::Long qw(GetOptions); | |||
8 | 2 2 2 | 550 26415 53 | use File::Slurp qw(read_file); | |||
9 | 2 2 2 | 6 0 53 | use JSON::MaybeXS; | |||
10 | 2 2 2 | 553 7846 66 | use Term::ANSIColor; | |||
11 | ||||||
12 | 2 2 2 | 382 5 128701 | use SEO::Inspector; | |||
13 | ||||||
14 | # ------------------------------- | |||||
15 | # Command line options | |||||
16 | # ------------------------------- | |||||
17 | 2 | 47118 | my ($url, $file, $json, $no_colour); | |||
18 | 2 | 4 | 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 | 851 | die 'Specify either --url or --file' unless $url || $file; | |||
27 | ||||||
28 | # ------------------------------- | |||||
29 | # Create inspector object | |||||
30 | # ------------------------------- | |||||
31 | 2 | 7 | 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 | 3 | if ($file) { | |||
42 | 2 | 4 | $html = read_file($file); | |||
43 | 2 | 97 | die 'File is empty' unless $html; | |||
44 | ||||||
45 | 2 | 5 | $plugin_results = $inspector->check_html($html); | |||
46 | 2 | 4 | $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 | 2 | if ($json) { | |||
63 | 1 | 28 | print JSON::MaybeXS::encode_json(\%results), "\n"; | |||
64 | } else { | |||||
65 | 1 | 4 | for my $key (sort keys %results) { | |||
66 | 18 | 11 | my $r = $results{$key}; | |||
67 | 18 | 11 | next unless ref($r) eq 'HASH'; | |||
68 | ||||||
69 | 18 | 12 | if($no_colour) { | |||
70 | 0 | 0 | printf "[%s] %s: %s\n", $r->{status} // 'unknown', $r->{name} // $key, $r->{notes} // ''; | |||
71 | } else { | |||||
72 | 18 | 12 | my $status = $r->{status} // 'unknown'; | |||
73 | 18 | 13 | 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 | 146 | print color($color); | |||
82 | 18 | 130 | print "[$status] "; | |||
83 | 18 | 11 | print color('reset'); | |||
84 | 18 | 130 | print color('bold'); | |||
85 | 18 | 142 | printf "%s: %s\n", $r->{name} // $key, $r->{notes} // ''; | |||
86 | 18 | 14 | print color('reset'); | |||
87 | } | |||||
88 | ||||||
89 | 18 | 174 | if ($r->{'resolution'} && length($r->{'resolution'})) { | |||
90 | 7 | 6 | print "\t", $r->{'resolution'}, "\n"; | |||
91 | } | |||||
92 | } | |||||
93 | } |