-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoday.go
62 lines (46 loc) · 1.03 KB
/
today.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"
"time"
"github.com/skratchdot/open-golang/open"
)
var (
year, month, day = time.Now().Date()
)
func main() {
path, err := createPath()
checkError(err)
fileName, err := createFile(path)
checkError(err)
err = openFile(fileName)
checkError(err)
}
func createPath() (string, error) {
path := fmt.Sprintf("%d/%d", year, month)
return path, os.MkdirAll(path, 0777)
}
func createFile(path string) (string, error) {
fileName := fmt.Sprintf("%s/%d.md", path, day)
templateContent, err := ioutil.ReadFile("templates/pt.md")
checkError(err)
fileTemplate :=
fmt.Sprintf("# %d/%d/%d", year, month, day) + string(templateContent)
if fileNotExists(fileName) {
err = ioutil.WriteFile(fileName, []byte(fileTemplate), 0644)
}
return fileName, err
}
func openFile(fileName string) error {
return open.Run(fileName)
}
func fileNotExists(filename string) bool {
_, err := os.Stat(filename)
return os.IsNotExist(err)
}
func checkError(err error) {
if err != nil {
panic(err)
}
}