-
Notifications
You must be signed in to change notification settings - Fork 44
/
dump.go
63 lines (53 loc) · 1.09 KB
/
dump.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
package cli
import (
"database/sql"
"fmt"
"os"
"github.com/cloudflare/cfssl_trust/dump"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var dumpCmd = &cobra.Command{
Use: "dump",
Short: "Dump a certificate to standard output or file.",
Long: "Dump a certificate to standard output or file given its SKI.",
Run: dumper,
}
func init() {
rootCmd.AddCommand(dumpCmd)
}
func dumper(cmd *cobra.Command, args []string) {
if len(args) == 0 {
os.Exit(0)
}
dbPath := viper.GetString("database.path")
db, err := sql.Open("sqlite3", dbPath)
if err != nil {
fmt.Fprintf(os.Stderr, "[!] %s\n", err)
os.Exit(1)
}
tx, err := db.Begin()
if err != nil {
fmt.Fprintf(os.Stderr, "[!] %s\n", err)
os.Exit(1)
}
defer func() {
if err == nil {
err = tx.Commit()
if err != nil {
fmt.Fprintf(os.Stderr, "[!] failed to commit transaction: %s\n", err)
os.Exit(1)
}
} else {
tx.Rollback()
}
}()
for _, ski := range args {
cert, err := dump.CertPEM(tx, ski)
if err != nil {
fmt.Fprintf(os.Stderr, "[!] %s\n", err)
os.Exit(1)
}
fmt.Println(string(cert))
}
}