-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.go
90 lines (70 loc) · 2.02 KB
/
list.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
package main
import (
"io"
"os"
"time"
)
// Tasklist holds the loaded tasks and has methods to modify those tasks.
type Tasklist struct {
filePath string // The path to the local taskfile.
tasks map[int]Task // Tasks mapped to an index.
modified bool // States whether the tasklist has been modified.
serialized []byte // Stores the serialzed tasks before saving.
loaded bool // True if the task has finished loading.
}
// LoadLocal reads the provided taskfile and parses tasks into the tasklist.
func (tl *Tasklist) LoadLocal() {
taskfile := OpenTaskfile(tl.filePath, true)
if taskfile != nil {
defer taskfile.Close()
tl.ParseTasklines(tl.filePath, taskfile)
}
tl.loaded = true
}
// SaveLocal serializes and writes tasks to the provided tasklist.
func (tl *Tasklist) SaveLocal() {
if !ConfigOptions.Reckless {
src := OpenTaskfile(tl.filePath, true)
if src != nil {
defer src.Close()
backupFilePath := GetMetafilePath(".bak", tl.filePath)
dst := CreateTaskfile(backupFilePath)
_, err := io.Copy(dst, src)
if err != nil {
Error(ErrBackupWrite, backupFilePath, err)
}
}
}
if tl.IsEmpty() {
if ConfigOptions.DeleteIfEmpty {
err := os.Remove(tl.filePath)
if err != nil {
Warn("Could not delete empty taskfile \"%s\": %v\n", tl.filePath, err)
}
} else {
err := os.Truncate(tl.filePath, 0)
if err != nil {
Error(ErrTaskfileWrite, tl.filePath, err)
}
}
return
}
taskfile := CreateTaskfile(tl.filePath)
defer taskfile.Close()
_, err := taskfile.Write(tl.serialized)
if err != nil {
Error(ErrTaskfileWrite, tl.filePath, err)
}
}
// MTimeAfter determines if the last modification time for a taskfile is after
// another time object.
func (tl *Tasklist) MTimeAfter(compare time.Time) bool {
stat, err := os.Stat(tl.filePath)
if err != nil {
if !os.IsNotExist(err) {
Warn("Could not retrieve mtime for taskfile \"%s\": %v", tl.filePath, err)
}
return false
}
return StripNanoFromTime(stat.ModTime()).After(compare)
}