-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
183 lines (154 loc) · 3.51 KB
/
main.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
package main
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"text/template"
"time"
"github.com/BurntSushi/toml"
"github.com/urfave/cli/v2"
)
const (
name = "qd"
helpName = "Quick Daily Notes"
usage = "qd [section title]"
templateDailyNoteContent = `[[daily notes]]
[[{{.Before}}]] | [[{{.After}}]]
`
)
func command() int {
app := cli.NewApp()
app.Name = name
app.HelpName = helpName
app.Usage = helpName
app.UsageText = usage
app.Version = version
app.Action = run
return handler(app.Run(os.Args))
}
type config struct {
DailyNoteDir string `toml:"dailynotedir"`
Editor string `toml:"editor"`
}
func (cfg *config) load() error {
dir := filepath.Join(os.Getenv("HOME"), ".config", "qd")
if err := os.MkdirAll(dir, 0700); err != nil {
return fmt.Errorf("cannot create directory: %v", err)
}
file := filepath.Join(dir, "config.toml")
_, err := os.Stat(file)
if err == nil {
_, err := toml.DecodeFile(file, cfg)
if err != nil {
return err
}
if len(cfg.DailyNoteDir) == 0 {
return fmt.Errorf("dailynotedir is not found: %v", err)
}
cfg.DailyNoteDir = expandPath(cfg.DailyNoteDir)
return nil
}
if !os.IsNotExist(err) {
return err
}
f, err := os.Create(file)
if err != nil {
return err
}
cfg.Editor = os.Getenv("EDITOR")
if cfg.Editor == "" {
cfg.Editor = "vim"
}
return toml.NewEncoder(f).Encode(cfg)
}
func run(c *cli.Context) error {
var cfg config
err := cfg.load()
if err != nil {
return err
}
now := time.Now()
const dateLayout = "2006-01-02"
dailyNoteName, err := getDailyNoteName(now)
if err != nil {
return err
}
before := now.AddDate(0, 0, -1).Format(dateLayout)
after := now.AddDate(0, 0, 1).Format(dateLayout)
file := filepath.Join(cfg.DailyNoteDir, dailyNoteName)
var title string
if c.Args().Present() {
title = c.Args().First()
}
nowString := now.Format("15:04")
if fileExists(file) {
f, err := os.OpenFile(file, os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
return err
}
if len(title) > 0 {
fmt.Fprintf(f, "\n### %s %s\n", nowString, title)
f.Close()
} else {
fmt.Fprintf(f, "\n### %s\n", nowString)
f.Close()
}
return openEditor(cfg.Editor, file)
}
var tmplString string
if len(title) > 0 {
tmplString = fmt.Sprintf("%s\n### %s %s\n", templateDailyNoteContent, nowString, title)
} else {
tmplString = fmt.Sprintf("%s\n### %s\n", templateDailyNoteContent, nowString)
}
t := template.Must(template.New("qd").Parse(tmplString))
f, err := os.Create(file)
if err != nil {
return err
}
err = t.Execute(f, struct {
Before, After string
}{
before, after,
})
if err != nil {
return err
}
f.Close()
return openEditor(cfg.Editor, file)
}
func expandPath(s string) string {
if len(s) >= 2 && s[0] == '~' && os.IsPathSeparator(s[1]) {
s = filepath.Join(os.Getenv("HOME"), s[2:])
}
return os.Expand(s, os.Getenv)
}
func openEditor(command, file string) error {
cmdargs := file
command += " " + cmdargs
cmd := exec.Command("sh", "-c", command)
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
cmd.Stdin = os.Stdin
return cmd.Run()
}
func handler(err error) int {
if err != nil {
fmt.Fprintf(os.Stderr, "%s: %v\n", os.Args[0], err)
return 1
}
return 0
}
func getDailyNoteName(now time.Time) (string, error) {
const dateLayout = "2006-01-02"
fname := fmt.Sprintf("%s.md", now.Format(dateLayout))
return fname, nil
}
func fileExists(filename string) bool {
_, err := os.Stat(filename)
return err == nil
}
func main() {
os.Exit(command())
}