/
index.go
248 lines (216 loc) · 4.39 KB
/
index.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package main
import (
"bufio"
"errors"
"fmt"
"os"
"strconv"
"strings"
"time"
)
const D rune = '\x01'
const Syncd rune = '✓'
const UnixDate string = "Mon Jan _2 15:04:05 MST 2006"
type Index struct {
FilePath string
}
func (r Index) Write(e IndexEntry) (err error) {
file, err := os.OpenFile(r.FilePath, os.O_RDWR|os.O_APPEND|os.O_CREATE, 0644)
defer file.Close()
if err != nil {
return err
}
record := toHistLine(e)
file.WriteString(record)
return nil
}
func (r Index) MarkSynced() (err error) {
file, err := os.OpenFile(r.FilePath, os.O_RDWR, 0644)
if err != nil {
return
}
var lines []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
if scanner.Err() != nil {
return err
}
file.Close()
os.Remove(r.FilePath)
file, err = os.Create(r.FilePath)
if err != nil {
return
}
w := bufio.NewWriter(file)
for _, line := range lines {
for index, runeval := range line {
if index > 0 {
break
}
if runeval != Syncd {
fmt.Fprintf(w, "%c%s\n", Syncd, line[1:len(line)-1])
} else {
fmt.Fprintf(w, "%s\n", line)
}
}
}
err = w.Flush()
file.Close()
return
}
func (r Index) GetUnsynced() (result []IndexEntry, err error) {
file, err := os.OpenFile(r.FilePath, os.O_RDWR, 0644)
if err != nil {
return
}
state, err := file.Stat()
if err != nil {
return
}
// exit if file is effectively empty
if state.Size() < 8 {
return nil, nil
}
reader := bufio.NewReader(file)
scanner := bufio.NewScanner(reader)
for scanner.Scan() {
for index, runeval := range scanner.Text() {
if index > 0 {
break
}
if runeval != Syncd {
line := scanner.Text()
invocation, parseErr := parseToEntry(line)
if parseErr != nil {
err = parseErr
return
}
result = append(result, invocation)
}
}
}
return
}
func (r Index) Flush() {
file, err := os.OpenFile(r.FilePath, os.O_APPEND|os.O_WRONLY, 0600)
if err != nil {
return
}
defer file.Close()
if _, err = file.WriteString("\n"); err != nil {
panic(err)
}
}
func (r Index) getLastLine() (last string) {
file, err := os.OpenFile(r.FilePath, os.O_RDONLY, 0644)
if err != nil {
return
}
reader := bufio.NewReader(file)
scanner := bufio.NewScanner(reader)
for scanner.Scan() {
last = scanner.Text()
}
defer file.Close()
return last
}
func (r Index) canWriteResult() (valid bool) {
line := r.getLastLine()
for index, runeval := range line {
if index > 0 {
break
}
if runeval == Syncd || runeval == 'U' {
valid = true
} else {
valid = false
}
}
if !valid {
return
}
return !r.canWriteContext()
}
func (r Index) canWriteContext() (valid bool) {
line := r.getLastLine()
if line == "" {
return true
}
for _, runeval := range line {
if runeval != D {
valid = true
} else {
valid = false
}
}
return valid
}
func parseToEntry(line string) (e IndexEntry, err error) {
tokens := strings.FieldsFunc(line, func(c rune) bool {
return c == D
})
e = IndexEntry{}
if tokens[0] == strconv.QuoteRune(Syncd) {
e.IsSynced = true
} else if tokens[0] == "U" {
e.IsSynced = false
} else {
errstr := "Your gohst index file appears malformed with line:\n" + line
return IndexEntry{}, errors.New(errstr)
}
e.Timestamp, err = time.Parse(UnixDate, tokens[1])
if err != nil {
return
}
e.User = tokens[2]
e.Host = tokens[3]
e.Shell = tokens[4]
e.Directory = tokens[5]
e.Command = tokens[6]
e.Tags = strings.Fields(tokens[7][1 : len(tokens[7])-1])
// if we have a command with no exit code, it means the shell probably
// exited between logging context and logging exit code. This could happen
// if the command was, for example, 'exit'. We assume it returned 0
var exitcode int
if len(tokens) < 9 {
exitcode = 0
} else {
exitcode, err = strconv.Atoi(tokens[8])
if err != nil {
return
}
}
e.Status = int8(exitcode)
e.HasStatus = true
return
}
func toHistLine(e IndexEntry) (record string) {
if e.Command != "" {
if e.Host == "" {
e.Host = "null"
}
if e.User == "" {
e.User = "null"
}
if e.Shell == "" {
e.Shell = "null"
}
if e.Directory == "" {
e.Directory = "null"
}
record = fmt.Sprintf("%c%c%v%c%s%c%s%c%s%c%s%c%s%c%s%c", 'U',
D, e.Timestamp.Format(time.UnixDate),
D, e.User,
D, e.Host,
D, e.Shell,
D, e.Directory,
D, e.Command,
D, e.Tags, D)
}
if e.HasStatus {
record = fmt.Sprintf("%s%d%c", record, e.Status, D)
}
return
}