/
filesystem.go
131 lines (106 loc) · 3.11 KB
/
filesystem.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
package main
import (
"fmt"
"os"
"os/exec"
"os/user"
"path"
"path/filepath"
"strings"
"time"
)
type FileSystem struct {
config *Config
}
func NewFileSystem(config *Config) *FileSystem {
return &FileSystem{
config: config,
}
}
func (f *FileSystem) bootstrap(path string) error {
dir := filepath.Dir(path)
if _, err := os.Stat(dir); os.IsNotExist(err) {
if err := os.MkdirAll(dir, 0777); err != nil {
return err
}
}
return nil
}
func (f *FileSystem) TemplateFilepath() string {
current, err := user.Current()
if err != nil {
return ""
}
path := path.Join(current.HomeDir, ".datebook.md")
if _, err := os.Stat(path); os.IsNotExist(err) {
return ""
}
return path
}
func (f FileSystem) DateFilepath(date time.Time) string {
filename := fmt.Sprintf("%s_%s_%d_%d.md", date.Weekday(), date.Month().String(), date.Day(), date.Year())
path := path.Join(f.MonthDirectory(date), strings.ToLower(filename))
f.bootstrap(path)
return path
}
func (f FileSystem) MonthDirectory(date time.Time) string {
year, month, _ := date.Date()
monthDir := strings.ToLower(fmt.Sprintf("%d_%s", month, month.String()))
yearDir := fmt.Sprintf("%d", year)
path := path.Join(f.config.Root, yearDir, monthDir)
f.bootstrap(path)
return path
}
func (f FileSystem) WeekFilepath(date time.Time) string {
start := date.AddDate(0, 0, -1*int(date.Weekday()))
year, month, day := start.Date()
filename := strings.ToLower(fmt.Sprintf("week_%s_%d_%d.md", month.String(), day, year))
path := path.Join(f.MonthDirectory(start), filename)
f.bootstrap(path)
return path
}
func (f FileSystem) LongtermFilepath() string {
path := path.Join(f.config.Root, "longterm.md")
f.bootstrap(path)
return path
}
func (f FileSystem) UpdateReadme(srcPath string) error {
// we want this symlink to work with Github so it needs to be relative, as of the root of the datebook dir
if err := os.Chdir(f.config.Root); err != nil {
return err
}
readmePath := "README.md"
// remove the current symlinking, ignoring errors if it doesn't exist
os.Remove(readmePath)
// now we chop off the full Root path from src path so it is relative now...
srcPath = strings.Replace(srcPath, f.config.Root, ".", 1)
if err := os.Symlink(srcPath, readmePath); err != nil {
return err
}
return nil
}
func (f FileSystem) Commit(date time.Time) error {
if _, err := os.Stat(path.Join(f.config.Root, ".git")); os.IsNotExist(err) {
fmt.Println("Datebook filesystem is not a git repository")
return nil
}
commitMsg := fmt.Sprintf("%s %s %d", date.Weekday(), date.Month().String(), date.Year())
os.Chdir(f.config.Root)
if err := exec.Command("git", "add", "--all").Run(); err != nil {
return err
}
exec.Command("git", "commit", "-m", commitMsg).Run()
fmt.Println(fmt.Sprintf("Committed changes to datebook filesystem for %s %s %d %d",
date.Weekday(), date.Month().String(), date.Day(), date.Year()))
return nil
}
func (f FileSystem) EditFile(path string) error {
command := exec.Command(os.Getenv("EDITOR"), path)
command.Stdin = os.Stdin
command.Stdout = os.Stdout
command.Stderr = os.Stderr
if err := command.Run(); err != nil {
return err
}
return nil
}