-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
113 lines (94 loc) · 2.76 KB
/
main.go
File metadata and controls
113 lines (94 loc) · 2.76 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package main
import (
"crypto/tls"
"encoding/json"
"log"
"net/http"
"time"
"consuldiff/consulkv"
"consuldiff/gitutil"
"consuldiff/kvtypes"
"consuldiff/state"
"consuldiff/storage"
"github.com/hashicorp/consul/api"
)
func main() {
// Initialize the state
s := state.InitState()
// Setup git repository configuration
if s.GitConfig.Enabled {
gitutil.SetupGitRepo(*s)
}
config := api.DefaultConfig()
if s.TLSSkipVerify {
// Custom HTTP client with TLS skip verify
config.HttpClient = &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
},
}
}
log.Println("Starting Consul KV Diff Tool...")
log.Printf("Polling Consul KV every %s", s.PollInterval)
// Init Consul client
client, err := api.NewClient(config)
if err != nil {
log.Fatalf("Error creating Consul client: %v", err)
}
// Check if the storage file exists and read it
fileCheck, err := storage.ReadMapFromFile(s.GitConfig.Filename)
checkBytes, err := json.Marshal(fileCheck)
if err != nil {
// handle error
}
check := string(checkBytes)
if check == "null" {
log.Printf("No previous KV state found, creating new file at %s", s.GitConfig.Filename)
firstRun, err := consulkv.FetchKV(client, s.KeyPrefix)
if err != nil {
log.Fatalf("Error fetching initial KV state: %v", err)
}
err = storage.WriteMapToFile(s.GitConfig.Filename, firstRun)
if err != nil {
log.Fatalf("Error writing initial KV state to file: %v", err)
}
}
log.Printf("Using storage file: %s", s.GitConfig.Filename)
log.Println("Starting KV polling for diff...")
for {
log.Println("Fetching current KV state from Consul...")
s.Current, err = consulkv.FetchKV(client, s.KeyPrefix)
if err != nil {
log.Fatalf("Error fetching KV: %v", err)
}
filepath := s.GitConfig.RepoPath + "/" + s.GitConfig.Filename
s.Previous, err = storage.ReadMapFromFile(filepath)
if err != nil {
log.Fatalf("Error reading previous KV state: %v", err)
s.Previous = []kvtypes.KVExportEntry{}
}
if s.Previous != nil {
log.Println("Comparing current KV state with previous state from file: ", filepath)
consulkv.LogKVDiff(s.Previous, s.Current, filepath)
}
log.Println("Writing current base64 KV state to file...")
kvB64, err := consulkv.FetchKVBase64(client, s.KeyPrefix)
if err != nil {
log.Fatalf("Error fetching KV Base64: %v", err)
}
err = storage.WriteMapToFile(filepath+".b64", kvB64)
if err != nil {
log.Fatalf("Error writing Base64 KV state to file: %v", err)
}
if s.GitConfig.Enabled {
log.Println("Git is enabled, committing changes if any...")
err := gitutil.GitCommitAndPush(*s)
if err != nil {
log.Fatalf("Error committing and pushing changes to Git: %v", err)
}
}
time.Sleep(s.PollInterval)
}
}