112 lines
2.4 KiB
Go
112 lines
2.4 KiB
Go
package inspect
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
"time"
|
|
"unicode/utf8"
|
|
|
|
"github.com/logrusorgru/aurora/v4"
|
|
"src.lwithers.me.uk/go/rsa/pkg/inspect"
|
|
)
|
|
|
|
var (
|
|
displayedFingerprints = map[string]int{}
|
|
)
|
|
|
|
func displayColoured(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
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// output options
|
|
var auroraHyper bool
|
|
switch os.Getenv("TERM") {
|
|
case "xterm-kitty":
|
|
auroraHyper = true
|
|
}
|
|
a := aurora.New(aurora.WithColors(true), aurora.WithHyperlinks(auroraHyper))
|
|
|
|
// display loop
|
|
for _, item := range info {
|
|
fmt.Printf("════════ %s:%s ════════\n",
|
|
a.BrightBlue(src), a.Blue(item.Location()))
|
|
for _, section := range item.Info() {
|
|
fmt.Println(aurora.Underline(section.Title))
|
|
for _, field := range section.Fields {
|
|
fmt.Printf(" %*s: ", maxKey, a.Yellow(field.Key))
|
|
switch v := field.Value.(type) {
|
|
case int:
|
|
fmt.Print(a.Blue(v))
|
|
|
|
case bool:
|
|
fmt.Print(a.Blue(v))
|
|
|
|
case time.Time:
|
|
var note string
|
|
switch {
|
|
case strings.Contains(field.Key, "from"):
|
|
if v.After(time.Now()) {
|
|
note = aurora.Red("not valid yet").String()
|
|
} else {
|
|
note = aurora.Green("ok").String()
|
|
}
|
|
case strings.Contains(field.Key, "until"):
|
|
if v.After(time.Now()) {
|
|
note = aurora.Green("ok").String()
|
|
} else {
|
|
note = aurora.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", a.Blue(fidx),
|
|
a.Magenta("first occurrence"))
|
|
} else {
|
|
note = fmt.Sprintf("#%d %s", a.Blue(fidx),
|
|
a.Green("already seen"))
|
|
}
|
|
|
|
fmt.Printf("%v [%s]", f, note)
|
|
|
|
default:
|
|
fmt.Print(v)
|
|
}
|
|
fmt.Println("")
|
|
}
|
|
}
|
|
fmt.Println("")
|
|
}
|
|
}
|