-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtodofile.go
62 lines (57 loc) · 1.42 KB
/
todofile.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
package main
import (
"fmt"
"io/ioutil"
"os"
"strconv"
"strings"
"time"
)
func (todo *Todo) readTodos() {
home, _ := os.UserHomeDir()
filepath := fmt.Sprintf("%s/.config/tuihub/todo.csv", home)
file, err := ioutil.ReadFile(filepath)
if err != nil {
e := createFile(home, filepath)
if e != nil {
panic(e)
}
}
todo.todos = nil
for _, line := range strings.Split(string(file), "\n") {
details := strings.Split(line, ",")
if len(details) < 2 {
continue
}
unix, parseErr := strconv.ParseInt(details[1], 10, 64)
ts := time.Unix(unix, 0)
if parseErr != nil {
ts = time.Now()
}
todo.appendTodo(details[0], ts)
}
}
func (todo *Todo) syncBack() {
home, _ := os.UserHomeDir()
filepath := fmt.Sprintf("%s/.config/tuihub/todo.csv", home)
content := ""
for _, item := range todo.todos {
content = fmt.Sprintf("%s%s,%v\n", content, item.task, item.timestamp.Unix())
}
err := ioutil.WriteFile(filepath, []byte(content), 0644)
if err != nil {
return
}
}
func createFile(home string, filepath string) error {
direrr := os.MkdirAll(fmt.Sprintf("%s/.config/tuihub", home), 0750)
if direrr != nil {
return direrr
}
f, createErr := os.Create(filepath)
if createErr != nil {
return createErr
}
f.Close()
return nil
}