package inspect import ( "encoding/json" "fmt" "os" "gopkg.in/yaml.v3" "src.lwithers.me.uk/go/rsa/pkg/inspect" ) var ( displayStructures []displayStructure ) func displayStructured(src string, info []inspect.Info) { for _, item := range info { displayStructures = append(displayStructures, displayStructure{ Source: src, Type: displayInfoType(item.Type()), Location: item.Location(), Info: item.Info(), }) } } func displayStructuredJSON() { enc := json.NewEncoder(os.Stdout) enc.SetIndent("", " ") if err := enc.Encode(displayStructures); err != nil { fmt.Fprintln(os.Stderr, "failed to encode as JSON: %v\n", aura.Red(err)) os.Exit(1) } } func displayStructuredYAML() { enc := yaml.NewEncoder(os.Stdout) enc.SetIndent(2) if err := enc.Encode(displayStructures); err != nil { fmt.Fprintln(os.Stderr, "failed to encode as YAML: %v\n", aura.Red(err)) os.Exit(2) } } type displayStructure struct { Source string `json:"source" yaml:"source"` Type string `json:"type" yaml:"type"` Location string `json:"location" yaml:"location"` Info []inspect.Section `json:"info" yaml:"info"` } func displayInfoType(t inspect.Type) string { switch t { case inspect.TypePrivateKey: return "private_key" case inspect.TypePublicKey: return "public_key" case inspect.TypeX509Certificate: return "certificate" case inspect.TypeX509CRL: return "crl" case inspect.TypeX509CSR: return "csr" case inspect.TypeTLSConnectionState: return "tls_state" default: return "???" } }