-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpersist.go
55 lines (44 loc) · 1.11 KB
/
persist.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
package main
import (
"bufio"
"fmt"
"os"
"time"
)
func LastRunTimeExists() bool {
return lastReadFileExists(GetLastRunFileName())
}
func WriteLastRunTime(timeRun time.Time) {
writeTimeToFile(timeRun, GetLastRunFileName())
}
func ReadLastRunTime() time.Time {
return readTimeFromFile(GetLastRunFileName())
}
func lastReadFileExists(fileName string) bool {
_, err := os.Stat(fileName)
return !os.IsNotExist(err)
}
func writeTimeToFile(timeRun time.Time, fileName string) {
file, err := os.Create(fileName)
if err != nil {
panic(fmt.Errorf("Unable to write lastRunFile: %v ", fileName))
}
_, err = file.WriteString(timeRun.Format(time.RFC3339))
if err != nil {
panic(err)
}
}
func readTimeFromFile(fileName string) time.Time {
open, err := os.Open(fileName)
if err != nil {
panic(fmt.Errorf("Unable to read from %v", fileName))
}
scanner := bufio.NewScanner(open)
scanner.Scan()
scannedText := scanner.Text()
parsedTime, err := time.Parse(time.RFC3339, scannedText)
if err != nil {
panic(fmt.Errorf("unable to parse date from string: %v in file %v", scannedText, fileName))
}
return parsedTime
}