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
|
package common
import (
"bufio"
"fmt"
"os"
)
type LogType int
const (
LOG_CREATED = LogType(iota)
LOG_APPROVED
LOG_DELETED
)
type LogEntry struct {
Hash string
Type LogType
}
type Log []LogEntry
// senary keeps issues and change requests on-disk and maintains a log of actions over these artifacts i.e. created, approved or deleted.
// logs can in theory be "collapsed" by permanently deleting create and approved entries if a deleted entry also exists
// we don't do this here, but may want to build a standalone command to do this in the future.
func WriteToLog(filename string, hash string, t LogType) error {
file, err := os.OpenFile(filename, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0644)
if err != nil {
return err
}
defer file.Close()
_, err = file.WriteString(fmt.Sprintf("%s %d\n", hash, t))
return err
}
// iterate over each log entry and callback to the provided function
func LogIer(filename string, callback func(LogEntry)) error {
file, err := os.Open(filename)
if err != nil {
return err
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
logline := scanner.Text()
logEntry := LogEntry{}
n, err := fmt.Sscanf(logline, "%s %d", &logEntry.Hash, &logEntry.Type)
// corrupted or malformed log entry, we can't recover from this so abort...
if n != 2 || err != nil {
return err
}
callback(logEntry)
}
return scanner.Err()
}
|