-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmd_db.go
More file actions
72 lines (61 loc) · 1.56 KB
/
cmd_db.go
File metadata and controls
72 lines (61 loc) · 1.56 KB
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 main
import (
"context"
"encoding/json"
"os"
"path/filepath"
"github.com/spf13/cobra"
)
func dbCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "db",
Short: "Database management commands",
}
cmd.AddCommand(syncCmd())
return cmd
}
func syncCmd() *cobra.Command {
return &cobra.Command{
Use: "sync",
Short: "Sync database with metadata files",
Long: "Scans the certs directory for .metadata.json files and imports them into the database if missing.",
RunE: func(cmd *cobra.Command, args []string) error {
count := 0
errors := 0
err := filepath.Walk("./certs", func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.Name() == ".metadata.json" {
// Read metadata
data, err := os.ReadFile(path)
if err != nil {
warnColor.Printf("Failed to read %s: %v\n", path, err)
errors++
return nil
}
var meta CertMetadata
if err := json.Unmarshal(data, &meta); err != nil {
warnColor.Printf("Failed to parse %s: %v\n", path, err)
errors++
return nil
}
// Save to DB
if err := SaveCertificateToDB(context.Background(), meta); err != nil {
warnColor.Printf("Failed to save %s to DB: %v\n", meta.CommonName, err)
errors++
} else {
count++
// fmt.Printf("Synced %s\n", meta.CommonName)
}
}
return nil
})
if err != nil {
return err
}
successColor.Printf("✓ Database sync completed. Processed %d certificates with %d errors.\n", count, errors)
return nil
},
}
}