Skip to content

Commit 6db9263

Browse files
committed
enable stdin when show a cert/csr
1 parent ed29d60 commit 6db9263

1 file changed

Lines changed: 11 additions & 6 deletions

File tree

cmd/show.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cmd
33
import (
44
"encoding/pem"
55
"fmt"
6+
"io"
67
"os"
78
"text/tabwriter"
89

@@ -12,7 +13,7 @@ import (
1213

1314
var (
1415
showCmd = &cobra.Command{
15-
Use: "show cert-or-csr-filepath",
16+
Use: "show cert-or-csr-filepath or - from stdin",
1617
Short: "Show certificate or certificate request info",
1718
Args: cobra.MinimumNArgs(1),
1819
RunE: func(_ *cobra.Command, args []string) error {
@@ -26,25 +27,29 @@ var (
2627

2728
func runShow(args []string) error {
2829
file := args[0]
29-
bytes, err := os.ReadFile(file)
30+
data, err := os.ReadFile(file)
3031
if err != nil {
31-
return err
32+
if file == "-" {
33+
data, err = io.ReadAll(os.Stdin)
34+
} else {
35+
return err
36+
}
3237
}
3338

34-
block, _ := pem.Decode(bytes)
39+
block, _ := pem.Decode(data)
3540
if block == nil {
3641
return fmt.Errorf("Failed to parse certificate or csr")
3742
}
3843

3944
var result []map[string]string
4045

4146
if block.Type == cert.CertReqBlockType {
42-
result, err = cert.GetCertRequestInfo(bytes)
47+
result, err = cert.GetCertRequestInfo(data)
4348
if err != nil {
4449
return err
4550
}
4651
} else if block.Type == cert.CertBlockType {
47-
result, err = cert.GetCertInfo(bytes)
52+
result, err = cert.GetCertInfo(data)
4853
if err != nil {
4954
return err
5055
}

0 commit comments

Comments
 (0)