Skip to content

Commit f9af07d

Browse files
committed
Merge pull request #37 from github/better-printer
Add #pretty_print which prints a nice table of data
2 parents 3c1674f + 4ae077c commit f9af07d

File tree

3 files changed

+85
-4
lines changed

3 files changed

+85
-4
lines changed

lib/github-pages-health-check.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
require "resolv"
1010
require "timeout"
1111
require "octokit"
12-
require "json"
13-
require "yaml"
1412
require_relative "github-pages-health-check/version"
1513

1614
if File.exists?(File.expand_path "../.env", File.dirname(__FILE__))
@@ -28,6 +26,7 @@ module HealthCheck
2826
autoload :Domain, "github-pages-health-check/domain"
2927
autoload :Repository, "github-pages-health-check/repository"
3028
autoload :Site, "github-pages-health-check/site"
29+
autoload :Printer, "github-pages-health-check/printer"
3130

3231
# DNS and HTTP timeout, in seconds
3332
TIMEOUT = 10

lib/github-pages-health-check/checkable.rb

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,23 @@ def to_hash
3939
alias_method :to_h, :to_hash
4040

4141
def to_json
42+
require 'json'
4243
to_hash.to_json
4344
end
4445

45-
# Convert the hash to a human-readable key/value pair string
4646
def to_s
47-
to_hash.to_yaml.sub(/\A---\n/, "").gsub(/^:/, "")
47+
printer.simple_string
48+
end
49+
50+
def to_s_pretty
51+
printer.pretty_print
52+
end
53+
alias_method :pretty_print, :to_s_pretty
54+
55+
private
56+
57+
def printer
58+
@printer ||= GitHubPages::HealthCheck::Printer.new(self)
4859
end
4960
end
5061
end
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
module GitHubPages
2+
module HealthCheck
3+
class Printer
4+
PRETTY_LEFT_WIDTH = 11
5+
PRETTY_JOINER = " | "
6+
7+
attr_reader :health_check
8+
9+
def initialize(health_check)
10+
@health_check = health_check
11+
end
12+
13+
def simple_string
14+
require 'yaml'
15+
health_check.to_hash.to_yaml.sub(/\A---\n/, "").gsub(/^:/, "")
16+
end
17+
18+
def pretty_print
19+
values = health_check.to_hash
20+
output = StringIO.new
21+
22+
# Header
23+
output.puts new_line "Domain", "#{values[:uri]}"
24+
output.puts ("-" * (PRETTY_LEFT_WIDTH + 1)) + "|" + "-" * 50
25+
26+
output.puts new_line "DNS", "does not resolve" if not values[:dns_resolves?]
27+
28+
# Valid?
29+
output.write new_line "State", "#{values[:valid?] ? "valid" : "invalid"}"
30+
output.puts " - is #{"NOT " if not values[:served_by_pages?]}served by Pages"
31+
32+
# What's wrong?
33+
output.puts new_line "Reason", "#{values[:reason]}" if not values[:valid?]
34+
output.puts new_line nil, "pointed to user domain" if values[:pointed_to_github_user_domain?]
35+
output.puts new_line nil, "pointed to pages IP" if values[:pointed_to_github_pages_ip?]
36+
37+
# DNS Record info
38+
output.write new_line "Record Type", "#{values[:a_record?] ? "A" : values[:cname_record?] ? "CNAME" : "other"}"
39+
output.puts values[:should_be_a_record?] ? ", should be A record" : ", should be CNAME"
40+
41+
ip_problems = []
42+
ip_problems << "not apex domain" if not values[:apex_domain?]
43+
ip_problems << "invalid domain" if not values[:valid_domain?]
44+
ip_problems << "old ip address used" if values[:old_ip_address?]
45+
output.puts new_line "IP Problems", "#{ip_problems.size > 0 ? ip_problems.join(", ") : "none"} "
46+
47+
if values[:proxied?]
48+
output.puts new_line "Proxied", "yes, through #{values[:cloudflare_ip?] ? "CloudFlare" : "unknown"}"
49+
end
50+
51+
output.puts new_line "Domain", "*.github.com/io domain" if values[:pages_domain?]
52+
53+
output.string
54+
end
55+
56+
def new_line(left = nil, right = nil)
57+
if left and right
58+
ljust(left) + PRETTY_JOINER + right
59+
elsif left
60+
ljust(left)
61+
elsif right
62+
" " * (PRETTY_LEFT_WIDTH + PRETTY_JOINER.size) + right
63+
end
64+
end
65+
66+
def ljust(line)
67+
line.ljust(PRETTY_LEFT_WIDTH)
68+
end
69+
end
70+
end
71+
end

0 commit comments

Comments
 (0)