From 825254e7f302ccc2a55e45cb79b0f6a612ea58bc Mon Sep 17 00:00:00 2001 From: Knut Ahlers Date: Fri, 15 Jun 2018 00:23:10 +0200 Subject: [PATCH] Allow listin certs as JSON for automated processing Signed-off-by: Knut Ahlers --- cmd/list.go | 26 +++++++++++++++++--------- cmd/structs.go | 8 ++++---- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/cmd/list.go b/cmd/list.go index 6230b3d..b681cf6 100644 --- a/cmd/list.go +++ b/cmd/list.go @@ -1,6 +1,7 @@ package cmd import ( + "encoding/json" "os" "sort" @@ -25,14 +26,11 @@ func init() { 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().String("format", "table", "Format to display the certificates in (table, json)") viper.BindPFlags(listCmd.Flags()) } func listCertificates() error { - table := tablewriter.NewWriter(os.Stdout) - table.SetHeader([]string{"FQDN", "Not Before", "Not After", "Serial"}) - table.SetBorder(false) - lines := []listCertificatesTableRow{} certs, err := fetchCertificatesFromVault(viper.GetBool("list-expired")) @@ -65,10 +63,20 @@ func listCertificates() error { } }) - for _, line := range lines { - table.Append(line.ToLine()) - } + switch viper.GetString("format") { + case "json": + return json.NewEncoder(os.Stdout).Encode(lines) - table.Render() - return nil + default: + table := tablewriter.NewWriter(os.Stdout) + table.SetHeader([]string{"FQDN", "Not Before", "Not After", "Serial"}) + table.SetBorder(false) + + for _, line := range lines { + table.Append(line.ToLine()) + } + + table.Render() + return nil + } } diff --git a/cmd/structs.go b/cmd/structs.go index db4b386..6ae2e71 100644 --- a/cmd/structs.go +++ b/cmd/structs.go @@ -10,10 +10,10 @@ type templateVars struct { } type listCertificatesTableRow struct { - FQDN string - NotBefore time.Time - NotAfter time.Time - Serial string + FQDN string `json:"fqdn"` + NotBefore time.Time `json:"not_before"` + NotAfter time.Time `json:"not_after"` + Serial string `json:"serial"` } func (l listCertificatesTableRow) ToLine() []string {