mirror of
https://github.com/Luzifer/vault-openvpn.git
synced 2024-12-25 06:11:21 +00:00
Allow listin certs as JSON for automated processing
Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
parent
c86c029f12
commit
825254e7f3
2 changed files with 21 additions and 13 deletions
26
cmd/list.go
26
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
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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 {
|
||||
|
|
Loading…
Reference in a new issue