1
0
Fork 0
mirror of https://github.com/Luzifer/vault-openvpn.git synced 2024-11-09 16:50:04 +00:00

Allow listin certs as JSON for automated processing

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2018-06-15 00:23:10 +02:00
parent c86c029f12
commit 825254e7f3
Signed by: luzifer
GPG key ID: DC2729FDD34BE99E
2 changed files with 21 additions and 13 deletions

View file

@ -1,6 +1,7 @@
package cmd package cmd
import ( import (
"encoding/json"
"os" "os"
"sort" "sort"
@ -25,14 +26,11 @@ func init() {
listCmd.Flags().StringVar(&cfg.Sort, "sort", "fqdn", "How to sort list output (fqdn, issuedate, expiredate)") listCmd.Flags().StringVar(&cfg.Sort, "sort", "fqdn", "How to sort list output (fqdn, issuedate, expiredate)")
listCmd.Flags().Bool("list-expired", false, "Also list expired certificates") listCmd.Flags().Bool("list-expired", false, "Also list expired certificates")
listCmd.Flags().String("format", "table", "Format to display the certificates in (table, json)")
viper.BindPFlags(listCmd.Flags()) viper.BindPFlags(listCmd.Flags())
} }
func listCertificates() error { func listCertificates() error {
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"FQDN", "Not Before", "Not After", "Serial"})
table.SetBorder(false)
lines := []listCertificatesTableRow{} lines := []listCertificatesTableRow{}
certs, err := fetchCertificatesFromVault(viper.GetBool("list-expired")) certs, err := fetchCertificatesFromVault(viper.GetBool("list-expired"))
@ -65,6 +63,15 @@ func listCertificates() error {
} }
}) })
switch viper.GetString("format") {
case "json":
return json.NewEncoder(os.Stdout).Encode(lines)
default:
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"FQDN", "Not Before", "Not After", "Serial"})
table.SetBorder(false)
for _, line := range lines { for _, line := range lines {
table.Append(line.ToLine()) table.Append(line.ToLine())
} }
@ -72,3 +79,4 @@ func listCertificates() error {
table.Render() table.Render()
return nil return nil
} }
}

View file

@ -10,10 +10,10 @@ type templateVars struct {
} }
type listCertificatesTableRow struct { type listCertificatesTableRow struct {
FQDN string FQDN string `json:"fqdn"`
NotBefore time.Time NotBefore time.Time `json:"not_before"`
NotAfter time.Time NotAfter time.Time `json:"not_after"`
Serial string Serial string `json:"serial"`
} }
func (l listCertificatesTableRow) ToLine() []string { func (l listCertificatesTableRow) ToLine() []string {