|
| 1 | +package result |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/csv" |
| 6 | + "encoding/json" |
| 7 | + "fmt" |
| 8 | + "strconv" |
| 9 | + "strings" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/olekukonko/tablewriter" |
| 13 | +) |
| 14 | + |
| 15 | +type Certificate struct { |
| 16 | + IssuerCaID int `json:"issuer_ca_id"` |
| 17 | + IssuerName string `json:"issuer_name"` |
| 18 | + CommonName string `json:"common_name"` |
| 19 | + NameValue string `json:"name_value"` |
| 20 | + ID int `json:"id"` |
| 21 | + EntryTimestamp time.Time `json:"entry_timestamp"` |
| 22 | + NotBefore time.Time `json:"not_before"` |
| 23 | + NotAfter time.Time `json:"not_after"` |
| 24 | + SerialNumber string `json:"serial_number"` |
| 25 | +} |
| 26 | + |
| 27 | +type CertResult []Certificate |
| 28 | + |
| 29 | +func (r CertResult) Table() string { |
| 30 | + res := new(bytes.Buffer) |
| 31 | + table := tablewriter.NewWriter(res) |
| 32 | + |
| 33 | + info := []string{"Matching", "Logged At", "Not Before", "Not After", "Issuer"} |
| 34 | + table.SetHeader(info) |
| 35 | + table.SetFooter(info) |
| 36 | + |
| 37 | + blue := tablewriter.Color(tablewriter.FgHiBlueColor) |
| 38 | + yellow := tablewriter.Color(tablewriter.FgHiYellowColor) |
| 39 | + white := tablewriter.Color(tablewriter.FgWhiteColor) |
| 40 | + |
| 41 | + table.SetHeaderColor(blue, blue, blue, blue, blue) |
| 42 | + table.SetFooterColor(blue, blue, blue, blue, blue) |
| 43 | + table.SetColumnColor(yellow, white, white, white, white) |
| 44 | + |
| 45 | + for _, cert := range r { |
| 46 | + table.Append([]string{ |
| 47 | + cert.NameValue, |
| 48 | + cert.EntryTimestamp.String()[0:10], |
| 49 | + cert.NotBefore.String()[0:10], |
| 50 | + cert.NotAfter.String()[0:10], |
| 51 | + strings.Trim(strings.Split(strings.Split(cert.IssuerName, "O=")[1], ",")[0], "\""), |
| 52 | + }) |
| 53 | + } |
| 54 | + |
| 55 | + table.SetRowLine(true) |
| 56 | + table.SetRowSeparator("—") |
| 57 | + table.Render() |
| 58 | + |
| 59 | + return res.String() |
| 60 | +} |
| 61 | + |
| 62 | +func (r CertResult) JSON() (string, error) { |
| 63 | + res, err := json.MarshalIndent(r, "", "\t") |
| 64 | + if err != nil { |
| 65 | + return "", fmt.Errorf("failed to marshal results: %s", err) |
| 66 | + } |
| 67 | + |
| 68 | + return string(res), nil |
| 69 | +} |
| 70 | + |
| 71 | +func (r CertResult) CSV() (string, error) { |
| 72 | + res := new(bytes.Buffer) |
| 73 | + w := csv.NewWriter(res) |
| 74 | + |
| 75 | + err := w.Write([]string{ |
| 76 | + "issuer_ca_id", "issuer_name", "common_name", "name_value", "id", |
| 77 | + "entry_timestamp", "not_before", "not_after", "serial_number", |
| 78 | + }) |
| 79 | + if err != nil { |
| 80 | + return "", fmt.Errorf("failed to write CSV headers: %s", err) |
| 81 | + } |
| 82 | + |
| 83 | + for _, v := range r { |
| 84 | + err = w.Write([]string{ |
| 85 | + strconv.Itoa(v.IssuerCaID), |
| 86 | + v.IssuerName, |
| 87 | + v.CommonName, |
| 88 | + v.NameValue, |
| 89 | + strconv.Itoa(v.ID), |
| 90 | + v.EntryTimestamp.String(), |
| 91 | + v.NotBefore.String(), |
| 92 | + v.NotAfter.String(), |
| 93 | + v.SerialNumber}) |
| 94 | + if err != nil { |
| 95 | + return "", fmt.Errorf("failed to write CSV content: %s", err) |
| 96 | + } |
| 97 | + } |
| 98 | + w.Flush() |
| 99 | + |
| 100 | + return res.String(), nil |
| 101 | +} |
| 102 | + |
| 103 | +func (r CertResult) Size() int { return len(r) } |
0 commit comments