rsa/cmd/inspect/display_text.go

102 lines
2.2 KiB
Go

package inspect
import (
"fmt"
"strings"
"time"
"unicode/utf8"
"src.lwithers.me.uk/go/rsa/pkg/inspect"
)
var (
displayedFingerprints = map[string]int{}
)
func displayText(src string, info []inspect.Info) {
// compute max key length, for nicely aligning columns
var maxKey int
for _, item := range info {
for _, section := range item.Info() {
for _, field := range section.Fields {
l := utf8.RuneCountInString(field.Key)
if l > maxKey {
maxKey = l
}
}
}
}
// display loop
for _, item := range info {
fmt.Printf("════════ %s:%s ════════\n",
aura.BrightBlue(src), aura.Blue(item.Location()))
for _, section := range item.Info() {
fmt.Println(aura.Underline(section.Title))
for _, field := range section.Fields {
fmt.Printf(" %*s: ", maxKey, aura.Yellow(field.Key))
switch v := field.Value.(type) {
case int:
fmt.Print(aura.Blue(v))
case bool:
fmt.Print(aura.Blue(v))
case time.Time:
var note string
switch {
case strings.Contains(field.Key, "from"):
if v.After(time.Now()) {
note = aura.Red("not valid yet").String()
} else {
note = aura.Green("ok").String()
}
case strings.Contains(field.Key, "until"):
if v.After(time.Now()) {
note = aura.Green("ok").String()
} else {
note = aura.Red("expired").String()
}
}
fmt.Printf("%s %s", v.Format(time.RFC3339), note)
case []string:
for i, s := range v {
fmt.Print(s)
if i < len(v)-1 {
fmt.Printf("\n%*s", maxKey+4, "")
}
}
case inspect.Fingerprint:
f := v.String()
fidx := displayedFingerprints[f]
var firstSeen bool
if fidx == 0 {
firstSeen = true
fidx = 1 + len(displayedFingerprints)
displayedFingerprints[f] = fidx
}
var note string
if firstSeen {
note = fmt.Sprintf("#%d %s", aura.Blue(fidx),
aura.Magenta("first occurrence"))
} else {
note = fmt.Sprintf("#%d %s", aura.Blue(fidx),
aura.Green("already seen"))
}
fmt.Printf("%v [%s]", f, note)
default:
fmt.Print(v)
}
fmt.Println("")
}
}
fmt.Println("")
}
}