|
| 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