-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwriter.go
133 lines (125 loc) · 3.43 KB
/
writer.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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package gitkeys
import (
"bytes"
"strings"
"time"
)
//
// WRITER
//
// writeFiles ...
func (r *Repo) writeFiles() error {
ts := time.Now()
if err := r.writeKeyFile(ts); err != nil {
if err != nil {
out(_app + err.Error())
}
}
if err := r.writeLogFile(ts); err != nil {
if err != nil {
out(_app + err.Error())
}
}
if err := r.writeUrlFile(ts); err != nil {
if err != nil {
out(_app + err.Error())
}
}
return nil
}
// writeLogFile ...
func (r *Repo) writeLogFile(ts time.Time) error {
if len(r.changeLog) == 0 {
return nil
}
out(_app + "writing clean log file : " + r.KeyFile + _log)
var buf bytes.Buffer
writeHead(len(r.changeLog), ts, &buf)
buf.WriteString(_doublelinefeed)
// todo:
// buf.WriteString("# Global CheckSum : ")
// buf.WriteString(...)
// buf.WriteString(_linefeed)
for _, u := range r.changeLog {
buf.WriteString(u)
buf.WriteString(_linefeed)
}
old := r.readLogFile()
buf.WriteString(strings.Join(old, _linefeed))
data := buf.Bytes()
if err := saveWriteFile(r.KeyFile+_log, &data); err != nil {
return err
}
return nil
}
// writeUrlFile ...
func (r *Repo) writeUrlFile(ts time.Time) error {
out(_app + "writing clean url file : " + r.KeyFile + _urls)
var buf bytes.Buffer
writeHead(len(r.urlMap), ts, &buf)
buf.WriteString(_doublelinefeed)
// todo:
// buf.WriteString("# Global CheckSum : ")
// buf.WriteString(...)
// buf.WriteString(_linefeed)
if len(r.urlMap) > 0 {
urls := sortedBoolMapIdx(&r.urlMap)
for _, u := range urls {
buf.WriteString(u)
buf.WriteString(_linefeed)
}
}
data := buf.Bytes()
if err := saveWriteFile(r.KeyFile+_urls, &data); err != nil {
return err
}
return nil
}
// writeKeyFile ...
func (r *Repo) writeKeyFile(ts time.Time) error {
out(_app + "writing clean key file : " + r.KeyFile)
var buf bytes.Buffer
writeHead(len(r.idMap), ts, &buf)
buf.WriteString(_globalCheckSum)
buf.WriteString(r.globalCheckSum(ts) + _linefeed)
buf.WriteString(_doublelinefeed)
if len(r.idMap) > 0 {
ownerList := sortedOwnerMapIdx(&r.idMap)
for _, o := range ownerList {
owner := r.idMap[o]
owner.checkSum()
buf.WriteString(_prefix + o + _linefeed)
buf.WriteString(_checkSumKeyset + base64CheckSum(owner.checksum) + _linefeed)
if !owner.empty {
buf.WriteString(owner.keySET.String())
}
buf.WriteString(_doublelinefeed)
}
}
data := buf.Bytes()
if err := saveWriteFile(r.KeyFile, &data); err != nil {
return err
}
return nil
}
// newHead ...
func writeHead(items int, ts time.Time, buf *bytes.Buffer) {
buf.WriteString("# [paepcke.de/gitkeys] DATABASE FILE -= DO NOT EDIT MANUALLY =-\n")
buf.WriteString(_commentline)
buf.WriteString(_tsUnix)
buf.WriteString(itoa64(ts.Unix()) + _linefeed)
buf.WriteString(_tsZulu)
buf.WriteString(ts.Format(time.RFC3339) + _linefeed)
buf.WriteString("# number of targets : " + itoa(items) + _linefeed)
buf.WriteString(_commentline)
buf.WriteString("# WARNING The Global CheckSum covers only keys, urls, logs, timestamps!\n")
buf.WriteString("# WARNING To get a clean and verified file, run 'gitkeys check.\n")
buf.WriteString(_commentline)
}
// writeSignature ... todo as soon [ec or pq signature] formats race settled (signify,minify,age,ssh-plain,sphincs ...)
// func writeSignature() {
// buf.WriteString("# WARNING: The Global Signature covers only keys, urls, logs, timestamps!")
// buf.WriteString(_linefeed)
// buf.WriteString("# Global Signature : ")
// buf.WriteString(_linefeed)
// }