-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
91 lines (75 loc) · 1.49 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
package main
import (
"fmt"
"io"
"os"
"strconv"
"strings"
"time"
"github.com/urfave/cli"
)
func main() {
app := cli.NewApp()
app.Name = "agemplate"
app.Version = Version
app.Usage = "generate agenda template in date directory"
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "template, t",
Value: "template.md",
Usage: "path of agenda template",
},
}
app.Action = func(c *cli.Context) error {
arg := c.Args().Get(0)
dir, agenda := divide(arg)
mkdir(dir)
tamplate := c.String("template")
copyy(tamplate, agenda)
return nil
}
app.Run(os.Args)
}
func mkdir(path string) {
if err := os.MkdirAll(path, os.ModeDir|0755); err != nil {
if !os.IsExist(err) {
panic(err)
}
}
}
func copyy(tamplate, agenda string) {
from_f, err := os.Open(tamplate)
if err != nil {
panic(err)
}
defer from_f.Close()
if _, err := os.Stat(agenda); err == nil {
fmt.Println(agenda + " is alreadly exist")
return
}
to_f, err := os.Create(agenda)
if err != nil {
panic(err)
}
defer to_f.Close()
_, err = io.Copy(to_f, from_f)
if err != nil {
panic(err)
}
}
func divide(arg string) (string, string) {
dir_path := ""
agenda := ""
if arg == "" {
year, month, day := time.Now().Date()
dir_path = strconv.Itoa(year) + "/" + strconv.Itoa(int(month))
agenda = dir_path + "/" + strconv.Itoa(day) + ".md"
} else {
path := strings.Split(arg, "/")
for _, v := range path[:len(path)-1] {
dir_path = dir_path + v + "/"
}
agenda = arg
}
return dir_path, agenda
}