-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshow.go
72 lines (61 loc) · 1.54 KB
/
show.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package cmd
import (
"encoding/pem"
"fmt"
"os"
"text/tabwriter"
"github.com/chenzhiwei/certctl/pkg/cert"
"github.com/spf13/cobra"
)
var (
showCmd = &cobra.Command{
Use: "show cert-or-csr-filepath",
Short: "Show certificate or certificate request info",
Args: cobra.MinimumNArgs(1),
RunE: func(_ *cobra.Command, args []string) error {
if err := runShow(args); err != nil {
return err
}
return nil
},
}
)
func runShow(args []string) error {
file := args[0]
bytes, err := os.ReadFile(file)
if err != nil {
return err
}
block, _ := pem.Decode(bytes)
if block == nil {
return fmt.Errorf("Failed to parse certificate or csr")
}
var result []map[string]string
if block.Type == cert.CertReqBlockType {
result, err = cert.GetCertRequestInfo(bytes)
if err != nil {
return err
}
} else if block.Type == cert.CertBlockType {
result, err = cert.GetCertInfo(bytes)
if err != nil {
return err
}
} else {
return fmt.Errorf("Unsupported type: %s", block.Type)
}
writer := tabwriter.NewWriter(os.Stdout, 0, 8, 1, '\t', tabwriter.AlignRight)
for _, info := range result {
for k, v := range info {
fmt.Fprintf(writer, "%s\t%s\n", k, v)
}
}
writer.Flush()
// a certificate/request can contain too many tings, no need to reinvent the wheel
if block.Type == cert.CertReqBlockType {
fmt.Printf("\nCheck more info with: openssl req -noout -text -in %s\n", file)
} else if block.Type == cert.CertBlockType {
fmt.Printf("\nCheck more info with: openssl x509 -noout -text -in %s\n", file)
}
return nil
}