diff options
author | Luke Shumaker <lukeshu@sbcglobal.net> | 2016-10-10 23:33:17 -0400 |
---|---|---|
committer | Luke Shumaker <lukeshu@sbcglobal.net> | 2016-10-10 23:33:17 -0400 |
commit | 9583d8ee78f233ec313f5586ead1a654a7b5ebbc (patch) | |
tree | 7020e959b0ca6f23216462729e9fb1c0f8e6238c | |
parent | f5892c09b6a8cfb6d15c18fe641db5bf40492941 (diff) |
colors!
-rw-r--r-- | pem2html.go | 74 |
1 files changed, 63 insertions, 11 deletions
diff --git a/pem2html.go b/pem2html.go index 2969e7e..b1bf36c 100644 --- a/pem2html.go +++ b/pem2html.go @@ -4,11 +4,11 @@ import ( "crypto/x509" "encoding/pem" "fmt" + "html/template" "io/ioutil" "os" - "time" - "html/template" "sort" + "time" ) func handleErr(err error, str string, a ...interface{}) { @@ -25,7 +25,11 @@ func handleBool(ok bool, str string, a ...interface{}) { } } -var tmpl = template.Must(template.New("pem2html").Parse(`<!DOCTYPE html> +var tmpl = template.Must(template.New("pem2html"). + Funcs(template.FuncMap{ + "red": red, + "green": green, + }).Parse(`<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> @@ -56,7 +60,7 @@ var tmpl = template.Must(template.New("pem2html").Parse(`<!DOCTYPE html> background-color: #F3F3F3; } tr:hover td { - background-color: #AAAAF3; + background-color: #AAAAF3 !important; } td a { padding: 0.1em 0.25em; @@ -78,17 +82,65 @@ var tmpl = template.Must(template.New("pem2html").Parse(`<!DOCTYPE html> </tr> {{range $cert := .}} <tr> - <td><a href="{{$cert.Url}}">{{$cert.Updated.Local.Format "2006-01-02 15:04:05"}}</a></td> - <td><a href="{{$cert.Url}}">{{$cert.X509.NotBefore.Local.Format "2006-01-02"}}</a></td> - <td><a href="{{$cert.Url}}">{{$cert.X509.NotAfter.Local.Format "2006-01-02"}}</a></td> - <td><a href="{{$cert.Url}}">{{$cert.X509.Subject.CommonName}}</a></td> - <td><a href="{{$cert.Url}}">{{$cert.X509.Issuer.Organization}}</a></td> + <td style="background-color: {{$cert.Updated | green}}"><a target="_blank" href="{{$cert.Url}}">{{$cert.Updated.Local.Format "2006-01-02 15:04:05"}}</a></td> + <td><a target="_blank" href="{{$cert.Url}}">{{$cert.X509.NotBefore.Local.Format "2006-01-02"}}</a></td> + <td style="background-color: {{$cert.X509.NotAfter | red}}"><a target="_blank" href="{{$cert.Url}}">{{$cert.X509.NotAfter.Local.Format "2006-01-02"}}</a></td> + <td><a target="_blank" href="{{$cert.Url}}">{{$cert.X509.Subject.CommonName}}</a></td> + <td><a target="_blank" href="{{$cert.Url}}">{{$cert.X509.Issuer.Organization}}</a></td> </tr> {{end}} </table> </body> </html>`)) +var now = time.Now() + +type interpolation struct { + ta, tb time.Time + ba, bb byte +} + +func (i interpolation) interpolate(tc time.Time) byte { + db := i.tb.Sub(i.ta) + dc := tc.Sub(i.ta) + + pct := float64(dc) / float64(db) + if pct < 0 { + pct = 0 + } else if pct > 1 { + pct = 1 + } + + sb := int16(i.bb) - int16(i.ba) + sc := int16(pct * float64(sb)) + + return byte(int16(i.ba) + sc) +} + +var daysago = interpolation{ + ta: now.AddDate(0, 0, -30), + tb: now, + ba: 0xF3, + bb: 0x00, +} + +var daysuntil = interpolation{ + ta: now, + tb: now.AddDate(0, 0, 30), + ba: 0x00, + bb: 0xF3, +} + +func green(t time.Time) string { + b := daysago.interpolate(t) + return fmt.Sprintf("#%02X%02X%02X", b, 0xF3, b) +} + +func red(t time.Time) string { + b := daysuntil.interpolate(t) + return fmt.Sprintf("#%02X%02X%02X", 0xF3, b, b) +} + type Cert struct { Url string Updated time.Time @@ -97,7 +149,7 @@ type Cert struct { type Certs []Cert - // Len is the number of elements in the collection. +// Len is the number of elements in the collection. func (l Certs) Len() int { return len(l) } @@ -142,5 +194,5 @@ func main() { } sort.Sort(certs) - tmpl.Execute(os.Stdout, certs) + handleErr(tmpl.Execute(os.Stdout, certs), "Could not execute template: %v\n") } |